门户网站好处,WordPress页面批量生成,网站推广的基本手段,wordpress畅言.net 安装Postgresql驱动程序ngpsql
最近搞一个物联网项目#xff0c;需要采集fanuc数控机床的数据#xff0c;厂家提供的API只支持windows#xff0c;所以就决定C#开发#xff0c;数据库用postgresql#xff0c; 安装数据库驱动一波三折。
作为一个讨厌微软的老程序猿需要采集fanuc数控机床的数据厂家提供的API只支持windows所以就决定C#开发数据库用postgresql 安装数据库驱动一波三折。
作为一个讨厌微软的老程序猿一看到.net这个乱更讨厌了
现在.net的框架。
.net framework.net standard.net core.net
乱不乱 都是些啥玩意不扯远了我是在讨厌微软的路上越走越远了可是为了生活还要弄这恶心的玩意。。
一、安装
ngpsql下载地址
https://www.nuget.org/stats/packages/Npgsql?groupbyVersion
一定要根据项目使用的框架选择合适的驱动我的项目使用的框架是.net framework 4.8,只能选择4.x的ngpsql再新的版本不支持了。
打开4.1.12的页面https://www.nuget.org/packages/Npgsql/4.1.12 在vs中工具菜单Nuget包管理器管理解决方案的Nuget程序包看看源 查看有没有这个源 如果没有手工添加一下。然后打开工具nuget包管理器程序包管理器控制台
这行这个命令
NuGet\Install-Package Npgsql -Version 4.1.12(base) PM NuGet\Install-Package Npgsql -Version 4.1.12正在尝试收集与目标为“.NETFramework,Versionv4.6.1”的项目“Test”有关的包“Npgsql.4.1.12”的依赖项信息
收集依赖项信息花费时间 9.77 秒
正在尝试解析程序包“Npgsql.4.1.12”的依赖项DependencyBehavior 为“Lowest”
解析依赖项信息花费时间 0 毫秒
正在解析操作以安装程序包“Npgsql.4.1.12”
已解析操作以安装程序包“Npgsql.4.1.12”
从“nuget.org”检索包“Microsoft.Bcl.AsyncInterfaces 1.1.0”
从“nuget.org”检索包“Npgsql 4.1.12”
从“nuget.org”检索包“System.Buffers 4.5.0”
从“nuget.org”检索包“System.Memory 4.5.3”
从“nuget.org”检索包“System.Numerics.Vectors 4.5.0”
从“nuget.org”检索包“System.Runtime.CompilerServices.Unsafe 4.6.0”
从“nuget.org”检索包“System.Text.Encodings.Web 4.6.0”
从“nuget.org”检索包“System.Text.Json 4.6.0”
从“nuget.org”检索包“System.Threading.Tasks.Extensions 4.5.3”
从“nuget.org”检索包“System.ValueTuple 4.5.0” GET https://api.nuget.org/v3-flatcontainer/npgsql/4.1.12/npgsql.4.1.12.nupkgGET https://api.nuget.org/v3-flatcontainer/system.buffers/4.5.0/system.buffers.4.5.0.nupkgGET https://api.nuget.org/v3-flatcontainer/system.text.json/4.6.0/system.text.json.4.6.0.nupkgGET https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/1.1.0/microsoft.bcl.asyncinterfaces.1.1.0.nupkgGET https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/4.6.0/system.text.encodings.web.4.6.0.nupkgGET https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/4.6.0/system.runtime.compilerservices.unsafe.4.6.0.nupkgGET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.5.3/system.threading.tasks.extensions.4.5.3.nupkgGET https://api.nuget.org/v3-flatcontainer/system.memory/4.5.3/system.memory.4.5.3.nupkgOK https://api.nuget.org/v3-flatcontainer/system.buffers/4.5.0/system.buffers.4.5.0.nupkg 142 毫秒OK https://api.nuget.org/v3-flatcontainer/system.memory/4.5.3/system.memory.4.5.3.nupkg 143 毫秒OK https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.5.3/system.threading.tasks.extensions.4.5.3.nupkg 180 毫秒OK https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/1.1.0/microsoft.bcl.asyncinterfaces.1.1.0.nupkg 186 毫秒OK https://api.nuget.org/v3-flatcontainer/system.text.json/4.6.0/system.text.json.4.6.0.nupkg 226 毫秒OK https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/4.6.0/system.runtime.compilerservices.unsafe.4.6.0.nupkg 232 毫秒OK https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/4.6.0/system.text.encodings.web.4.6.0.nupkg 274 毫秒OK https://api.nuget.org/v3-flatcontainer/npgsql/4.1.12/npgsql.4.1.12.nupkg 308 毫秒
已通过内容哈希 1Am6l4Vpn3/K32daEqZIFFr96OlZkgwK2LcT3pZ2zWubR5zTPW3/FkO1Rat9kb7oQOa4rxgl9LJHc5tspCWfg 从 https://api.nuget.org/v3/index.json 安装 Microsoft.Bcl.AsyncInterfaces 1.1.0 。
已通过内容哈希 HxozeSlipUK7dAroTYwIcGwKDeOVpQnJlpVaOkBz7CM4TsE5b/tKlQBZecTjh6FzcSbxndYaxxpsBMzwMJeyw 从 https://api.nuget.org/v3/index.json 安装 System.Runtime.CompilerServices.Unsafe 4.6.0 。
已通过内容哈希 pL2ChpaRRWI/p4LXyy4RgeWlYF2sgfj/pnVMvBqwNFr5cXg7CXNnWZWxrOONLg8VGdFB8oBEG2Qw4MLgTOeA 从 https://api.nuget.org/v3/index.json 安装 System.Buffers 4.5.0 。
已通过内容哈希 MvhNtcvIbqmhANyKu91jQnvIRVSTiaOiFNfKWwXGHG48YAb4I/TyH8spsySiPYla7gKal5ZnF3teJqZAximyQ 从 https://api.nuget.org/v3/index.json 安装 System.Threading.Tasks.Extensions 4.5.3 。
已通过内容哈希 BXgFO8Yi7ao7hVA/nklD0Hre1Bbce048ZqryGZVFifGNPuh2jqF1i/jLJLMfFGZIzUOwnCIeH24SQhghDSPw 从 https://api.nuget.org/v3/index.json 安装 System.Text.Encodings.Web 4.6.0 。
正在将程序包“System.Buffers.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已通过内容哈希 3oDzvc/zzetpTKWMShs1AADwZjQ/36HnsufHRPcOjyRAAMLDlu2iD33MBI2opxnezcVUtXyqDXXjoFMOU9c7SA 从 https://api.nuget.org/v3/index.json 安装 System.Memory 4.5.3 。
已通过内容哈希 4F8XeJIkVoDJ8hDAZ7HqLkjctN/6WItJIzQaifBwClC7wmoLSda/Sv2i6i1kycqDb3hWF4JCVbpAweyOKHEUA 从 https://api.nuget.org/v3/index.json 安装 System.Text.Json 4.6.0 。
已将程序包“System.Buffers.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已通过内容哈希 VGcF1AQRGd1HQvXFqc0uswxZMGCFy/T8eCk7w2Jo9oXYkNWdb7FWQVd7lM1Zm0gwVucK04smyneMXnUdhI7Q 从 https://api.nuget.org/v3/index.json 安装 Npgsql 4.1.12 。
已将程序包“System.Buffers.4.5.0”添加到“packages.config”
已将“System.Buffers 4.5.0”成功安装到 Test
正在将程序包“System.Numerics.Vectors.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Numerics.Vectors.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Numerics.Vectors.4.5.0”添加到“packages.config”
已将“System.Numerics.Vectors 4.5.0”成功安装到 Test
正在将程序包“System.Runtime.CompilerServices.Unsafe.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Runtime.CompilerServices.Unsafe.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Runtime.CompilerServices.Unsafe.4.6.0”添加到“packages.config”
已将“System.Runtime.CompilerServices.Unsafe 4.6.0”成功安装到 Test
正在将程序包“System.Memory.4.5.3”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Memory.4.5.3”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Memory.4.5.3”添加到“packages.config”
已将“System.Memory 4.5.3”成功安装到 Test
正在将程序包“System.Text.Encodings.Web.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Text.Encodings.Web.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Text.Encodings.Web.4.6.0”添加到“packages.config”
已将“System.Text.Encodings.Web 4.6.0”成功安装到 Test
正在将程序包“System.Threading.Tasks.Extensions.4.5.3”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Threading.Tasks.Extensions.4.5.3”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Threading.Tasks.Extensions.4.5.3”添加到“packages.config”
已将“System.Threading.Tasks.Extensions 4.5.3”成功安装到 Test
正在将程序包“Microsoft.Bcl.AsyncInterfaces.1.1.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“Microsoft.Bcl.AsyncInterfaces.1.1.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“Microsoft.Bcl.AsyncInterfaces.1.1.0”添加到“packages.config”
已将“Microsoft.Bcl.AsyncInterfaces 1.1.0”成功安装到 Test
正在将程序包“System.ValueTuple.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.ValueTuple.4.5.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.ValueTuple.4.5.0”添加到“packages.config”
已将“System.ValueTuple 4.5.0”成功安装到 Test
正在将程序包“System.Text.Json.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Text.Json.4.6.0”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“System.Text.Json.4.6.0”添加到“packages.config”
已将“System.Text.Json 4.6.0”成功安装到 Test
正在将程序包“Npgsql.4.1.12”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“Npgsql.4.1.12”添加到文件夹“D:\BaiduSyncdisk\7_浩信\6_hx_lot\Test\packages”
已将程序包“Npgsql.4.1.12”添加到“packages.config”
已将“Npgsql 4.1.12”成功安装到 Test
执行 nuget 操作花费时间 21.43 秒
已用时间: 00:00:32.9171668它会自动解决依赖问题,前提是源要设置正确。
二、测试
static void TestPG(){// 连接字符串string connString $Host{dbIp};Username{dbUser};Password{dbPasswd};Database{dbName};// 创建连接对象using (var conn new NpgsqlConnection(connString)){conn.Open(); // 打开数据库连接// 执行查询using (var cmd new NpgsqlCommand(SELECT id,login FROM res_users, conn)){using (var reader cmd.ExecuteReader()){while (reader.Read()){// 读取数据int id reader.GetInt32(0);string name reader.GetString(1);Console.WriteLine($ID: {id}, Name: {name});}}}// 执行插入//using (var cmd new NpgsqlCommand(INSERT INTO mytable (id, name) VALUES (id, name), conn))//{// cmd.Parameters.AddWithValue(id, 1);// cmd.Parameters.AddWithValue(name, John Doe);// int rowsAffected cmd.ExecuteNonQuery();// Console.WriteLine($Rows Affected: {rowsAffected});//}conn.Close(); // 关闭数据库连接}}测试没有问题折腾了一天终于搞定。