C#连接操作PostgreSQL数据库
|
admin
2025年7月4日 21:40
本文热度 13
|
数据库 主要是PostgreSQL15
开发软件 Visual Studio 2012
C#用到DLL是 Npgsql.dll
二.连接数据库连接串
Server=数据库IP;Port=5432;User Id=用户名;Password=密码;Database=数据库;
Server是数据库IP
Port端口5432(默认)
User Id用户名
Password密码
Database数据库名
三.执行查询语句,返回DataSet
第一步创建数据库连接
NpgsqlConnection connection = new NpgsqlConnection(PGConnection)
PGConnection 为连接数据库连接串
第二步打开数据库连接
connection.Open();
第三步执行 使用NpgsqlCommand构造SQL语句
NpgsqlCommand command = new NpgsqlCommand(SQLString, connection);
第四步执行 查询语句 返回 NpgsqlDataAdapter类
NpgsqlDataAdapter npgdata = new NpgsqlDataAdapter(command);
第五步 NpgsqlDataAdapter填充DataSet
npgdata.Fill(ds);
第六步关闭数据库连接
connection.Close();
四.执行修改 删除 创建语句,返回是否成功
第一步创建数据库连接
NpgsqlConnection connection = new NpgsqlConnection(PGConnection)
PGConnection 为连接数据库连接串
第二步打开数据库连接
connection.Open();
第三步执行 使用NpgsqlCommand类 执行数据库执行的 SQL 语句
NpgsqlCommand command = new NpgsqlCommand(SQLString, connection);
第四步 获取受影响的行数
int recordCount = command.ExecuteNonQuery();
第五步关闭数据库连接
connection.Close();
阅读原文:原文链接
该文章在 2025/7/7 11:55:17 编辑过