SqlHelper.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Configuration;
  10. using System.Data;
  11. using System.Data.SqlClient;
  12. using System.Reflection;
  13. namespace LeaRun.Application.Web.Common
  14. {
  15. public class SqlHelper
  16. {
  17. //连接字符串
  18. private static readonly string connStrN = ConfigurationManager.ConnectionStrings["BaseDb"].ConnectionString;
  19. //IP地址
  20. //private static String SqlServerId = Wujian_Jm.C_DesDecrypt(GetConfigValue("SqlServerId"), "WWKJ");
  21. ////用户名
  22. //private static String SqlUserId = Wujian_Jm.C_DesDecrypt(GetConfigValue("SqlUserId"), "WWKJ");
  23. ////用户密码
  24. //private static String PassWord = Wujian_Jm.C_DesDecrypt(GetConfigValue("PassWord"), "WWKJ");
  25. //数据库名
  26. //private static String DbaseName = GetConfigValue("DbaseName");
  27. //拼接链接字符串
  28. // private static r string connStrN = "data source=" + SqlServerId + ";initial catalog=" + DbaseName + ";user id=" + SqlUserId + ";password=" + PassWord + ";";
  29. //1.执行insert/delete/update的方法
  30. #region 配置文件字段的读取函数
  31. public static string GetConfigValue(string key)
  32. {
  33. string value = "";
  34. try
  35. {
  36. String Path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "XmlConfig/database.config";
  37. string assemblyConfigFile = Assembly.GetEntryAssembly().Location;
  38. Configuration config = ConfigurationManager.OpenExeConfiguration(assemblyConfigFile);
  39. AppSettingsSection app = config.AppSettings;
  40. value = app.Settings[key].Value;
  41. return value;
  42. }
  43. catch
  44. {
  45. return value;
  46. }
  47. }
  48. public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] pms)
  49. {
  50. using (SqlConnection con = new SqlConnection(connStrN))
  51. {
  52. using (SqlCommand cmd = new SqlCommand(sql, con))
  53. {
  54. //设置当前执行的是存储过程还是带参数的Sql语句
  55. cmd.CommandType = cmdType;
  56. if (pms != null)
  57. {
  58. cmd.Parameters.AddRange(pms);
  59. }
  60. try
  61. {
  62. con.Open();
  63. return cmd.ExecuteNonQuery();
  64. }
  65. catch
  66. {
  67. con.Close();
  68. con.Dispose();
  69. throw;
  70. }
  71. }
  72. }
  73. }
  74. #endregion
  75. public static int ExecuteNoQuery(string sql, CommandType cmdType, params SqlParameter[] pms)
  76. {
  77. using (SqlConnection con = new SqlConnection(connStrN))
  78. {
  79. using (SqlCommand cmd = new SqlCommand(sql, con))
  80. {
  81. //设置当前执行的是存储过程还是带参数的Sql语句
  82. cmd.CommandType = cmdType;
  83. if (pms != null)
  84. {
  85. cmd.Parameters.AddRange(pms);
  86. }
  87. try
  88. {
  89. con.Open();
  90. return cmd.ExecuteNonQuery();
  91. }
  92. catch
  93. {
  94. con.Close();
  95. con.Dispose();
  96. throw;
  97. }
  98. }
  99. }
  100. }
  101. //2.执行返回单个值的方法
  102. public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms)
  103. {
  104. ////IP地址
  105. //String SqlServerIP = Wujian_Jm.C_DesDecrypt(GetConfigValue("SqlServerId"), "WWKJ");
  106. ////用户名
  107. //String userId = Wujian_Jm.C_DesDecrypt(GetConfigValue("SqlUserId"), "WWKJ");
  108. ////用户密码
  109. //String pwd = Wujian_Jm.C_DesDecrypt(GetConfigValue("PassWord"), "WWKJ");
  110. ////数据库名
  111. //String dbName = GetConfigValue("DbaseName");
  112. ////拼接链接字符串
  113. //string connStrNew = "data source=" + SqlServerIP + ";initial catalog=" + dbName + ";user id=" + userId + ";password=" + pwd + ";";
  114. using (SqlConnection con = new SqlConnection(connStrN))
  115. {
  116. using (SqlCommand cmd = new SqlCommand(sql, con))
  117. {
  118. cmd.Parameters.Clear();
  119. //设置当前执行的是存储过程还是带参数的Sql语句
  120. cmd.CommandType = cmdType;
  121. if (pms != null)
  122. {
  123. cmd.Parameters.AddRange(pms);
  124. }
  125. con.Open();
  126. return cmd.ExecuteScalar();
  127. }
  128. }
  129. }
  130. //3.返回SqlDataReader的方法
  131. public static SqlDataReader ExecuteReader(string sql, CommandType cmdType, params SqlParameter[] pms)
  132. {
  133. SqlConnection con = new SqlConnection(connStrN);
  134. using (SqlCommand cmd = new SqlCommand(sql, con))
  135. {
  136. cmd.CommandType = cmdType;
  137. if (pms != null)
  138. {
  139. cmd.Parameters.AddRange(pms);
  140. }
  141. try
  142. {
  143. con.Open();
  144. SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); //修改 刘学铸 by 2017-06-02
  145. cmd.Parameters.Clear();
  146. return dr;//cmd.ExecuteReader(CommandBehavior.CloseConnection);
  147. }
  148. catch
  149. {
  150. con.Close();
  151. con.Dispose();
  152. throw;
  153. }
  154. }
  155. }
  156. //4.返回DataTable
  157. public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms)
  158. {
  159. DataTable dt = new DataTable();
  160. using (SqlDataAdapter adapter = new SqlDataAdapter(sql, connStrN))
  161. {
  162. adapter.SelectCommand.CommandType = cmdType;
  163. if (pms != null)
  164. {
  165. adapter.SelectCommand.Parameters.AddRange(pms);
  166. }
  167. adapter.Fill(dt);
  168. return dt;
  169. }
  170. }
  171. /// <summary>
  172. /// 执行sql语句无参数
  173. /// </summary>
  174. public static int ExecuteNoParams(string sql, CommandType cmdType)
  175. {
  176. using (SqlConnection conn = new SqlConnection(connStrN))
  177. {
  178. conn.Open();
  179. using (SqlCommand cmd = new SqlCommand(sql, conn))
  180. {
  181. cmd.CommandType = cmdType;
  182. try
  183. {
  184. return cmd.ExecuteNonQuery();
  185. }
  186. catch (Exception)
  187. {
  188. conn.Close();
  189. throw;
  190. }
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 带事务的存储过程
  196. /// </summary>
  197. /// <param name="procName">存储过程名称</param>
  198. /// <param name="parameters">存储过程参数-不能为空</param>
  199. /// <returns>0:执行成功 异常抛出</returns>
  200. public static int ExecStoredProcedure(string procName, params SqlParameter[] parameters)
  201. {
  202. int rtn = 0;
  203. using (SqlConnection conn = new SqlConnection(connStrN))
  204. {
  205. conn.Open();
  206. using (SqlCommand cmd = conn.CreateCommand())
  207. {
  208. SqlTransaction st = conn.BeginTransaction();
  209. cmd.Transaction = st;
  210. try
  211. {
  212. cmd.CommandText = procName;
  213. cmd.CommandType = CommandType.StoredProcedure;
  214. cmd.Parameters.AddRange(parameters);
  215. rtn = cmd.ExecuteNonQuery();
  216. st.Commit();
  217. cmd.Parameters.Clear();//修改 刘学铸 at 2017-07-13
  218. return rtn;
  219. }
  220. catch (SqlException sqlex)
  221. {
  222. st.Rollback();
  223. throw sqlex;
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }