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