Aucune description

DbHelperMySQL.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. 
  2. using MySql.Data.MySqlClient;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. namespace CallCenterApi.DB
  9. {
  10. public abstract class DbHelperMySQL
  11. {
  12. //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
  13. public static string connectionString = ConfigurationManager.ConnectionStrings["MySqlconnection"].ConnectionString;
  14. public DbHelperMySQL() { }
  15. #region 公用方法
  16. /// <summary>
  17. /// 得到最大值
  18. /// </summary>
  19. /// <param name="FieldName"></param>
  20. /// <param name="TableName"></param>
  21. /// <returns></returns>
  22. public static int GetMaxID(string FieldName, string TableName)
  23. {
  24. string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  25. object obj = GetSingle(strsql);
  26. if (obj == null)
  27. {
  28. return 1;
  29. }
  30. else
  31. {
  32. return int.Parse(obj.ToString());
  33. }
  34. }
  35. /// <summary>
  36. /// 是否存在
  37. /// </summary>
  38. /// <param name="strSql"></param>
  39. /// <returns></returns>
  40. public static bool Exists(string strSql)
  41. {
  42. object obj = GetSingle(strSql);
  43. int cmdresult;
  44. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  45. {
  46. cmdresult = 0;
  47. }
  48. else
  49. {
  50. cmdresult = int.Parse(obj.ToString());
  51. }
  52. if (cmdresult == 0)
  53. {
  54. return false;
  55. }
  56. else
  57. {
  58. return true;
  59. }
  60. }
  61. #endregion
  62. #region 执行简单SQL语句
  63. /// <summary>
  64. /// 执行SQL语句,返回影响的记录数
  65. /// </summary>
  66. /// <param name="SQLString">SQL语句</param>
  67. /// <returns>影响的记录数</returns>
  68. public static int ExecuteSql(string SQLString)
  69. {
  70. using (MySqlConnection connection = new MySqlConnection(connectionString))
  71. {
  72. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  73. {
  74. try
  75. {
  76. connection.Open();
  77. int rows = cmd.ExecuteNonQuery();
  78. return rows;
  79. }
  80. catch (MySql.Data.MySqlClient.MySqlException e)
  81. {
  82. connection.Close();
  83. throw e;
  84. }
  85. }
  86. }
  87. }
  88. /// <summary>
  89. /// 执行SQL语句,返回影响的记录数
  90. /// </summary>
  91. /// <param name="SQLString">SQL语句</param>
  92. /// <returns>影响的记录数</returns>
  93. public static int ExecuteSqlConn(string SQLString, string connectStr)
  94. {
  95. using (MySqlConnection connection = new MySqlConnection(connectStr))
  96. {
  97. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  98. {
  99. try
  100. {
  101. connection.Open();
  102. int rows = cmd.ExecuteNonQuery();
  103. return rows;
  104. }
  105. catch (MySql.Data.MySqlClient.MySqlException e)
  106. {
  107. connection.Close();
  108. throw e;
  109. }
  110. }
  111. }
  112. }
  113. public static int ExecuteSqlByTime(string SQLString, int Times)
  114. {
  115. using (MySqlConnection connection = new MySqlConnection(connectionString))
  116. {
  117. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  118. {
  119. try
  120. {
  121. connection.Open();
  122. cmd.CommandTimeout = Times;
  123. int rows = cmd.ExecuteNonQuery();
  124. return rows;
  125. }
  126. catch (MySql.Data.MySqlClient.MySqlException e)
  127. {
  128. connection.Close();
  129. throw e;
  130. }
  131. }
  132. }
  133. }
  134. /// <summary>
  135. /// 执行多条SQL语句,实现数据库事务。
  136. /// </summary>
  137. /// <param name="SQLStringList">多条SQL语句</param>
  138. public static int ExecuteSqlTran(List<String> SQLStringList)
  139. {
  140. using (MySqlConnection conn = new MySqlConnection(connectionString))
  141. {
  142. conn.Open();
  143. MySqlCommand cmd = new MySqlCommand();
  144. cmd.Connection = conn;
  145. MySqlTransaction tx = conn.BeginTransaction();
  146. cmd.Transaction = tx;
  147. try
  148. {
  149. int count = 0;
  150. for (int n = 0; n < SQLStringList.Count; n++)
  151. {
  152. string strsql = SQLStringList[n];
  153. if (strsql.Trim().Length > 1)
  154. {
  155. cmd.CommandText = strsql;
  156. count += cmd.ExecuteNonQuery();
  157. }
  158. }
  159. tx.Commit();
  160. return count;
  161. }
  162. catch (Exception ex)
  163. {
  164. tx.Rollback();
  165. throw ex;
  166. return 0;
  167. }
  168. }
  169. }
  170. /// <summary>
  171. /// 执行带一个存储过程参数的的SQL语句。
  172. /// </summary>
  173. /// <param name="SQLString">SQL语句</param>
  174. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  175. /// <returns>影响的记录数</returns>
  176. public static int ExecuteSql(string SQLString, string content)
  177. {
  178. using (MySqlConnection connection = new MySqlConnection(connectionString))
  179. {
  180. MySqlCommand cmd = new MySqlCommand(SQLString, connection);
  181. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
  182. myParameter.Value = content;
  183. cmd.Parameters.Add(myParameter);
  184. try
  185. {
  186. connection.Open();
  187. int rows = cmd.ExecuteNonQuery();
  188. return rows;
  189. }
  190. catch (MySql.Data.MySqlClient.MySqlException e)
  191. {
  192. throw e;
  193. }
  194. finally
  195. {
  196. cmd.Dispose();
  197. connection.Close();
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// 执行带一个存储过程参数的的SQL语句。
  203. /// </summary>
  204. /// <param name="SQLString">SQL语句</param>
  205. /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  206. /// <returns>影响的记录数</returns>
  207. public static object ExecuteSqlGet(string SQLString, string content)
  208. {
  209. using (MySqlConnection connection = new MySqlConnection(connectionString))
  210. {
  211. MySqlCommand cmd = new MySqlCommand(SQLString, connection);
  212. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText);
  213. myParameter.Value = content;
  214. cmd.Parameters.Add(myParameter);
  215. try
  216. {
  217. connection.Open();
  218. object obj = cmd.ExecuteScalar();
  219. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  220. {
  221. return null;
  222. }
  223. else
  224. {
  225. return obj;
  226. }
  227. }
  228. catch (MySql.Data.MySqlClient.MySqlException e)
  229. {
  230. throw e;
  231. }
  232. finally
  233. {
  234. cmd.Dispose();
  235. connection.Close();
  236. }
  237. }
  238. }
  239. /// <summary>
  240. /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  241. /// </summary>
  242. /// <param name="strSQL">SQL语句</param>
  243. /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  244. /// <returns>影响的记录数</returns>
  245. public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  246. {
  247. using (MySqlConnection connection = new MySqlConnection(connectionString))
  248. {
  249. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  250. MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@fs", SqlDbType.Image);
  251. myParameter.Value = fs;
  252. cmd.Parameters.Add(myParameter);
  253. try
  254. {
  255. connection.Open();
  256. int rows = cmd.ExecuteNonQuery();
  257. return rows;
  258. }
  259. catch (MySql.Data.MySqlClient.MySqlException e)
  260. {
  261. throw e;
  262. }
  263. finally
  264. {
  265. cmd.Dispose();
  266. connection.Close();
  267. }
  268. }
  269. }
  270. /// <summary>
  271. /// 执行一条计算查询结果语句,返回查询结果(object)。
  272. /// </summary>
  273. /// <param name="SQLString">计算查询结果语句</param>
  274. /// <returns>查询结果(object)</returns>
  275. public static object GetSingle(string SQLString)
  276. {
  277. using (MySqlConnection connection = new MySqlConnection(connectionString))
  278. {
  279. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  280. {
  281. try
  282. {
  283. connection.Open();
  284. object obj = cmd.ExecuteScalar();
  285. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  286. {
  287. return null;
  288. }
  289. else
  290. {
  291. return obj;
  292. }
  293. }
  294. catch (MySql.Data.MySqlClient.MySqlException e)
  295. {
  296. connection.Close();
  297. throw e;
  298. }
  299. }
  300. }
  301. }
  302. public static object GetSingle(string SQLString, int Times)
  303. {
  304. using (MySqlConnection connection = new MySqlConnection(connectionString))
  305. {
  306. using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
  307. {
  308. try
  309. {
  310. connection.Open();
  311. cmd.CommandTimeout = Times;
  312. object obj = cmd.ExecuteScalar();
  313. if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  314. {
  315. return null;
  316. }
  317. else
  318. {
  319. return obj;
  320. }
  321. }
  322. catch (MySql.Data.MySqlClient.MySqlException e)
  323. {
  324. connection.Close();
  325. throw e;
  326. }
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
  332. /// </summary>
  333. /// <param name="strSQL">查询语句</param>
  334. /// <returns>MySqlDataReader</returns>
  335. public static MySqlDataReader ExecuteReader(string strSQL)
  336. {
  337. MySqlConnection connection = new MySqlConnection(connectionString);
  338. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  339. try
  340. {
  341. connection.Open();
  342. MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  343. return myReader;
  344. }
  345. catch (MySql.Data.MySqlClient.MySqlException e)
  346. {
  347. throw e;
  348. }
  349. }
  350. /// <summary>
  351. /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close )
  352. /// </summary>
  353. /// <param name="strSQL">查询语句</param>
  354. /// <returns>MySqlDataReader</returns>
  355. public static MySqlDataReader ExecuteReader(string strSQL, string connectionStr)
  356. {
  357. MySqlConnection connection = new MySqlConnection(connectionStr);
  358. MySqlCommand cmd = new MySqlCommand(strSQL, connection);
  359. try
  360. {
  361. connection.Open();
  362. MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  363. return myReader;
  364. }
  365. catch (MySql.Data.MySqlClient.MySqlException e)
  366. {
  367. throw e;
  368. }
  369. }
  370. /// <summary>
  371. /// 执行查询语句,返回DataSet
  372. /// </summary>
  373. /// <param name="SQLString">查询语句</param>
  374. /// <returns>DataSet</returns>
  375. public static DataSet Query(string SQLString)
  376. {
  377. using (MySqlConnection connection = new MySqlConnection(connectionString))
  378. {
  379. DataSet ds = new DataSet();
  380. try
  381. {
  382. connection.Open();
  383. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  384. command.Fill(ds, "ds");
  385. }
  386. catch (MySql.Data.MySqlClient.MySqlException ex)
  387. {
  388. throw new Exception(ex.Message);
  389. }
  390. return ds;
  391. }
  392. }
  393. /// <summary>
  394. /// 执行查询语句,返回DataSet
  395. /// </summary>
  396. /// <param name="SQLString">查询语句</param>
  397. /// <returns>DataSet</returns>
  398. public static DataSet QueryConn(string SQLString, string conStr)
  399. {
  400. using (MySqlConnection connection = new MySqlConnection(conStr))
  401. {
  402. DataSet ds = new DataSet();
  403. try
  404. {
  405. connection.Open();
  406. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  407. command.Fill(ds, "ds");
  408. }
  409. catch (MySql.Data.MySqlClient.MySqlException ex)
  410. {
  411. throw new Exception(ex.Message);
  412. }
  413. finally
  414. {
  415. connection.Close();
  416. }
  417. return ds;
  418. }
  419. }
  420. public static DataSet Query(string SQLString, int Times)
  421. {
  422. using (MySqlConnection connection = new MySqlConnection(connectionString))
  423. {
  424. DataSet ds = new DataSet();
  425. try
  426. {
  427. connection.Open();
  428. MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
  429. command.SelectCommand.CommandTimeout = Times;
  430. command.Fill(ds, "ds");
  431. }
  432. catch (MySql.Data.MySqlClient.MySqlException ex)
  433. {
  434. throw new Exception(ex.Message);
  435. }
  436. return ds;
  437. }
  438. }
  439. #endregion
  440. }
  441. }