C# PgSql工具类

目录

1、安装pgsql工具包

2、连接pgsql数据库

3、执行一条sql语句

4、执行多条SQL语句

5、执行sql语句返回datatable

6、执行sql返回泛型list集合

7、转换空值


1、安装pgsql工具包
2、连接pgsql数据库

public NpgsqlConnection Conn = null;//连接数据库

public string Host = "localhost";

public string Username = "postgres";

public string Port = "5432";

public string Password = "123456";

public string Database = "postgres"; //基础数据库 根据需求更改数据库名称

public bool StarConn()

{

try

{

string connstr = string.Format(@"Host={0};Database={1};Username={2};Port={3};Password={4};MaxPoolSize=500;", Host, Database, Username, Port, Password);

Conn = new NpgsqlConnection(connstr);

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

return Conn.State == ConnectionState.Open;

}

catch (Exception ex)

{//异常处理

return false;

}

}

3、执行一条sql语句

public bool ExcuteNonQuery(string sql, NpgsqlParameter[] param = null)

{

try

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

using (NpgsqlCommand com = new NpgsqlCommand(sql, Conn))

{

if (param != null)//添加参数

{

SetParmDBNull(param);

com.Parameters.AddRange(param);

}

int r = com.ExecuteNonQuery();

return true;

}

}

catch (Exception ex)

{

//"执行SQL语句异常"+ ex.Message;

return false;

}

}

4、执行多条SQL语句

public bool ExcuteNonQueryBatch(List<String> SQLStringList)

{

try

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

using (NpgsqlCommand cmd = new NpgsqlCommand())

{

cmd.Connection = Conn;

NpgsqlTransaction tx = Conn.BeginTransaction();

cmd.Transaction = tx;

int count = 0;

for (int i = 0; i < SQLStringList.Count; i++)

{

cmd.CommandType = CommandType.Text;

cmd.CommandText = SQLStringList[i];

count += cmd.ExecuteNonQuery();

}

tx.Commit();

return true;

}

}

catch (Exception ex)

{

//"批量执行sql语句异常"+ ex.Message;

return false;

}

}

5、执行sql语句返回datatable

public DataTable GetDataTable(string strsql, NpgsqlParameter[] param = null)//获取datatable

{

DataTable dt = new DataTable();

try

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

using (var nda = new NpgsqlDataAdapter(strsql, Conn))

{

if (param != null)//添加参数

{

SetParmDBNull(param);

nda.SelectCommand.Parameters.AddRange(param);

}

nda.Fill(dt);

return dt;

}

}

catch (Exception ex) { //"查询数据返回datatable异常"+ ex.Message; return dt; }

}

6、执行sql返回泛型list集合

public List<T> GetList<T>(string strsql)

{

List<T> ts = new List<T>();

try

{

if (Conn.State != ConnectionState.Open)

{

Conn.Open();

}

T t = default(T);

using (var nda = new NpgsqlDataAdapter(strsql, Conn))

{

DataTable dt = new DataTable();

nda.Fill(dt);

// 获得此模型的类型

// Type type = typeof(McDevice);

string tempName = "";

foreach (DataRow dr in dt.Rows)

{

t = Activator.CreateInstance<T>();

// 获得此模型的公共属性

PropertyInfo[] propertys = t.GetType().GetProperties();

foreach (PropertyInfo pi in propertys)

{

tempName = pi.Name; // 检查DataTable是否包含此列

if (dt.Columns.Contains(tempName))

{

// 判断此属性是否有Setter

if (!pi.CanWrite) continue;

object value = dr[tempName];

if (value != DBNull.Value)

pi.SetValue(t, value, null);

}

}

ts.Add(t);

}

}

}

catch (Exception ex){ CheckResults(false,"查询数据返回lis集合异常"+ ex.Message); }

return ts;

}

7、转换空值

public void SetParmDBNull(NpgsqlParameter[] param)

{

foreach (var p in param)

{

if (p.Value == null)

{

p.Value = DBNull.Value;

}

}

}

相关推荐
CodeJourney.26 分钟前
EndNote与Word关联:科研写作的高效助力
数据库·人工智能·算法·架构
trigger3331 小时前
MongoDB 简介
数据库·mongodb
许心月1 小时前
MongoDB#常用语句
数据库·mongodb
Jason95101 小时前
使用大语言模型(Deepseek)构建一个基于 SQL 数据的问答系统
数据库·sql·问答系统·大语言模型·deepseek
苍老流年1 小时前
Redis底层数据结构
数据结构·数据库·redis
三天不学习2 小时前
Redis面试宝典【刷题系列】
数据库·redis·面试
HaoHao_0102 小时前
如何将MySQL数据库迁移至阿里云
服务器·数据库·阿里云·云计算·云服务器·迁移
Мартин.2 小时前
[Meachines] [Easy] Wifinetic FTP匿名登录+Reaver WPS PIN密码泄露权限提升
数据库·postgresql·wps
茂桑2 小时前
MVCC(多版本并发控制)
java·开发语言·数据库
卷心菜不卷Iris2 小时前
第1章大型互联网公司的基础架构——1.9 LSM Tree
数据库·lsm-tree·互联网大厂·基础架构