1、 1. using System; 2. using System.Collections.Generic; 3. using System.Linq; 4. using System.Text; 5. using System.Data.SqlClient; 6. using System.Collections; 7. using System.Data; 8. using System.Configuration; 9. using System.Web; 10. 11. 12. public sealed cl
2、ass SqlHelper 13. { 14. 15. public static string connectionString = ConfigurationManager.ConnectionStrings["db_JXCconn"].ConnectionString; 16. 17. public SqlHelper() 18. { 19. } 20. 21. #region 公用方法 22. public static int GetMaxID(string FieldNa
3、me, string TableName) 23. { 24. string strsql = "select max(" + FieldName + ")+1 from " + TableName; 25. object obj = SqlHelper.GetSingle(strsql); 26. if (obj == null) 27. { 28. return 1; 29. } 30. else 31.
4、 { 32. return int.Parse(obj.ToString()); 33. } 34. } 35. 36. public static bool Exists(string strSql) 37. { 38. object obj = SqlHelper.GetSingle(strSql); 39. int cmdresult; 40. if ((Object.Equals(obj, null)) ||
5、Object.Equals(obj, System.DBNull.Value))) 41. { 42. cmdresult = 0; 43. } 44. else 45. { 46. cmdresult = int.Parse(obj.ToString()); 47. } 48. if (cmdresult == 0) 49. { 50. return
6、 false; 51. } 52. else 53. { 54. return true; 55. } 56. } 57. 58. public static bool Exists(string strSql, params SqlParameter[] cmdParms) 59. { 60. object obj = SqlHelper.GetSingle(strSql, cmdParms); 6
7、1. int cmdresult; 62. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) 63. { 64. cmdresult = 0; 65. } 66. else 67. { 68. cmdresult = int.Parse(obj.ToString()); 69. }
8、
70. if (cmdresult == 0)
71. {
72. return false;
73. }
74. else
75. {
76. return true;
77. }
78. }
79. #endregion
80.
81. #region 执行简单SQL语句
82. ///
9、SQL语句,返回影响的记录数
84. ///
85. /// SQL语句
86. ///
10、 { 91. using (SqlCommand cmd = new SqlCommand(SQLString, connection)) 92. { 93. try 94. { 95. connection.Open(); 96. int rows = cmd.ExecuteNonQuery(); 97. re
11、turn rows; 98. } 99. catch (System.Data.SqlClient.SqlException E) 100. { 101. connection.Close(); 102. throw new Exception(E.Message); 103. } 104. } 105.
12、 }
106. }
107.
108. ///
13、5. using (SqlConnection connection = new SqlConnection(connectionString)) 116. { 117. using (SqlCommand cmd = new SqlCommand(SQLString, connection)) 118. { 119. try 120. { 121. connection.Op
14、en(); 122. int rows = Convert.ToInt32(cmd.ExecuteScalar()); 123. return rows; 124. } 125. catch (System.Data.SqlClient.SqlException E) 126. { 127. connection.Close(); 128.
15、 throw new Exception(E.Message);
129. }
130. }
131. }
132. }
133.
134. ///
16、8. public static void ExecuteSqlTran(ArrayList SQLStringList) 139. { 140. using (SqlConnection conn = new SqlConnection(connectionString)) 141. { 142. conn.Open(); 143. SqlCommand cmd = new SqlCommand(); 144. cmd.Connectio
17、n = conn; 145. SqlTransaction tx = conn.BeginTransaction(); 146. cmd.Transaction = tx; 147. try 148. { 149. for (int n = 0; n < SQLStringList.Count; n++) 150. { 151. string strsql
18、 = SQLStringList[n].ToString(); 152. if (strsql.Trim().Length > 1) 153. { 154. cmd.CommandText = strsql; 155. cmd.ExecuteNonQuery(); 156. } 157. } 158.
19、 tx.Commit(); 159. } 160. catch (System.Data.SqlClient.SqlException E) 161. { 162. tx.Rollback(); 163. throw new Exception(E.Message); 164. } 165. } 166. } 167. 168.
20、 ///
21、l(string SQLString, string content) 175. { 176. using (SqlConnection connection = new SqlConnection(connectionString)) 177. { 178. SqlCommand cmd = new SqlCommand(SQLString, connection); 179. System.Data.SqlClient.SqlParameter myParameter =
22、new System.Data.SqlClient.SqlParameter("@content", SqlDbType.VarChar); 180. 181. myParameter.Value = content; 182. cmd.Parameters.Add(myParameter); 183. try 184. { 185. connection.Open(); 186. int r
23、ows = cmd.ExecuteNonQuery(); 187. return rows; 188. } 189. catch (System.Data.SqlClient.SqlException E) 190. { 191. throw new Exception(E.Message); 192. } 193. finally 194.
24、 {
195. cmd.Dispose();
196. connection.Close();
197. }
198. }
199. }
200.
201. ///
25、
205. /// 图像字节,数据库的字段类型为image的情况
206. ///
26、 211. SqlCommand cmd = new SqlCommand(strSQL, connection); 212. System.Data.SqlClient.SqlParameter myParameter = new System.Data.SqlClient.SqlParameter("@fs", SqlDbType.Binary); 213. myParameter.Value = fs; 214. cmd.Parameters.Add(myParamete
27、r); 215. try 216. { 217. connection.Open(); 218. int rows = cmd.ExecuteNonQuery(); 219. return rows; 220. } 221. catch (System.Data.SqlClient.SqlException E) 222. {
28、223. throw new Exception(E.Message);
224. }
225. finally
226. {
227. cmd.Dispose();
228. connection.Close();
229. }
230. }
231. }
232.
233. ///
29、234. /// 执行一条计算查询结果语句,返回查询结果(object)。
235. ///
236. /// 计算查询结果语句
237. ///
30、ew SqlConnection(connectionString)) 241. { 242. using (SqlCommand cmd = new SqlCommand(SQLString, connection)) 243. { 244. try 245. { 246. connection.Open(); 247. object obj =
31、 cmd.ExecuteScalar(); 248. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) 249. { 250. return null; 251. } 252. else 253. { 25
32、4. return obj; 255. } 256. } 257. catch (System.Data.SqlClient.SqlException e) 258. { 259. connection.Close(); 260. throw new Exception(e.Message);
33、
261. }
262. }
263. }
264. }
265.
266. ///
34、tic SqlDataReader ExecuteReader(string strSQL) 272. { 273. SqlConnection connection = new SqlConnection(connectionString); 274. SqlCommand cmd = new SqlCommand(strSQL, connection); 275. try 276. { 277. connection.Open(); 278.
35、 SqlDataReader myReader = cmd.ExecuteReader(); 279. return myReader; 280. } 281. catch (System.Data.SqlClient.SqlException e) 282. { 283. throw new Exception(e.Message); 284. } 285. 286. } 287. 288.
36、 ///
37、Connection(connectionString)) 296. { 297. DataSet ds = new DataSet(); 298. try 299. { 300. connection.Open(); 301. SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); 302. 303.
38、 command.Fill(ds, "ds"); 304. } 305. catch (System.Data.SqlClient.SqlException ex) 306. { 307. throw new Exception(ex.Message); 308. } 309. return ds; 310. } 311. } 312. 31
39、3. ///
40、ction = new SqlConnection(connectionString)) 321. { 322. DataSet ds = new DataSet(); 323. try 324. { 325. connection.Open(); 326. SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); 327.
41、 command.Fill(ds, "ds"); 328. } 329. catch (System.Data.SqlClient.SqlException ex) 330. { 331. throw new Exception(ex.Message); 332. } 333. return ds.Tables[0]; 334. } 335.
42、}
336. #endregion
337.
338. #region 执行带参数的SQL语句
339. ///
43、ring, params SqlParameter[] cmdParms) 345. { 346. using (SqlConnection connection = new SqlConnection(connectionString)) 347. { 348. using (SqlCommand cmd = new SqlCommand()) 349. { 350. try 351. { 35
44、2. PrepareCommand(cmd, connection, null, SQLString, cmdParms); 353. int rows = cmd.ExecuteNonQuery(); 354. cmd.Parameters.Clear(); 355. return rows; 356. } 357. catch (Syste
45、m.Data.SqlClient.SqlException E)
358. {
359. throw new Exception(E.Message);
360. }
361. }
362. }
363. }
364.
365. ///
46、68. /// SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[]) 369. public static void ExecuteSqlTran(Hashtable SQLStringList) 370. { 371. using (SqlConnection conn = new SqlConnection(connectionString)) 372. { 373. co
47、nn.Open(); 374. using (SqlTransaction trans = conn.BeginTransaction()) 375. { 376. SqlCommand cmd = new SqlCommand(); 377. try 378. { 379. //循环 380. foreach (Dictionar
48、yEntry myDE in SQLStringList) 381. { 382. string cmdText = myDE.Key.ToString(); 383. SqlParameter[] cmdParms = (SqlParameter[])myDE.Value; 384. PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
49、 385. int val = cmd.ExecuteNonQuery(); 386. cmd.Parameters.Clear(); 387. 388. trans.Commit(); 389. } 390. } 391. catch 392. { 393.
50、 trans.Rollback();
394. throw;
395. }
396. }
397. }
398. }
399.
400. ///






