目录
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;
}
}
}