当前位置:   article > 正文

DbHelper通用数据库类及增删改 使用示例

dbhelpersql删除数据库
  1. DbHelper db = new DbHelper();
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. }
  5. //新增数据
  6. protected void Button1_Click(object sender, EventArgs e)
  7. {
  8. List<FieldTypeValue> ftvlist = new List<FieldTypeValue>();
  9. ftvlist.Add(new FieldTypeValue("[D_Name]", "测试用户" + DateTime.Now.ToString("yyyyMMddhhmmss")));
  10. ftvlist.Add(new FieldTypeValue("[D_Password]", "测试密码" + DateTime.Now.ToString("yyyyMMddhhmmss")));
  11. ftvlist.Add(new FieldTypeValue("[D_Else]", "测试备注" + DateTime.Now.ToString("yyyyMMddhhmmss")));
  12. string sql = BuilderSql.createInsertSql("TestTable", ftvlist);
  13. int opFlag = db.ExecuteNonQuery(db.GetSqlStringCommand(sql));
  14. if (opFlag > 0) { JsHelper.Alert("新增成功!", Page); } else { JsHelper.Alert("新增失败!", Page); };
  15. }
  16. //修改数据
  17. protected void Button2_Click(object sender, EventArgs e)
  18. {
  19. List<FieldTypeValue> ftvlist = new List<FieldTypeValue>();
  20. ftvlist.Add(new FieldTypeValue("[D_Name]", "这是个错误dsadsadasd"));
  21. ftvlist.Add(new FieldTypeValue("[D_Password]", "aaa这是个错误,我还没有处理"));
  22. ftvlist.Add(new FieldTypeValue("[D_Else]", "aaa这是个错误,我还没有处理"));
  23. string sql = BuilderSql.createUpdateSql("TestTable", ftvlist, "D_ID", "1");
  24. int opFlag = db.ExecuteNonQuery(db.GetSqlStringCommand(sql));
  25. if (opFlag > 0) { JsHelper.Alert("修改成功!", Page); } else { JsHelper.Alert("修改失败!", Page); };
  26. }
  27. //删除数据
  28. protected void Button3_Click(object sender, EventArgs e)
  29. {
  30. string sql = BuilderSql.createDeleteSql("[TestTable]", "[D_Id]", "1");
  31. int opFlag = db.ExecuteNonQuery(db.GetSqlStringCommand(sql));
  32. if (opFlag > 0) { JsHelper.Alert("删除成功!", Page); } else { JsHelper.Alert("删除失败!", Page); };
  33. }
  34. //事务提交
  35. protected void Button4_Click(object sender, EventArgs e)
  36. {
  37. using (Trans t = new Trans())
  38. {
  39. try
  40. {
  41. db.ExecuteNonQuery(db.GetSqlStringCommand("insert into TestTable(D_Name,D_Password,D_Else) values('aaaaa1','bbbbbb1','cccccc1')"), t);
  42. db.ExecuteNonQuery(db.GetSqlStringCommand("insert into TestTable(D_Name,D_Password,D_Else) values('aaaaa2','bbbbbb2','cccccc2')"), t);
  43. db.ExecuteNonQuery(db.GetSqlStringCommand("insert into TestTable(D_Name,D_Password,D_Else) values('aaaaa3','bbbbbb3','cccccc3')"), t);
  44. db.ExecuteNonQuery(db.GetSqlStringCommand("insert into TestTable(D_Name,D_Password,D_Else) values('aaaaa4','bbbbbb4','cccccc4')"), t);
  45. db.ExecuteNonQuery(db.GetSqlStringCommand("insert into TestTable(D_Name,D_Password,D_Else) values('aaaaa5','bbbbbb5','cccccc5')"), t);
  46. t.Commit();
  47. JsHelper.Alert("事务提交成功!", Page);
  48. }
  49. catch
  50. {
  51. t.RollBack();
  52. JsHelper.Alert("事务提交失败!", Page);
  53. }
  54. }
  55. }
  56. //分页控件分页
  57. protected void Page_Load(object sender, EventArgs e)
  58. {
  59. if (!IsPostBack)
  60. {
  61. BindGridViewData();
  62. }
  63. }
  64. private void BindGridViewData()
  65. {
  66. string strCondition = "";
  67. MSCL.PageHelper wp = new MSCL.PageHelper();
  68. wp.TableName = "TestTable";
  69. wp.KeyField = "D_Id";
  70. wp.SortName = "D_Id";
  71. wp.Condition = strCondition;
  72. wp.CurrentPageIndex = AspNetPager1.CurrentPageIndex;
  73. wp.PageSize = AspNetPager1.PageSize;//=PageSize;
  74. DataTable dt = wp.GetDataTableMyPage();
  75. GridView1.DataSource = dt;
  76. GridView1.DataBind();
  77. AspNetPager1.RecordCount = wp.RecordCount;
  78. AspNetPager1.CustomInfoHTML = " 共<b>" + wp.RecordCount.ToString() + "</b>条记录/";
  79. AspNetPager1.CustomInfoHTML += " <b>" + wp.PageCount.ToString() + "</b>页";
  80. AspNetPager1.CustomInfoHTML += " 当前第<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>页";
  81. }
  82. protected void AspNetPager1_PageChanged(object sender, EventArgs e)
  83. {
  84. BindGridViewData();
  85. }
  86. //字符串分页
  87. protected void Page_Load(object sender, EventArgs e)
  88. {
  89. if (!IsPostBack)
  90. {
  91. bindData();
  92. }
  93. }
  94. protected void bindData()
  95. {
  96. int count = 0; //记录总数
  97. int pageSize = 10; //每页显示记录数
  98. int pageIndex = (Request["PageIndex"] == null) ? 1 : Convert.ToInt32(Request["PageIndex"]);
  99. DataSet ds = MSCL.PageHelper.PageList("TestTable", pageSize, pageIndex, "D_ID", true, "", out count);
  100. GridView1.DataSource = ds;
  101. GridView1.DataBind();
  102. ltPage.Text = MSCL.PageHelper.ToSplitPageHtml(count, pageSize, 5, 1, "", true);
  103. }
  104. 以上我们好像没有指定数据库连接字符串,大家如果看下DbHelper的代码,就知道要使用它必须在config中配置两个参数,如下:
  105. <appSettings>
  106. <add key="DbHelperProvider" value="System.Data.SqlClient"/>
  107. <add key="DbHelperConnectionString" value="Data Source=(local);Initial Catalog=DbHelperTest;Persist Security Info=True;User ID=sa;Password=sa"/>
  108. </appSettings>


 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace FairHR.Util
  6. {
  7. #region 数据表字段类
  8. /// <summary>
  9. /// 数据表字段类
  10. /// </summary>
  11. public class FieldTypeValue
  12. {
  13. /// <summary>
  14. /// 字段容器
  15. /// </summary>
  16. /// <param name="fieldName">字段名</param>
  17. /// <param name="fieldValue">字段值</param>
  18. /// <param name="isNum"></param>
  19. public FieldTypeValue(string fieldName, string fieldValue, bool isNum)
  20. {
  21. this.fieldName = fieldName;
  22. this.fieldValue = fieldValue;
  23. this.isNum = isNum;
  24. }
  25. /// <summary>
  26. /// 字段容器
  27. /// </summary>
  28. /// <param name="fieldName">字段名</param>
  29. /// <param name="fieldValue">字段值</param>
  30. public FieldTypeValue(string fieldName, string fieldValue)
  31. {
  32. this.fieldName = fieldName;
  33. this.fieldValue = fieldValue;
  34. }
  35. private string fieldName;
  36. /// <summary>
  37. /// 字段名
  38. /// </summary>
  39. public string FieldName
  40. {
  41. get { return fieldName; }
  42. set { fieldName = value; }
  43. }
  44. private bool isNum = false;
  45. /// <summary>
  46. /// 是否数字
  47. /// </summary>
  48. public bool IsNum
  49. {
  50. get { return isNum; }
  51. set { isNum = value; }
  52. }
  53. private string fieldValue;
  54. /// <summary>
  55. /// 字段值
  56. /// </summary>
  57. public string FieldValue
  58. {
  59. get { return fieldValue; }
  60. set { fieldValue = value; }
  61. }
  62. }
  63. #endregion
  64. #region SQL语句的构造类
  65. /// <summary>
  66. /// SQL语句的构造类
  67. /// </summary>
  68. public class BuilderSql
  69. {
  70. /// <summary>
  71. /// 构造新增Insert语句
  72. /// </summary>
  73. /// <param name="tableName">表名</param>
  74. /// <param name="ftvlist">list</param>
  75. /// <returns></returns>
  76. public static string createInsertSql(string tableName, List<FieldTypeValue> ftvlist)
  77. {
  78. StringBuilder sb = new StringBuilder();
  79. sb.Append(" insert into ");
  80. sb.Append(tableName);
  81. sb.Append("(");
  82. for (int i = 0; i < ftvlist.Count; i++)
  83. {
  84. FieldTypeValue ftv = (FieldTypeValue)ftvlist[i];
  85. if (i != ftvlist.Count - 1)
  86. {
  87. sb.Append(ftv.FieldName + ",");
  88. }
  89. else
  90. {
  91. sb.Append(ftv.FieldName);
  92. }
  93. }
  94. sb.Append(") values(");
  95. for (int i = 0; i < ftvlist.Count; i++)
  96. {
  97. FieldTypeValue ftv = (FieldTypeValue)ftvlist[i];
  98. if (ftv.IsNum)
  99. {
  100. if (i != ftvlist.Count - 1)
  101. {
  102. sb.Append(ftv.FieldValue + ",");
  103. }
  104. else
  105. {
  106. sb.Append(ftv.FieldValue);
  107. }
  108. }
  109. else
  110. {
  111. if (i != ftvlist.Count - 1)
  112. {
  113. sb.Append("'" + ftv.FieldValue + "',");
  114. }
  115. else
  116. {
  117. sb.Append("'" + ftv.FieldValue + "'");
  118. }
  119. }
  120. }
  121. sb.Append(")");
  122. return sb.ToString();
  123. }
  124. /// <summary>
  125. /// 构造更新Update语句
  126. /// </summary>
  127. /// <param name="tableName">表名</param>
  128. /// <param name="ftvlist">list</param>
  129. /// <param name="pkName">条件字段名</param>
  130. /// <param name="pkValue">条件字段值</param>
  131. /// <returns></returns>
  132. public static string createUpdateSql(string tableName, List<FieldTypeValue> ftvlist, string pkName, string pkValue)
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. sb.Append(" update ");
  136. sb.Append(tableName);
  137. sb.Append(" set");
  138. for (int i = 0; i < ftvlist.Count; i++)
  139. {
  140. FieldTypeValue ftv = (FieldTypeValue)ftvlist[i];
  141. if (i != ftvlist.Count - 1)
  142. {
  143. if (ftv.IsNum)
  144. {
  145. sb.Append(" " + ftv.FieldName + "=" + ftv.FieldValue + ",");
  146. }
  147. else
  148. {
  149. sb.Append(" " + ftv.FieldName + "='" + ftv.FieldValue + "',");
  150. }
  151. }
  152. else
  153. {
  154. if (ftv.IsNum)
  155. {
  156. sb.Append(" " + ftv.FieldName + "=" + ftv.FieldValue + "");
  157. }
  158. else
  159. {
  160. sb.Append(" " + ftv.FieldName + "='" + ftv.FieldValue + "'");
  161. }
  162. }
  163. }
  164. sb.Append(" where " + pkName + "='" + pkValue + "'");
  165. return sb.ToString();
  166. }
  167. /// <summary>
  168. /// 构造删除Delete语句
  169. /// </summary>
  170. /// <param name="tableName">表名</param>
  171. /// <param name="pkName">条件字段名</param>
  172. /// <param name="pkValue">条件字段值</param>
  173. /// <returns></returns>
  174. public static string createDeleteSql(string tableName, string pkName, string pkValue)
  175. {
  176. StringBuilder sb = new StringBuilder();
  177. sb.Append(" delete from ");
  178. sb.Append(tableName);
  179. sb.Append(" where " + pkName + " = '" + pkValue + "'");
  180. return sb.ToString();
  181. }
  182. }
  183. #endregion
  184. }


转载于:https://my.oschina.net/smartsmile/blog/814943

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/860809
推荐阅读
相关标签
  

闽ICP备14008679号