Sin descripción

DbHelperMySQL.cs 16KB

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