.net 安裝Postgresql驅動程序ngpsql
最近搞一個物聯網項目,需要采集fanuc數控機床的數據,廠家提供的API只支持windows,所以就決定C#開發,數據庫用postgresql, 安裝數據庫驅動一波三折。
作為一個討厭微軟的老程序猿,一看到.net這個亂,更討厭了
現在.net的框架。
- .net framework
- .net standard
- .net core
- .net
亂不亂? 都是些啥玩意,不扯遠了,我是在討厭微軟的路上越走越遠了,可是為了生活,還要弄這惡心的玩意。。
一、安裝
ngpsql下載地址:
https://www.nuget.org/stats/packages/Npgsql?groupby=Version
一定要根據項目使用的框架選擇合適的驅動,我的項目使用的框架是.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,Version=v4.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/K32daEqZI+FFr96OlZkgwK2LcT3pZ2zWubR5zTPW3/FkO1Rat9kb7oQOa4rxgl9LJHc5tspCWfg== 從 https://api.nuget.org/v3/index.json 安裝 Microsoft.Bcl.AsyncInterfaces 1.1.0 。
已通過內容哈希 HxozeSlipUK7dAroTYwIcGwKDeOVpQnJlpVaOkBz7CM4TsE5b/tKlQBZecTjh6FzcSbxndYaxxpsBMz+wMJeyw== 從 https://api.nuget.org/v3/index.json 安裝 System.Runtime.CompilerServices.Unsafe 4.6.0 。
已通過內容哈希 pL2ChpaRRWI/p4LXyy4RgeWlYF2sgfj/pnVMvBqwNFr5cXg7CXNnWZWxrOONLg8VGdFB8oB+EG2Qw4MLgTOe+A== 從 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/nklD0Hre1Bbce048ZqryGZVFifGNPuh+2jqF1i/jLJLMfFGZIzUOw+nCIeH24SQhghDSPw== 從 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 。
已通過內容哈希 4F8Xe+JIkVoDJ8hDAZ7HqLkjctN/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”
已通過內容哈希 VGcF1AQRGd1+HQvXFqc0u+swxZMGCFy/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(); // 關閉數據庫連接}}
測試沒有問題,折騰了一天,終于搞定。