SqlHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.SqlClient;
  6. using System.Data;
  7. using System.Configuration;
  8. namespace DAL
  9. {
  10. public class SqlHelper
  11. {
  12. //获得数据库连接字符串
  13. public static readonly string conString = ConfigurationManager.ConnectionStrings["sqlCon"].ConnectionString;
  14. /// <summary>
  15. /// 创建数据库连接对象
  16. /// </summary>
  17. /// <returns>返回数据库连接对象</returns>
  18. public static SqlConnection createCon()
  19. {
  20. return new SqlConnection(conString);
  21. }
  22. /// <summary>
  23. /// 创建执行对象
  24. /// </summary>
  25. /// <param name="cmdText">文本内容</param>
  26. /// <param name="cmdType">文本类型</param>
  27. /// <param name="param">可变参数数组</param>
  28. /// <returns>返回执行对象</returns>
  29. public static SqlCommand createCmd(string cmdText, CommandType cmdType, params SqlParameter[] param)
  30. {
  31. //实例化执行对象
  32. SqlCommand cmd = new SqlCommand();
  33. //执行连接对象
  34. cmd.Connection = createCon();
  35. //执行文本内容
  36. cmd.CommandText = cmdText;
  37. //选择文本类型
  38. cmd.CommandType = cmdType;
  39. //判断可变参数数组里是否为null 如果不为null 则添加进可变参数数组中
  40. if (param != null)
  41. foreach (SqlParameter p in param)
  42. cmd.Parameters.Add(p);
  43. //cmd.Parameters.Clear();//执行完后清理SqlParameter中的值
  44. //返回执行对象
  45. return cmd;
  46. }
  47. /// <summary>
  48. /// 创建ExcuteReader()方法
  49. /// </summary>
  50. /// <param name="cmdText">文本内容</param>
  51. /// <param name="cmdType">文本类型</param>
  52. /// <param name="param">可变参数数组</param>
  53. /// <returns>返回读取对象</returns>
  54. public static SqlDataReader ExcuteReader(string cmdText, CommandType cmdType, params SqlParameter[] param)
  55. {
  56. //创建执行对象
  57. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  58. try
  59. {
  60. //打开连接
  61. cmd.Connection.Open();
  62. //执行读取方法
  63. SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  64. //返回读取对象
  65. return dr;
  66. }
  67. catch(Exception ex)
  68. {
  69. //关闭连接
  70. cmd.Connection.Close();
  71. //返回空
  72. return null;
  73. }
  74. }
  75. /// <summary>
  76. /// 创建ExecuteScalar()方法
  77. /// </summary>
  78. /// <param name="cmdText">文本内容</param>
  79. /// <param name="cmdType">文本类型</param>
  80. /// <param name="param">可变参数数组</param>
  81. /// <returns>返回第1行第1列的值</returns>
  82. public static Object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] param)
  83. {
  84. //创建执行对象
  85. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  86. try
  87. {
  88. //打开连接
  89. cmd.Connection.Open();
  90. //执行查询方法
  91. Object obj = cmd.ExecuteScalar();
  92. //返回第1行第1列的值
  93. return obj;
  94. }
  95. catch(Exception EX)
  96. {
  97. //关闭连接
  98. cmd.Connection.Close();
  99. //返回空
  100. return null;
  101. }
  102. }
  103. /// <summary>
  104. /// 创建ExecuteNonQuery()方法
  105. /// </summary>
  106. /// <param name="cmdText">文本内容</param>
  107. /// <param name="cmdType">文本类型</param>
  108. /// <param name="param">可变参数数组</param>
  109. /// <returns>返回受影响行数</returns>
  110. public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] param)
  111. {
  112. //创建执行对象
  113. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  114. try
  115. {
  116. //打开连接
  117. cmd.Connection.Open();
  118. //执行ExecuteNonQuery()方法
  119. int count = cmd.ExecuteNonQuery();
  120. //关闭连接
  121. cmd.Connection.Close();
  122. //返回受影响行数
  123. return count;
  124. }
  125. catch(Exception e)
  126. {
  127. string error = e.Message.ToString();
  128. //关闭连接
  129. cmd.Connection.Close();
  130. //返回 0
  131. return 0;
  132. }
  133. }
  134. /// <summary>
  135. /// 调用存储过程
  136. /// </summary>
  137. /// <param name="cmdText"></param>
  138. /// <param name="cmdType"></param>
  139. /// <param name="param"></param>
  140. /// <returns>返回DataTable类型的数据</returns>
  141. public static DataTable TransferProcedure(string cmdText, CommandType cmdType, params SqlParameter[] param)
  142. {
  143. //创建执行对象
  144. SqlCommand cmd = createCmd(cmdText, cmdType, param);
  145. try
  146. {
  147. //打开连接
  148. cmd.Connection.Open();
  149. SqlDataAdapter sda = new SqlDataAdapter(cmd);
  150. DataTable dt = new DataTable();
  151. sda.Fill(dt);
  152. //关闭连接
  153. cmd.Connection.Close();
  154. if (dt != null)
  155. return dt;
  156. else
  157. return null;
  158. }
  159. catch
  160. {
  161. //关闭连接
  162. cmd.Connection.Close();
  163. //返回 0
  164. return null;
  165. }
  166. }
  167. }
  168. }