postgresql的 sqlhelper
来源:互联网 发布:python 能做界面吗 编辑:程序博客网 时间:2024/06/16 01:00
算是积累一下自己的代码库,这几天虽然压抑了些,但也算有成长
public string ConnectionString { get; set; } public PgHelper(string connStr) { ConnectionString = connStr; } /// <summary> /// 执行SQL语句 /// </summary> /// <param name="sql">SQL</param> /// <returns>成功返回大于0的数字</returns> 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; } /// <summary> /// 参数化执行SQL语句 /// </summary> /// <param name="sql">带参数的SQL语句</param> /// <param name="paras">参数列表</param> /// <returns></returns> public static int ExecuteParameterSQL(string sql, Dictionary<string, string> 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; }
/// <summary> /// 批量执行SQL语句(事务) /// </summary> /// <param name="lstSql">SQL语句</param> /// <returns>true:执行成功,false:执行失败</returns> public bool ExecuteTransSQL(List<string> 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(); } } } } } /// <summary> /// 参数化批量执行SQL语句(事务) /// </summary> /// <param name="dic"></param> /// <returns></returns> public bool ExecuteParameterListSQL(Dictionary<string, Dictionary<string, string>> 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<string, Dictionary<string, string>> item in dic) { command.CommandText = item.Key; foreach (KeyValuePair<string, string> 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的时候没怎么批量插入过,现在发现真的蛮省事的。
1 0
- postgresql的 sqlhelper
- 自己写的sqlhelper
- VS2005的SQLHelper文件
- sqlhelper.cs的应用
- SqlHelper的用法
- 关于SqlHelper的学习
- 应用sqlhelper的注意事项
- 微软的SQLHelper类
- 我的SQLHelper
- sqlHelper的介绍文章
- 我的SQLHelper
- 常用的SQLHelper
- 自己写的SQLHelper
- 微软的SqlHelper类
- 我的SQLhelper
- 微软官方的SqlHelper
- Imba的SqlHelper
- sqlHelper的编写
- 个人博客系统 crazymad.cn
- 归并法外排序—海量数据排序
- Dojo1.11官方教程文档翻译(3.7)扩展NodeList
- 出现次数最多的整数
- Android动画失效
- postgresql的 sqlhelper
- Java 解惑:Comparable 和 Comparator 的区别
- 面试中会遇到的问题:TCP、HTTP、SOCKET之间的关系(详)
- UVALive-3637 The Bookcase(DP)
- BOM之Window案例
- 修改applicationid
- Android 为什么要有handler机制
- 字母图形
- Maximum Depth of Binary Tree ---LeetCode