postgresql的 sqlhelper

网友投稿 273 2022-11-21

postgresql的 sqlhelper

算是积累一下自己的代码库,这几天虽然压抑了些,但也算有成长 public string ConnectionString { get; set; } public PgHelper(string connStr) { ConnectionString = connStr; } ///

/// 执行SQL语句 /// /// SQL /// 成功返回大于0的数字 public int ExecuteSQL(string sql) { int num2 = -1; using (NpgsqlConnection connection = new NpgsqlConnection(ConnectionString)) { using (NpgsqlCommand command = new NpgsqlCommand(sql, connection)) { try { connection.Open(); num2=command.ExecuteNonQuery(); } catch (NpgsqlException exception) { throw new Exception(string.Format("执行SQL【{0}】出错,详细信息为:{1}", sql, exception.Message)); } finally { connection.Close(); } } } return num2; } /// /// 参数化执行SQL语句 /// /// 带参数的SQL语句 /// 参数列表 /// public static int ExecuteParameterSQL(string sql, Dictionary paras) { int num2 = -1; string strConn = ConfigurationManager.AppSettings["dbconn"].ToString(); using (NpgsqlConnection connection = new NpgsqlConnection(strConn)) { using (NpgsqlCommand command = new NpgsqlCommand(sql, connection)) { try { connection.Open(); foreach (string key in paras.Keys) { command.Parameters.Add(key, paras[key]); } num2 = command.ExecuteNonQuery(); } catch (NpgsqlException exception) { throw new Exception(string.Format("执行SQL【{0}】出错,详细信息为:{1}", sql, exception.Message)); } finally { connection.Close(); } } } return num2; } /// /// 批量执行SQL语句(事务) /// /// SQL语句 /// true:执行成功,false:执行失败 public bool ExecuteTransSQL(List lstSql) { if (lstSql == null || lstSql.Count == 0) { return false; } else { using (NpgsqlConnection dbConnection = new NpgsqlConnection(ConnectionString)) { using (NpgsqlCommand command = new NpgsqlCommand()) { NpgsqlTransaction ts = null; try { dbConnection.Open(); ts = dbConnection.BeginTransaction(); command.Connection = dbConnection; int cnt = 0; foreach (string item in lstSql) { if (!String.IsNullOrEmpty(item) && item.Trim().Length > 0) { command.CommandText = item; cnt += command.ExecuteNonQuery(); } } ts.Commit(); return true; } catch (NpgsqlException ex) { if (ts != null) { ts.Rollback(); } throw new Exception(string.Format("执行SQL出错:{0}", ex.Message)); return false; } finally { dbConnection.Close(); } } } } } /// /// 参数化批量执行SQL语句(事务) /// /// /// public bool ExecuteParameterListSQL(Dictionary> dic) { string strConn = ConnectionString; if (dic == null || dic.Count == 0) { return false; } else { NpgsqlTransaction ts = null; using (NpgsqlConnection connection = new NpgsqlConnection(strConn)) { using (NpgsqlCommand command = new NpgsqlCommand()) { try { connection.Open(); ts = connection.BeginTransaction(); command.Connection = connection; foreach (KeyValuePair> item in dic) { command.CommandText = item.Key; foreach (KeyValuePair para in item.Value) { command.Parameters.Add(para.Key, para.Value); } command.ExecuteNonQuery(); } ts.Commit(); return true; } catch (Exception ex) { if (ts != null) { ts.Rollback(); } throw ex; } finally { connection.Close(); } } } } } 目前我用的最多的就是:批量插入。以前在用sql的时候没怎么批量插入过,现在发现真的蛮省事的。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:多线程写到一个日志文件中
下一篇:CS5216DP转HDMI1080p转换器或者转接线设计原理
相关文章

 发表评论

暂时没有评论,来抢沙发吧~