.net 安装Postgresql驱动程序ngpsql

.net 安装Postgresql驱动程序ngpsql

最近搞一个物联网项目,需要采集fanuc数控机床的数据,厂家提供的API只支持windows,所以就决定C#开发,数据库用postgresql, 安装数据库驱动一波三折。

作为一个讨厌微软的老程序猿,一看到.net这个乱,更讨厌了

现在.net的框架。

  1. .net framework
  2. .net standard
  3. .net core
  4. .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

.net 安装Postgresql驱动程序ngpsql_第1张图片

在vs中,工具菜单,Nuget包管理器,管理解决方案的Nuget程序包,看看源

.net 安装Postgresql驱动程序ngpsql_第2张图片

查看有没有这个源:

.net 安装Postgresql驱动程序ngpsql_第3张图片

如果没有,手工添加一下。然后打开工具,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.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.buffers/4.5.0/system.buffers.4.5.0.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.text.json/4.6.0/system.text.json.4.6.0.nupkg
  GET https://api.nuget.org/v3-flatcontainer/microsoft.bcl.asyncinterfaces/1.1.0/microsoft.bcl.asyncinterfaces.1.1.0.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.text.encodings.web/4.6.0/system.text.encodings.web.4.6.0.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.runtime.compilerservices.unsafe/4.6.0/system.runtime.compilerservices.unsafe.4.6.0.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.threading.tasks.extensions/4.5.3/system.threading.tasks.extensions.4.5.3.nupkg
  GET https://api.nuget.org/v3-flatcontainer/system.memory/4.5.3/system.memory.4.5.3.nupkg
  OK 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(); // 关闭数据库连接
            }
        }

测试没有问题,折腾了一天,终于搞定。

你可能感兴趣的:(C#,.net,postgresql,数据库)