(06)通用的数据访问 SqlHelper
2008-06-24 14:48 通用C#系统架构 阅读(1628) 评论(3) 编辑 收藏 举报出现错误的地方删除掉,能编译通过,就可以用了
Code
1//------------------------------------------------------------
2// All Rights Reserved , Copyright (C) 2008 , Jirisoft , Ltd.
3//------------------------------------------------------------
4
5using System;
6using System.IO;
7using System.Text;
8using System.Globalization;
9using System.Data;
10using System.Data.Common;
11using System.Data.SqlClient;
12using System.Reflection;
13using System.Diagnostics;
14
15namespace Jirisoft.Common.DbUtilities
16{
17 using Jirisoft.Common;
18 using Jirisoft.Common.Utilities;
19
20 /**//// <summary>
21 /// SqlHelper
22 /// 有关数据库连接的方法。
23 ///
24 /// 修改纪录
25 ///
26 /// 2008.06.01 版本:1.1 JiRiGaLa 数据库连接获得方式进行改进,构造函数获得调通。
27 /// 2008.05.07 版本:1.0 JiRiGaLa 创建。
28 ///
29 /// 版本:1.0
30 ///
31 /// <author>
32 /// <name>JiRiGaLa</name>
33 /// <date>2008.05.07</date>
34 /// </author>
35 /// </summary>
36 public class SqlHelper : IDbHelper, IDisposable
37 {
38 public SqlConnection mySqlConnection = new SqlConnection();
39 public SqlCommand mySqlCommand = new SqlCommand();
40 public SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
41 public SqlTransaction mySqlTransaction;
42 private bool inTransaction; // 是否已在事务之中
43
44 public String FileName = "SqlHelper.txt"; // SQL查询语句日志
45
46
47 public SqlHelper()#region public SqlHelper()
48 /**//// <summary>
49 /// 构造方法
50 /// </summary>
51 public SqlHelper()
52 {
53 }
54 #endregion
55
56 public SqlHelper(String connectionString)#region public SqlHelper(String connectionString)
57 /**//// <summary>
58 /// 设定软件名称
59 /// </summary>
60 /// <param name="connectionString">数据连接</param>
61 public SqlHelper(String connectionString)
62 {
63 this.mySqlConnection.ConnectionString = connectionString;
64 }
65 #endregion
66
67 public String GetDBNow()#region public String GetDBNow()
68 /**//// <summary>
69 /// 获得数据库日期时间
70 /// </summary>
71 /// <returns>日期时间</returns>
72 public String GetDBNow()
73 {
74 String returnValue = " Getdate() ";
75 switch (BaseConfiguration.Instance.DataBaseType)
76 {
77 case DataBaseType.Access:
78 returnValue = "'" + DateTime.Now.ToString() + "'";
79 break;
80 case DataBaseType.Sqlserver:
81 returnValue = " GetDate() ";
82 break;
83 case DataBaseType.Oracle:
84 returnValue = " SYSDATE ";
85 break;
86 }
87 return returnValue;
88 }
89 #endregion
90
91 public DateTime GetDBDateTime() 获得数据库日期时间#region public DateTime GetDBDateTime() 获得数据库日期时间
92 /**//// <summary>
93 /// 获得数据库日期时间
94 /// </summary>
95 /// <returns>日期时间</returns>
96 public DateTime GetDBDateTime()
97 {
98 String sqlQuery = this.GetDBNow();
99 Object myObject = this.ExecuteScalar(sqlQuery);
100 return DateTime.Parse(myObject.ToString());
101 }
102 #endregion
103
104 public bool InTransaction 是否已采用事务#region public bool InTransaction 是否已采用事务
105 /**//// <summary>
106 /// 是否已采用事务
107 /// </summary>
108 /// <returns>采用事务</returns>
109 public bool InTransaction
110 {
111 get
112 {
113 return this.inTransaction;
114 }
115 set
116 {
117 this.inTransaction = value;
118 }
119 }
120 #endregion
121
122 public IDbConnection GetDbConnection() 获取数据库连接#region public IDbConnection GetDbConnection() 获取数据库连接
123 /**//// <summary>
124 /// 获取数据库连接
125 /// </summary>
126 /// <returns>数据库连接</returns>
127 public IDbConnection GetDbConnection()
128 {
129 return this.mySqlConnection;
130 }
131 #endregion
132
133 public IDbTransaction GetDbTransaction() 获取数据源上执行的事务#region public IDbTransaction GetDbTransaction() 获取数据源上执行的事务
134 /**//// <summary>
135 /// 获取数据源上执行的事务
136 /// </summary>
137 /// <returns>数据源上执行的事务</returns>
138 public IDbTransaction GetDbTransaction()
139 {
140 return this.mySqlTransaction;
141 }
142 #endregion
143
144 public IDbCommand GetDbCommand() 获取数据源上命令#region public IDbCommand GetDbCommand() 获取数据源上命令
145 /**//// <summary>
146 /// 获取数据源上命令
147 /// </summary>
148 /// <returns>数据源上命令</returns>
149 public IDbCommand GetDbCommand()
150 {
151 return this.mySqlCommand;
152 }
153 #endregion
154
155
156 public IDbConnection Open()#region public IDbConnection Open()
157 /**//// <summary>
158 /// 这时主要的获取数据库连接的方法
159 /// </summary>
160 /// <returns>数据库连接</returns>
161 public IDbConnection Open()
162 {
163 #if (DEBUG)
164 int milliStart = Environment.TickCount;
165 #endif
166 if (String.IsNullOrEmpty(this.mySqlConnection.ConnectionString))
167 {
168 // 这里是获取一个连接的详细方法
169 if (BaseConfiguration.Instance.ConnectionString.Length == 0)
170 {
171 BaseConfiguration.Instance.GetSetting();
172 }
173 this.Open(BaseConfiguration.Instance.SqlConnection);
174 }
175 // 写入调试信息
176 #if (DEBUG)
177 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
178 #endif
179 return this.mySqlConnection;
180 }
181 #endregion
182
183 public IDbConnection Open(String connectionString)#region public IDbConnection Open(String connectionString)
184 /**//// <summary>
185 /// 获得新的数据库连接
186 /// </summary>
187 /// <param name="connectionString">数据库连接字符串</param>
188 /// <returns>数据库连接</returns>
189 public IDbConnection Open(String connectionString)
190 {
191 // 写入调试信息
192 #if (DEBUG)
193 int milliStart = Environment.TickCount;
194 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
195 #endif
196 // 这里数据库连接打开的时候,就判断注册属性的有效性
197 if (!BaseCodeChange.Instance.CheckRegister())
198 {
199 // 若没有进行注册,让程序无法打开数据库比较好。
200 this.mySqlConnection.ConnectionString = String.Empty;
201 // 抛出异常信息显示给客户
202 throw new Exception(BaseConfiguration.Instance.ExceptionInfo);
203 }
204 if (this.mySqlConnection.State == ConnectionState.Closed)
205 {
206 this.mySqlConnection.ConnectionString = connectionString;
207 this.mySqlConnection.Open();
208 // 创建对象
209 this.mySqlCommand = new SqlCommand(String.Empty, this.mySqlConnection);
210 this.mySqlDataAdapter = new SqlDataAdapter(String.Empty, this.mySqlConnection);
211 // 写入调试信息
212 #if (DEBUG)
213 int milliEnd = Environment.TickCount;
214 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
215 #endif
216 }
217 return this.mySqlConnection;
218 }
219 #endregion
220
221
222 public bool TestConn(String dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)#region public bool TestConn(String dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)
223 /**//// <summary>
224 /// 测试数据库连接是否成功,这里抛出异常进行加强,充分使用系统的异常功能。
225 /// </summary>
226 /// <param name="dataBaseType">数据库类别</param>
227 /// <param name="dataBase">数据库名称</param>
228 /// <param name="userName">用户名</param>
229 /// <param name="password">密码</param>
230 /// <param name="workstation">服务器名称</param>
231 /// <param name="trustLink">是否信任的连接</param>
232 /// <returns>是否连接成功</returns>
233 public bool TestConn(DataBaseType dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)
234 {
235 // 写入调试信息
236 #if (DEBUG)
237 int milliStart = Environment.TickCount;
238 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
239 #endif
240 bool returnValue = false; // 连接是否成功
241 String connectionString = String.Empty; // 连接字符传
242 SqlConnection DbConnection = null; // 数据库连接
243 connectionString = BaseConfiguration.Instance.GetOleDbConnection(dataBaseType, dataBase, userName, password, workstation, trustLink);
244 DbConnection = (SqlConnection)this.Open(connectionString);
245 try
246 {
247 if (DbConnection.State == ConnectionState.Closed)
248 {
249 DbConnection.Open();
250 }
251 DbConnection.Close();
252 returnValue = true;
253 }
254 catch (SqlException mySqlException)
255 {
256 DbConnection = null;
257 throw mySqlException;
258 }
259 // 写入调试信息
260 #if (DEBUG)
261 int milliEnd = Environment.TickCount;
262 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
263 #endif
264 return returnValue;
265 }
266 #endregion
267
268 public String SqlSafe(String value) 检查参数的安全性#region public String SqlSafe(String value) 检查参数的安全性
269 /**//// <summary>
270 /// 检查参数的安全性
271 /// </summary>
272 /// <param name="value">参数</param>
273 /// <returns>安全的参数</returns>
274 public String SqlSafe(String value)
275 {
276 value = value.Replace("'", "''");
277 // value = value.Replace("%", "'%");
278 return value;
279 }
280 #endregion
281
282 public DbParameter[] GetParameters(String[] targetFileds, Object[] targetValues)#region public DbParameter[] GetParameters(String[] targetFileds, Object[] targetValues)
283 /**//// <summary>
284 /// 获取参数
285 /// </summary>
286 /// <param name="targetFiled">目标字段</param>
287 /// <param name="targetValue">值</param>
288 /// <returns>参数集</returns>
289 public DbParameter[] GetParameters(String[] targetFileds, Object[] myTargetValues)
290 {
291 DbParameter[] myDbParameters = new DbParameter[0];
292 if (targetFileds != null && myTargetValues != null)
293 {
294 myDbParameters = new DbParameter[targetFileds.Length];
295 for (int i = 0; i < targetFileds.Length; i++)
296 {
297 myDbParameters[i] = this.GetParameter(targetFileds[i], myTargetValues[i]);
298 }
299 }
300 return myDbParameters;
301 }
302 #endregion
303
304 public DbParameter GetParameter(String targetFiled, Object targetValue)#region public DbParameter GetParameter(String targetFiled, Object targetValue)
305 /**//// <summary>
306 /// 获取参数
307 /// </summary>
308 /// <param name="targetFiled">目标字段</param>
309 /// <param name="targetValue">值</param>
310 /// <returns>参数</returns>
311 public DbParameter GetParameter(String targetFiled, Object myTargetValue)
312 {
313 SqlParameter myDbParameter = null;
314 if (myTargetValue is String)
315 {
316 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
317 }
318 else
319 {
320 if (myTargetValue is Boolean)
321 {
322 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Bit);
323 }
324 else
325 {
326 if (myTargetValue is DateTime)
327 {
328 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.DateTime);
329 }
330 else
331 {
332 if (myTargetValue is int)
333 {
334 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Int);
335 }
336 else
337 {
338 if (myTargetValue is Int64)
339 {
340 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.BigInt);
341 }
342 else
343 {
344 if (myTargetValue is Double)
345 {
346 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Decimal);
347 }
348 else
349 {
350 if (myTargetValue is Byte[])
351 {
352 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Binary);
353 }
354 else
355 {
356 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
357 }
358 }
359 }
360 }
361 }
362 }
363 }
364
365 myDbParameter.Value = myTargetValue;
366 return myDbParameter;
367 }
368 #endregion
369
370 public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)#region public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)
371 /**//// <summary>
372 /// 添加参数
373 /// </summary>
374 /// <param name="myTargetDbCommand">数据库命令</param>
375 /// <param name="targetFiled">目标字段</param>
376 /// <param name="targetValue">值</param>
377 public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)
378 {
379 ((SqlCommand)myTargetDbCommand).Parameters.Add(this.GetParameter(targetFiled, myTargetValue));
380 }
381 #endregion
382
383 public String GetWhereString(String[] names, ref Object[] values, String relation) 获得条件语句#region public String GetWhereString(String[] names, ref Object[] values, String relation) 获得条件语句
384 /**//// <summary>
385 /// 获得条件语句
386 /// </summary>
387 /// <param name="names">字段名</param>
388 /// <param name="values">字段值</param>
389 /// <param name="relation">逻辑关系</param>
390 /// <returns>字符串</returns>
391 public String GetWhereString(String[] names, ref Object[] values, String relation)
392 {
393 String returnValue = String.Empty;
394 String subSqlQuery = String.Empty;
395 for (int i = 0; i < names.Length; i++)
396 {
397 if ((names[i] != null) && (names[i].Length > 0))
398 {
399 if (values[i] == null || String.IsNullOrEmpty(values[i].ToString()))
400 {
401 subSqlQuery = " (" + names[i] + " IS NULL) ";
402 }
403 else
404 {
405 // 这里操作,就会有些重复了,不应该进行处理
406 // values[i] = this.SqlSafe(values[i].ToString());
407 subSqlQuery = " (" + names[i] + " = " + this.GetParameter(names[i]) + ") ";
408
409 if ((values[i].ToString().IndexOf('[') >= 0) || (values[i].ToString().IndexOf(']') >= 0))
410 {
411 values[i] = values[i].ToString().Replace("[", "/[");
412 values[i] = values[i].ToString().Replace("]", "/]");
413 values[i] = this.SqlSafe(values[i].ToString());
414 subSqlQuery = " (" + names[i] + " LIKE '" + values[i] + "' ESCAPE '/') ";
415 values[i] = null;
416 // subSqlQuery = " (" + names[i] + " LIKE ? ESCAPE '/') ";
417 }
418 }
419 returnValue += subSqlQuery + relation;
420 }
421 }
422 if (returnValue.Length > 0)
423 {
424 returnValue = returnValue.Substring(0, returnValue.Length - relation.Length - 1);
425 }
426 return returnValue;
427 }
428 #endregion
429
430 String GetParameter(String parameter) 获得参数Sql表达式#region String GetParameter(String parameter) 获得参数Sql表达式
431 /**//// <summary>
432 /// 获得参数Sql表达式
433 /// </summary>
434 /// <param name="parameter">参数名称</param>
435 /// <returns>字符串</returns>
436 public String GetParameter(String parameter)
437 {
438 return " @" + parameter + " ";
439 }
440 #endregion
441
442 public String PlusSign() 字符串相加符号#region public String PlusSign() 字符串相加符号
443 /**//// <summary>
444 /// 字符串相加符号
445 /// </summary>
446 /// <returns>字符加</returns>
447 public String PlusSign()
448 {
449 return " + ";
450 }
451 #endregion
452
453
454 public IDataReader ExecuteReader(String sqlQuery)#region public IDataReader ExecuteReader(String sqlQuery)
455 /**//// <summary>
456 /// 执行查询
457 /// </summary>
458 /// <param name="sqlQuery">sql查询</param>
459 /// <returns>结果集流</returns>
460 public IDataReader ExecuteReader(String sqlQuery)
461 {
462 // 写入调试信息
463 #if (DEBUG)
464 int milliStart = Environment.TickCount;
465 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
466 #endif
467 this.mySqlCommand.CommandType = CommandType.Text;
468 this.mySqlCommand.CommandText = sqlQuery;
469 if (this.InTransaction)
470 {
471 this.mySqlCommand.Transaction = this.mySqlTransaction;
472 }
473 SqlDataReader mySqlDataReader = this.mySqlCommand.ExecuteReader();
474 // 写入调试信息
475 #if (DEBUG)
476 int milliEnd = Environment.TickCount;
477 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
478 #endif
479 // 写入日志
480 this.WriteLog(sqlQuery);
481 return mySqlDataReader;
482 }
483 #endregion
484
485 public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters);#region public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters);
486 /**//// <summary>
487 /// 执行查询
488 /// </summary>
489 /// <param name="sqlQuery">sql查询</param>
490 /// <param name="myDbParameterCollection">参数集</param>
491 /// <returns>结果集流</returns>
492 public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters)
493 {
494 return this.ExecuteReader(CommandType.Text, sqlQuery, myDbParameters);
495 }
496 #endregion
497
498 public IDataReader ExecuteReader(String sqlQuery, String name, Object value)#region public IDataReader ExecuteReader(String sqlQuery, String name, Object value)
499 /**//// <summary>
500 /// 执行查询
501 /// </summary>
502 /// <param name="myDataSet">数据集</param>
503 /// <param name="sqlQuery">sql查询</param>
504 /// <param name="name">参数名</param>
505 /// <param name="value">参数值</param>
506 /// <returns>结果集流</returns>
507 public IDataReader ExecuteReader(String sqlQuery, String name, Object value)
508 {
509 String[] names = new String[1] { name };
510 Object[] values = new Object[1] { value };
511 return this.ExecuteReader(CommandType.Text, sqlQuery, names, values);
512 }
513 #endregion
514
515 public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)#region public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)
516 /**//// <summary>
517 /// 执行查询
518 /// </summary>
519 /// <param name="sqlQuery">sql查询</param>
520 /// <param name="names">参数名</param>
521 /// <param name="values">参数值</param>
522 /// <returns>结果集流</returns>
523 public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)
524 {
525 return this.ExecuteReader(CommandType.Text, sqlQuery, names, values);
526 }
527 #endregion
528
529 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
530 /**//// <summary>
531 /// 执行查询
532 /// </summary>
533 /// <param name="myCommandType">命令分类</param>
534 /// <param name="sqlQuery">sql查询</param>
535 /// <param name="myDbParameterCollection">参数集</param>
536 /// <returns>结果集流</returns>
537 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
538 {
539 // 写入调试信息
540 #if (DEBUG)
541 int milliStart = Environment.TickCount;
542 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
543 #endif
544 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
545 this.mySqlCommand.CommandType = myCommandType;
546 if (mySqlTransaction != null)
547 {
548 this.mySqlCommand.Transaction = mySqlTransaction;
549 }
550 if (myDbParameters != null)
551 {
552 this.mySqlCommand.Parameters.Clear();
553 for (int i = 0; i < myDbParameters.Length; i++)
554 {
555 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
556 }
557 }
558 SqlDataReader mySqlDataReader = this.mySqlCommand.ExecuteReader();
559 this.mySqlCommand.Parameters.Clear();
560 // 写入调试信息
561 #if (DEBUG)
562 int milliEnd = Environment.TickCount;
563 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
564 #endif
565 // 写入日志
566 this.WriteLog(sqlQuery);
567 return mySqlDataReader;
568 }
569 #endregion
570
571 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
572 /**//// <summary>
573 /// 执行查询
574 /// </summary>
575 /// <param name="myCommandType">命令分类</param>
576 /// <param name="sqlQuery">sql查询</param>
577 /// <param name="names">参数名</param>
578 /// <param name="values">参数值</param>
579 /// <returns>结果集流</returns>
580 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
581 {
582 return this.ExecuteReader(myCommandType, sqlQuery, this.GetParameters(names, values)); ;
583 }
584 #endregion
585
586
587 public int ExecuteNonQuery(String sqlQuery)#region public int ExecuteNonQuery(String sqlQuery)
588 /**//// <summary>
589 /// 执行查询, SQL BUILDER 用了这个东西?参数需要保存, 不能丢失.
590 /// </summary>
591 /// <param name="sqlQuery">sql查询</param>
592 /// <returns>影响行数</returns>
593 public int ExecuteNonQuery(String sqlQuery)
594 {
595 // 写入调试信息
596 #if (DEBUG)
597 int milliStart = Environment.TickCount;
598 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
599 #endif
600 this.mySqlCommand.CommandType = CommandType.Text;
601 this.mySqlCommand.CommandText = sqlQuery;
602 if (this.InTransaction)
603 {
604 this.mySqlCommand.Transaction = this.mySqlTransaction;
605 }
606 int returnValue = this.mySqlCommand.ExecuteNonQuery();
607 // 写入调试信息
608 #if (DEBUG)
609 int milliEnd = Environment.TickCount;
610 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
611 #endif
612 // 写入日志
613 this.WriteLog(sqlQuery);
614 return returnValue;
615 }
616 #endregion
617
618 public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters);#region public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters);
619 /**//// <summary>
620 /// 执行查询
621 /// </summary>
622 /// <param name="sqlQuery">sql查询</param>
623 /// <param name="myDbParameterCollection">参数集</param>
624 /// <returns>影响行数</returns>
625 public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters)
626 {
627 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, myDbParameters);
628 }
629 #endregion
630
631 public int ExecuteNonQuery(String sqlQuery, String name, Object value)#region public int ExecuteNonQuery(String sqlQuery, String name, Object value)
632 /**//// <summary>
633 /// 执行查询
634 /// </summary>
635 /// <param name="myDataSet">数据集</param>
636 /// <param name="sqlQuery">sql查询</param>
637 /// <param name="name">参数名</param>
638 /// <param name="value">参数值</param>
639 /// <returns>影响行数</returns>
640 public int ExecuteNonQuery(String sqlQuery, String name, Object value)
641 {
642 String[] names = new String[1] { name };
643 Object[] values = new Object[1] { value };
644 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, names, values);
645 }
646 #endregion
647
648 public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)
649 /**//// <summary>
650 /// 执行查询
651 /// </summary>
652 /// <param name="sqlQuery">sql查询</param>
653 /// <param name="names">参数名</param>
654 /// <param name="values">参数值</param>
655 /// <returns>影响行数</returns>
656 public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)
657 {
658 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, names, values);
659 }
660 #endregion
661
662 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
663 /**//// <summary>
664 /// 执行查询
665 /// </summary>
666 /// <param name="myCommandType">命令分类</param>
667 /// <param name="sqlQuery">sql查询</param>
668 /// <param name="myDbParameterCollection">参数集</param>
669 /// <returns>影响行数</returns>
670 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
671 {
672 SqlTransaction mySqlTransaction = null;
673 if (this.InTransaction)
674 {
675 mySqlTransaction = this.mySqlTransaction;
676 }
677 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, myDbParameters);
678 }
679 #endregion
680
681 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
682 /**//// <summary>
683 /// 执行查询
684 /// </summary>
685 /// <param name="myCommandType">命令分类</param>
686 /// <param name="sqlQuery">sql查询</param>
687 /// <param name="names">参数名</param>
688 /// <param name="values">参数值</param>
689 /// <returns>影响行数</returns>
690 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
691 {
692 SqlTransaction mySqlTransaction = null;
693 if (this.InTransaction)
694 {
695 mySqlTransaction = this.mySqlTransaction;
696 }
697 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, names, values);
698 }
699 #endregion
700
701 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
702 /**//// <summary>
703 /// 执行查询
704 /// </summary>
705 /// <param name="mySqlTransaction">数据库事务</param>
706 /// <param name="myCommandType">命令分类</param>
707 /// <param name="sqlQuery">sql查询</param>
708 /// <param name="myDbParameterCollection">参数集</param>
709 /// <returns>影响行数</returns>
710 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
711 {
712 // 写入调试信息
713 #if (DEBUG)
714 int milliStart = Environment.TickCount;
715 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
716 #endif
717 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
718 this.mySqlCommand.CommandType = myCommandType;
719 if (mySqlTransaction != null)
720 {
721 this.mySqlCommand.Transaction = (SqlTransaction)mySqlTransaction;
722 }
723 if (myDbParameters != null)
724 {
725 this.mySqlCommand.Parameters.Clear();
726 for (int i = 0; i < myDbParameters.Length; i++)
727 {
728 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
729 }
730 }
731 int returnValue = this.mySqlCommand.ExecuteNonQuery();
732 this.mySqlCommand.Parameters.Clear();
733 // 写入调试信息
734 #if (DEBUG)
735 int milliEnd = Environment.TickCount;
736 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
737 #endif
738 // 写入日志
739 this.WriteLog(sqlQuery);
740 return returnValue;
741 }
742 #endregion
743
744 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
745 /**//// <summary>
746 /// 执行查询
747 /// </summary>
748 /// <param name="mySqlTransaction">数据库事务</param>
749 /// <param name="myCommandType">命令分类</param>
750 /// <param name="sqlQuery">sql查询</param>
751 /// <param name="names">参数名</param>
752 /// <param name="values">参数值</param>
753 /// <returns>影响行数</returns>
754 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
755 {
756 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, this.GetParameters(names, values));
757 }
758 #endregion
759
760
761 public Object ExecuteScalar(String sqlQuery)#region public Object ExecuteScalar(String sqlQuery)
762 /**//// <summary>
763 /// 执行查询
764 /// </summary>
765 /// <param name="sqlQuery">sql查询</param>
766 /// <returns>object</returns>
767 public Object ExecuteScalar(String sqlQuery)
768 {
769 return this.ExecuteScalar(CommandType.Text, sqlQuery, null, null);
770 }
771 #endregion
772
773 public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)
774 /**//// <summary>
775 /// 执行查询
776 /// </summary>
777 /// <param name="sqlQuery">sql查询</param>
778 /// <param name="myDbParameterCollection">参数集</param>
779 /// <returns>Object</returns>
780 public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)
781 {
782 return this.ExecuteScalar(CommandType.Text, sqlQuery, myDbParameters);
783 }
784 #endregion
785
786 public Object ExecuteScalar(String sqlQuery, String name, Object value)#region public Object ExecuteScalar(String sqlQuery, String name, Object value)
787 /**//// <summary>
788 /// 执行查询
789 /// </summary>
790 /// <param name="sqlQuery">sql查询</param>
791 /// <param name="name">参数名</param>
792 /// <param name="value">参数值</param>
793 /// <returns>Object</returns>
794 public Object ExecuteScalar(DataSet myDataSet, String sqlQuery, String name, Object value)
795 {
796 String[] names = new String[1] { name };
797 Object[] values = new Object[1] { value };
798 return this.ExecuteScalar(CommandType.Text, sqlQuery, names, values);
799 }
800 #endregion
801
802 public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)
803 /**//// <summary>
804 /// 执行查询
805 /// </summary>
806 /// <param name="sqlQuery">sql查询</param>
807 /// <param name="names">参数名</param>
808 /// <param name="values">参数值</param>
809 /// <returns>影响行数</returns>
810 public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)
811 {
812 return this.ExecuteScalar(CommandType.Text, sqlQuery, names, values);
813 }
814 #endregion
815
816 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
817 /**//// <summary>
818 /// 执行查询
819 /// </summary>
820 /// <param name="myCommandType">命令分类</param>
821 /// <param name="sqlQuery">sql查询</param>
822 /// <param name="myDbParameterCollection">参数集</param>
823 /// <returns>Object</returns>
824 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
825 {
826 SqlTransaction mySqlTransaction = null;
827 if (this.InTransaction)
828 {
829 mySqlTransaction = this.mySqlTransaction;
830 }
831 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, myDbParameters);
832 }
833 #endregion
834
835 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
836 /**//// <summary>
837 /// 执行查询
838 /// </summary>
839 /// <param name="myCommandType">命令分类</param>
840 /// <param name="sqlQuery">sql查询</param>
841 /// <param name="names">参数名</param>
842 /// <param name="values">参数值</param>
843 /// <returns>影响行数</returns>
844 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
845 {
846 SqlTransaction mySqlTransaction = null;
847 if (this.InTransaction)
848 {
849 mySqlTransaction = this.mySqlTransaction;
850 }
851 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, names, values);
852 }
853 #endregion
854
855 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
856 /**//// <summary>
857 /// 执行查询
858 /// </summary>
859 /// <param name="mySqlTransaction">数据库事务</param>
860 /// <param name="myCommandType">命令分类</param>
861 /// <param name="sqlQuery">sql查询</param>
862 /// <param name="myDbParameterCollection">参数集</param>
863 /// <returns>Object</returns>
864 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
865 {
866 // 写入调试信息
867 #if (DEBUG)
868 int milliStart = Environment.TickCount;
869 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
870 #endif
871 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
872 this.mySqlCommand.CommandType = myCommandType;
873 if (mySqlTransaction != null)
874 {
875 this.mySqlCommand.Transaction = (SqlTransaction)mySqlTransaction;
876 }
877 if (myDbParameters != null)
878 {
879 this.mySqlCommand.Parameters.Clear();
880 for (int i = 0; i < myDbParameters.Length; i++)
881 {
882 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
883 }
884 }
885 Object returnValue = this.mySqlCommand.ExecuteScalar();
886 this.mySqlCommand.Parameters.Clear();
887 // 写入调试信息
888 #if (DEBUG)
889 int milliEnd = Environment.TickCount;
890 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
891 #endif
892 // 写入日志
893 this.WriteLog(sqlQuery);
894 return returnValue;
895 }
896 #endregion
897
898 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
899 /**//// <summary>
900 /// 执行查询
901 /// </summary>
902 /// <param name="mySqlTransaction">数据库事务</param>
903 /// <param name="myCommandType">命令分类</param>
904 /// <param name="sqlQuery">sql查询</param>
905 /// <param name="names"></param>
906 /// <param name="values"></param>
907 /// <returns>影响行数</returns>
908 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
909 {
910 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, this.GetParameters(names, values));
911 }
912 #endregion
913
914
915 public DataTable Fill(DataTable myDataTable, String sqlQuery)#region public DataTable Fill(DataTable myDataTable, String sqlQuery)
916 /**//// <summary>
917 /// 填充数据表
918 /// </summary>
919 /// <param name="myDataTable">目标数据表</param>
920 /// <param name="sqlQuery">查询</param>
921 /// <returns>数据表</returns>
922 public DataTable Fill(DataTable myDataTable, String sqlQuery)
923 {
924 return this.Fill(myDataTable, CommandType.Text, sqlQuery, null, null);
925 }
926 #endregion
927
928 public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)
929 /**//// <summary>
930 /// 填充数据表
931 /// </summary>
932 /// <param name="myDataTable">目标数据表</param>
933 /// <param name="sqlQuery">sql查询</param>
934 /// <param name="myDbParameterCollection">参数集</param>
935 /// <returns>数据表</returns>
936 public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)
937 {
938 return this.Fill(myDataTable, CommandType.Text, sqlQuery, myDbParameters);
939 }
940 #endregion
941
942 public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)
943 /**//// <summary>
944 /// 填充数据表
945 /// </summary>
946 /// <param name="myDataSet">目标数据表</param>
947 /// <param name="sqlQuery">sql查询</param>
948 /// <param name="name">参数名</param>
949 /// <param name="value">参数值</param>
950 /// <returns>数据表</returns>
951 public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)
952 {
953 String[] names = new String[1] { name };
954 Object[] values = new Object[1] { value };
955 return this.Fill(myDataTable, CommandType.Text, sqlQuery, names, values);
956 }
957 #endregion
958
959 public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)
960 /**//// <summary>
961 /// 填充数据表
962 /// </summary>
963 /// <param name="myDataSet">目标数据表</param>
964 /// <param name="sqlQuery">sql查询</param>
965 /// <param name="names">参数名</param>
966 /// <param name="values">参数值</param>
967 /// <returns>数据表</returns>
968 public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)
969 {
970 return this.Fill(myDataTable, CommandType.Text, sqlQuery, names, values);
971 }
972 #endregion
973
974 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
975 /**//// <summary>
976 /// 填充数据表
977 /// </summary>
978 /// <param name="myDataSet">目标数据表</param>
979 /// <param name="myCommandType">命令分类</param>
980 /// <param name="sqlQuery">sql查询</param>
981 /// <param name="myDbParameterCollection">参数集</param>
982 /// <returns>数据表</returns>
983 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
984 {
985 // 写入调试信息
986 #if (DEBUG)
987 int milliStart = Environment.TickCount;
988 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
989 #endif
990 this.mySqlDataAdapter = new SqlDataAdapter(sqlQuery, this.mySqlConnection);
991 this.mySqlDataAdapter.SelectCommand.CommandType = myCommandType;
992 if (this.InTransaction)
993 {
994 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
995 }
996 if (myDbParameters != null)
997 {
998 this.mySqlCommand.Parameters.Clear();
999 for (int i = 0; i < myDbParameters.Length; i++)
1000 {
1001 this.mySqlDataAdapter.SelectCommand.Parameters.Add(myDbParameters[i]);
1002 }
1003 }
1004 this.mySqlDataAdapter.Fill(myDataTable);
1005 this.mySqlDataAdapter.SelectCommand.Parameters.Clear();
1006 // 写入调试信息
1007 #if (DEBUG)
1008 int milliEnd = Environment.TickCount;
1009 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1010 #endif
1011 // 写入日志
1012 this.WriteLog(sqlQuery);
1013 return myDataTable;
1014 }
1015 #endregion
1016
1017 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1018 /**//// <summary>
1019 /// 填充数据表
1020 /// </summary>
1021 /// <param name="myDataSet">目标数据表</param>
1022 /// <param name="myCommandType">命令分类</param>
1023 /// <param name="sqlQuery">sql查询</param>
1024 /// <param name="names">参数名</param>
1025 /// <param name="values">参数值</param>
1026 /// <returns>数据表</returns>
1027 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1028 {
1029 return this.Fill(myDataTable, myCommandType, sqlQuery, this.GetParameters(names, values));
1030 }
1031 #endregion
1032
1033
1034 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)
1035 /**//// <summary>
1036 /// 填充数据集
1037 /// </summary>
1038 /// <param name="myDataSet">目标数据集</param>
1039 /// <param name="sqlQuery">查询</param>
1040 /// <param name="tableName">填充表</param>
1041 /// <returns>数据集</returns>
1042 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)
1043 {
1044 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, null, null);
1045 }
1046 #endregion
1047
1048 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1049 /**//// <summary>
1050 /// 填充数据集
1051 /// </summary>
1052 /// <param name="myDataSet">数据集</param>
1053 /// <param name="sqlQuery">sql查询</param>
1054 /// <param name="tableName">填充表</param>
1055 /// <param name="myDbParameterCollection">参数集</param>
1056 /// <returns>数据集</returns>
1057 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1058 {
1059 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, myDbParameters);
1060 }
1061 #endregion
1062
1063 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)
1064 /**//// <summary>
1065 /// 填充数据集
1066 /// </summary>
1067 /// <param name="myDataSet">数据集</param>
1068 /// <param name="sqlQuery">sql查询</param>
1069 /// <param name="tableName">填充表</param>
1070 /// <param name="name">参数名</param>
1071 /// <param name="value">参数值</param>
1072 /// <returns>数据集</returns>
1073 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)
1074 {
1075 String[] names = new String[1] { name };
1076 Object[] values = new Object[1] { value };
1077 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, names, values);
1078 }
1079 #endregion
1080
1081 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)
1082 /**//// <summary>
1083 /// 填充数据集
1084 /// </summary>
1085 /// <param name="myDataSet">数据集</param>
1086 /// <param name="sqlQuery">sql查询</param>
1087 /// <param name="tableName">填充表</param>
1088 /// <param name="names">参数名</param>
1089 /// <param name="values">参数值</param>
1090 /// <returns>数据集</returns>
1091 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)
1092 {
1093 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, names, values);
1094 }
1095 #endregion
1096
1097 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)#region public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1098 /**//// <summary>
1099 /// 填充数据集
1100 /// </summary>
1101 /// <param name="myDataSet">数据集</param>
1102 /// <param name="myCommandType">命令分类</param>
1103 /// <param name="sqlQuery">sql查询</param>
1104 /// <param name="tableName">填充表</param>
1105 /// <param name="myDbParameterCollection">参数集</param>
1106 /// <returns>数据集</returns>
1107 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1108 {
1109 // 写入调试信息
1110 #if (DEBUG)
1111 int milliStart = Environment.TickCount;
1112 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1113 #endif
1114 this.mySqlDataAdapter = new SqlDataAdapter(sqlQuery, this.mySqlConnection);
1115 this.mySqlDataAdapter.SelectCommand.CommandType = myCommandType;
1116 if (this.InTransaction)
1117 {
1118 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
1119 }
1120 if (myDbParameters != null)
1121 {
1122 this.mySqlCommand.Parameters.Clear();
1123 for (int i = 0; i < myDbParameters.Length; i++)
1124 {
1125 this.mySqlDataAdapter.SelectCommand.Parameters.Add(myDbParameters[i]);
1126 }
1127 }
1128 this.mySqlDataAdapter.Fill(myDataSet, tableName);
1129 this.mySqlDataAdapter.SelectCommand.Parameters.Clear();
1130 // 写入调试信息
1131 #if (DEBUG)
1132 int milliEnd = Environment.TickCount;
1133 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1134 #endif
1135 // 写入日志
1136 this.WriteLog(sqlQuery);
1137 return myDataSet;
1138 }
1139 #endregion
1140
1141 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)#region public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)
1142 /**//// <summary>
1143 /// 填充数据集
1144 /// </summary>
1145 /// <param name="myDataSet">数据集</param>
1146 /// <param name="myCommandType">命令分类</param>
1147 /// <param name="sqlQuery">sql查询</param>
1148 /// <param name="tableName">填充表</param>
1149 /// <param name="names">参数名</param>
1150 /// <param name="values">参数值</param>
1151 /// <returns>数据集</returns>
1152 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)
1153 {
1154 return this.Fill(myDataSet, myCommandType, sqlQuery, tableName, this.GetParameters(names, values));
1155 }
1156 #endregion
1157
1158
1159 public int ExecuteProcedure(String procedureName)#region public int ExecuteProcedure(String procedureName)
1160 /**//// <summary>
1161 /// 执行数据库查询
1162 /// </summary>
1163 /// <param name="procedureName">存储过程</param>
1164 /// <returns>int</returns>
1165 public int ExecuteProcedure(String procedureName)
1166 {
1167 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, null, null);
1168 }
1169 #endregion
1170
1171 public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)#region public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)
1172 /**//// <summary>
1173 /// 执行存储过程
1174 /// </summary>
1175 /// <param name="procedureName">存储过程名</param>
1176 /// <param name="myDbParameterCollection">参数集</param>
1177 /// <returns>影响行数</returns>
1178 public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)
1179 {
1180 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, myDbParameters);
1181 }
1182 #endregion
1183
1184 public int ExecuteProcedure(String procedureName, String name, String value) 执行存储过程#region public int ExecuteProcedure(String procedureName, String name, String value) 执行存储过程
1185 /**//// <summary>
1186 /// 执行存储过程
1187 /// </summary>
1188 /// <param name="procedureName">存储过程名</param>
1189 /// <param name="name">参数名</param>
1190 /// <param name="value">参数值</param>
1191 /// <returns>影响行数</returns>
1192 public int ExecuteProcedure(String procedureName, String name, String value)
1193 {
1194 String[] names = new String[1];
1195 Object[] values = new Object[1];
1196 int returnValue = 0;
1197 names[0] = name;
1198 values[0] = value;
1199 returnValue = this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, names, values);
1200 return returnValue;
1201 }
1202 #endregion
1203
1204 public int ExecuteProcedure(String procedureName, String[] names, Object[] values)#region public int ExecuteProcedure(String procedureName, String[] names, Object[] values)
1205 /**//// <summary>
1206 /// 执行数据库脚本
1207 /// </summary>
1208 /// <param name="procedureName">存储过程</param>
1209 /// <param name="names">参数名</param>
1210 /// <param name="values">参数值</param>
1211 /// <returns>影响行数</returns>
1212 public int ExecuteProcedure(String procedureName, String[] names, Object[] values)
1213 {
1214 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, names, values);
1215 }
1216 #endregion
1217
1218 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)#region public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)
1219 /**//// <summary>
1220 /// 执行数据库脚本
1221 /// </summary>
1222 /// <param name="myDataSet">数据集</param>
1223 /// <param name="procedureName">存储过程</param>
1224 /// <param name="tableName">填充表</param>
1225 /// <param name="myDbParameterCollection">参数集</param>
1226 /// <returns>数据集</returns>
1227 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)
1228 {
1229 return this.Fill(myDataSet, CommandType.StoredProcedure, procedureName, tableName, myDbParameters);
1230 }
1231 #endregion
1232
1233 public DataSet ExecuteProcedureForDataSet(ref DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)#region public DataSet ExecuteProcedureForDataSet(ref DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)
1234 /**//// <summary>
1235 /// 执行数据库脚本
1236 /// </summary>
1237 /// <param name="myDataSet">数据集</param>
1238 /// <param name="procedureName">存储过程</param>
1239 /// <param name="tableName">填充表</param>
1240 /// <param name="names">参数名</param>
1241 /// <param name="values">参数值</param>
1242 /// <returns>影响行数</returns>
1243 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)
1244 {
1245 return this.Fill(myDataSet, CommandType.StoredProcedure, procedureName, tableName, names, values);
1246 }
1247 #endregion
1248
1249
1250 public IDbTransaction BeginTransaction()#region public IDbTransaction BeginTransaction()
1251 /**//// <summary>
1252 /// 事务开始
1253 /// </summary>
1254 /// <returns>事务</returns>
1255 public IDbTransaction BeginTransaction()
1256 {
1257 // 写入调试信息
1258 #if (DEBUG)
1259 int milliStart = Environment.TickCount;
1260 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1261 #endif
1262 if (!this.InTransaction)
1263 {
1264 this.InTransaction = true;
1265 this.mySqlTransaction = this.mySqlConnection.BeginTransaction();
1266 this.mySqlCommand.Transaction = this.mySqlTransaction;
1267 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
1268 }
1269 // 写入调试信息
1270 #if (DEBUG)
1271 int milliEnd = Environment.TickCount;
1272 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1273 #endif
1274 return this.mySqlTransaction;
1275 }
1276 #endregion
1277
1278 public void CommitTransaction()#region public void CommitTransaction()
1279 /**//// <summary>
1280 /// 递交事务
1281 /// </summary>
1282 public void CommitTransaction()
1283 {
1284 // 写入调试信息
1285 #if (DEBUG)
1286 int milliStart = Environment.TickCount;
1287 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1288 #endif
1289 if (this.InTransaction)
1290 {
1291 // 事务已经完成了,一定要更新标志信息
1292 this.InTransaction = false;
1293 this.mySqlTransaction.Commit();
1294 }
1295 // 写入调试信息
1296 #if (DEBUG)
1297 int milliEnd = Environment.TickCount;
1298 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1299 #endif
1300 }
1301 #endregion
1302
1303 public void RollbackTransaction()#region public void RollbackTransaction()
1304 /**//// <summary>
1305 /// 回滚事务
1306 /// </summary>
1307 public void RollbackTransaction()
1308 {
1309 // 写入调试信息
1310 #if (DEBUG)
1311 int milliStart = Environment.TickCount;
1312 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1313 #endif
1314 if (this.InTransaction)
1315 {
1316 this.InTransaction = false;
1317 this.mySqlTransaction.Rollback();
1318 }
1319 // 写入调试信息
1320 #if (DEBUG)
1321 int milliEnd = Environment.TickCount;
1322 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1323 #endif
1324 }
1325 #endregion
1326
1327
1328 public delegate int Asyncdelegate();
1329
1330 private void CallbackMethod(IAsyncResult myIAsyncResult) 异步调用的挂接代理#region private void CallbackMethod(IAsyncResult myIAsyncResult) 异步调用的挂接代理
1331 /**//// <summary>
1332 /// 异步调用的挂接代理
1333 /// </summary>
1334 /// <param name="myIAsyncResult">回调</param>
1335 private void CallbackMethod(IAsyncResult myIAsyncResult)
1336 {
1337 Asyncdelegate myAsyncdelegate = (Asyncdelegate)myIAsyncResult.AsyncState;
1338 myAsyncdelegate.EndInvoke(myIAsyncResult);
1339 }
1340 #endregion
1341
1342 // 定义与方法同签名的委托
1343 private delegate int DelegateExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values);
1344
1345 private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1346 /**//// <summary>
1347 /// 异步调用数据库查询
1348 /// </summary>
1349 /// <param name="myCommandType">命令分类</param>
1350 /// <param name="sqlQuery">sql查询</param>
1351 /// <param name="names">参数名</param>
1352 /// <param name="values">参数值</param>
1353 /// <returns>影响行数</returns>
1354 private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1355 {
1356 int returnValue = 0;
1357 this.Open();
1358 returnValue = this.ExecuteNonQuery(myCommandType, sqlQuery, names, values);
1359 this.Close();
1360 return returnValue;
1361 }
1362 #endregion
1363
1364 public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1365 /**//// <summary>
1366 /// 异步调用数据库查询
1367 /// </summary>
1368 /// <param name="myCommandType">命令分类</param>
1369 /// <param name="sqlQuery">sql查询</param>
1370 /// <param name="names">参数名</param>
1371 /// <param name="values">参数值</param>
1372 public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1373 {
1374 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1375
1376 DelegateExecuteNonQuery myDelegateExecuteNonQuery = new DelegateExecuteNonQuery(AsyncExecuteNonQuery);
1377 myDelegateExecuteNonQuery.BeginInvoke(myCommandType, sqlQuery, names, values, myAsyncCallback, null);
1378 }
1379 #endregion
1380
1381 // 定义与方法同签名的委托
1382 private delegate Object DelegateExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values);
1383
1384 private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1385 /**//// <summary>
1386 /// 异步调用数据库查询
1387 /// </summary>
1388 /// <param name="myCommandType">命令分类</param>
1389 /// <param name="sqlQuery">sql查询</param>
1390 /// <param name="names">参数名</param>
1391 /// <param name="values">参数值</param>
1392 /// <returns>对象</returns>
1393 private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1394 {
1395 Object returnValue = null;
1396 this.Open();
1397 returnValue = this.ExecuteScalar(myCommandType, sqlQuery, names, values);
1398 this.Close();
1399 return returnValue;
1400 }
1401 #endregion
1402
1403 public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1404 /**//// <summary>
1405 /// 异步调用数据库查询
1406 /// </summary>
1407 /// <param name="myCommandType">命令分类</param>
1408 /// <param name="sqlQuery">sql查询</param>
1409 /// <param name="names">参数名</param>
1410 /// <param name="values">参数值</param>
1411 public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1412 {
1413 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1414
1415 DelegateExecuteScalar myDelegateExecuteScalar = new DelegateExecuteScalar(AsyncExecuteScalar);
1416 myDelegateExecuteScalar.BeginInvoke(myCommandType, sqlQuery, names, values, myAsyncCallback, null);
1417 }
1418 #endregion
1419
1420 // 定义与方法同签名的委托
1421 private delegate int DelegateExecuteProcedure(String procedureName, String[] names, Object[] values);
1422
1423 private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values) 异步调用数据库查询#region private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values) 异步调用数据库查询
1424 /**//// <summary>
1425 /// 异步调用数据库查询
1426 /// </summary>
1427 /// <param name="procedureName">存储过程</param>
1428 /// <param name="names">参数名</param>
1429 /// <param name="values">参数值</param>
1430 /// <returns>影响行数</returns>
1431 private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values)
1432 {
1433 int returnValue = 0;
1434 this.Open();
1435 returnValue = this.ExecuteProcedure(procedureName, names, values);
1436 this.Close();
1437 return returnValue;
1438 }
1439 #endregion
1440
1441 public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values) 异步调用数据库查询
1442 /**//// <summary>
1443 /// 异步调用数据库查询
1444 /// </summary>
1445 /// <param name="procedureName">存储过程</param>
1446 /// <param name="names">参数名</param>
1447 /// <param name="values">参数值</param>
1448 public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values)
1449 {
1450 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1451
1452 DelegateExecuteProcedure myDelegateExecuteProcedure = new DelegateExecuteProcedure(AsyncExecuteProcedure);
1453 myDelegateExecuteProcedure.BeginInvoke(procedureName, names, values, myAsyncCallback, null);
1454 }
1455 #endregion
1456
1457
1458 public void WriteLog(String sqlQuery) 写入SQL查询语句日志#region public void WriteLog(String sqlQuery) 写入SQL查询语句日志
1459 /**//// <summary>
1460 /// 写入SQL查询语日志
1461 /// </summary>
1462 /// <param name="sqlQuery">SQL查询语</param>
1463 public void WriteLog(String sqlQuery)
1464 {
1465 this.WriteLog(DateTime.Now.ToString(BaseSystemInfo.DateFormat) + "_" + this.FileName, sqlQuery);
1466 // 将调试信息输出到屏幕上
1467 #if DEBUG
1468 System.Console.WriteLine(sqlQuery);
1469 #endif
1470 }
1471 #endregion
1472
1473 public void WriteLog(String fileName, String sqlQuery) 写入SQL查询语句日志#region public void WriteLog(String fileName, String sqlQuery) 写入SQL查询语句日志
1474 /**//// <summary>
1475 /// 写入SQL查询语句
1476 /// </summary>
1477 /// <param name="fileName">文件名</param>
1478 /// <param name="sqlQuery">异常</param>
1479 public void WriteLog(String fileName, String sqlQuery)
1480 {
1481 String returnValue = String.Empty;
1482 // 系统里应该可以配置是否记录异常现象
1483 if (!BaseSystemInfo.LogSQL)
1484 {
1485 return;
1486 }
1487 // 将异常信息写入本地文件中
1488 String logDirectory = BaseSystemInfo.StartupPath + @"\\Log\\Query";
1489 if (!System.IO.Directory.Exists(logDirectory))
1490 {
1491 System.IO.Directory.CreateDirectory(logDirectory);
1492 }
1493 String writerFileName = logDirectory + "\\" + fileName;
1494 if (!File.Exists(writerFileName))
1495 {
1496 FileStream myFileStream = new FileStream(writerFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
1497 myFileStream.Close();
1498 }
1499 StreamWriter myStreamWriter = new StreamWriter(writerFileName, true, Encoding.Default);
1500 myStreamWriter.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " " + sqlQuery);
1501 myStreamWriter.Close();
1502 }
1503 #endregion
1504
1505
1506 public void Close()#region public void Close()
1507 /**//// <summary>
1508 /// 关闭数据库连接
1509 /// </summary>
1510 public void Close()
1511 {
1512 // 写入调试信息
1513 #if (DEBUG)
1514 int milliStart = Environment.TickCount;
1515 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1516 #endif
1517 if (this.mySqlConnection != null)
1518 {
1519 this.mySqlConnection.Close();
1520 this.mySqlConnection.Dispose();
1521 }
1522 // 写入调试信息
1523 #if (DEBUG)
1524 int milliEnd = Environment.TickCount;
1525 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1526 #endif
1527 }
1528 #endregion
1529
1530 private void Dispose(bool disposing)#region private void Dispose(bool disposing)
1531 /**//// <summary>
1532 /// 卸载
1533 /// </summary>
1534 /// <param name="disposing">卸载</param>
1535 private void Dispose(bool disposing)
1536 {
1537 if (disposing)
1538 {
1539 this.mySqlCommand.Dispose();
1540 this.mySqlDataAdapter.Dispose();
1541 this.mySqlTransaction.Dispose();
1542 this.mySqlConnection.Dispose();
1543 }
1544 }
1545 #endregion
1546
1547 public void Dispose()#region public void Dispose()
1548 /**//// <summary>
1549 /// 卸载
1550 /// </summary>
1551 public void Dispose()
1552 {
1553 Dispose(true);
1554 GC.SuppressFinalize(this);
1555 }
1556 #endregion
1557 }
1558}
1//------------------------------------------------------------
2// All Rights Reserved , Copyright (C) 2008 , Jirisoft , Ltd.
3//------------------------------------------------------------
4
5using System;
6using System.IO;
7using System.Text;
8using System.Globalization;
9using System.Data;
10using System.Data.Common;
11using System.Data.SqlClient;
12using System.Reflection;
13using System.Diagnostics;
14
15namespace Jirisoft.Common.DbUtilities
16{
17 using Jirisoft.Common;
18 using Jirisoft.Common.Utilities;
19
20 /**//// <summary>
21 /// SqlHelper
22 /// 有关数据库连接的方法。
23 ///
24 /// 修改纪录
25 ///
26 /// 2008.06.01 版本:1.1 JiRiGaLa 数据库连接获得方式进行改进,构造函数获得调通。
27 /// 2008.05.07 版本:1.0 JiRiGaLa 创建。
28 ///
29 /// 版本:1.0
30 ///
31 /// <author>
32 /// <name>JiRiGaLa</name>
33 /// <date>2008.05.07</date>
34 /// </author>
35 /// </summary>
36 public class SqlHelper : IDbHelper, IDisposable
37 {
38 public SqlConnection mySqlConnection = new SqlConnection();
39 public SqlCommand mySqlCommand = new SqlCommand();
40 public SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
41 public SqlTransaction mySqlTransaction;
42 private bool inTransaction; // 是否已在事务之中
43
44 public String FileName = "SqlHelper.txt"; // SQL查询语句日志
45
46
47 public SqlHelper()#region public SqlHelper()
48 /**//// <summary>
49 /// 构造方法
50 /// </summary>
51 public SqlHelper()
52 {
53 }
54 #endregion
55
56 public SqlHelper(String connectionString)#region public SqlHelper(String connectionString)
57 /**//// <summary>
58 /// 设定软件名称
59 /// </summary>
60 /// <param name="connectionString">数据连接</param>
61 public SqlHelper(String connectionString)
62 {
63 this.mySqlConnection.ConnectionString = connectionString;
64 }
65 #endregion
66
67 public String GetDBNow()#region public String GetDBNow()
68 /**//// <summary>
69 /// 获得数据库日期时间
70 /// </summary>
71 /// <returns>日期时间</returns>
72 public String GetDBNow()
73 {
74 String returnValue = " Getdate() ";
75 switch (BaseConfiguration.Instance.DataBaseType)
76 {
77 case DataBaseType.Access:
78 returnValue = "'" + DateTime.Now.ToString() + "'";
79 break;
80 case DataBaseType.Sqlserver:
81 returnValue = " GetDate() ";
82 break;
83 case DataBaseType.Oracle:
84 returnValue = " SYSDATE ";
85 break;
86 }
87 return returnValue;
88 }
89 #endregion
90
91 public DateTime GetDBDateTime() 获得数据库日期时间#region public DateTime GetDBDateTime() 获得数据库日期时间
92 /**//// <summary>
93 /// 获得数据库日期时间
94 /// </summary>
95 /// <returns>日期时间</returns>
96 public DateTime GetDBDateTime()
97 {
98 String sqlQuery = this.GetDBNow();
99 Object myObject = this.ExecuteScalar(sqlQuery);
100 return DateTime.Parse(myObject.ToString());
101 }
102 #endregion
103
104 public bool InTransaction 是否已采用事务#region public bool InTransaction 是否已采用事务
105 /**//// <summary>
106 /// 是否已采用事务
107 /// </summary>
108 /// <returns>采用事务</returns>
109 public bool InTransaction
110 {
111 get
112 {
113 return this.inTransaction;
114 }
115 set
116 {
117 this.inTransaction = value;
118 }
119 }
120 #endregion
121
122 public IDbConnection GetDbConnection() 获取数据库连接#region public IDbConnection GetDbConnection() 获取数据库连接
123 /**//// <summary>
124 /// 获取数据库连接
125 /// </summary>
126 /// <returns>数据库连接</returns>
127 public IDbConnection GetDbConnection()
128 {
129 return this.mySqlConnection;
130 }
131 #endregion
132
133 public IDbTransaction GetDbTransaction() 获取数据源上执行的事务#region public IDbTransaction GetDbTransaction() 获取数据源上执行的事务
134 /**//// <summary>
135 /// 获取数据源上执行的事务
136 /// </summary>
137 /// <returns>数据源上执行的事务</returns>
138 public IDbTransaction GetDbTransaction()
139 {
140 return this.mySqlTransaction;
141 }
142 #endregion
143
144 public IDbCommand GetDbCommand() 获取数据源上命令#region public IDbCommand GetDbCommand() 获取数据源上命令
145 /**//// <summary>
146 /// 获取数据源上命令
147 /// </summary>
148 /// <returns>数据源上命令</returns>
149 public IDbCommand GetDbCommand()
150 {
151 return this.mySqlCommand;
152 }
153 #endregion
154
155
156 public IDbConnection Open()#region public IDbConnection Open()
157 /**//// <summary>
158 /// 这时主要的获取数据库连接的方法
159 /// </summary>
160 /// <returns>数据库连接</returns>
161 public IDbConnection Open()
162 {
163 #if (DEBUG)
164 int milliStart = Environment.TickCount;
165 #endif
166 if (String.IsNullOrEmpty(this.mySqlConnection.ConnectionString))
167 {
168 // 这里是获取一个连接的详细方法
169 if (BaseConfiguration.Instance.ConnectionString.Length == 0)
170 {
171 BaseConfiguration.Instance.GetSetting();
172 }
173 this.Open(BaseConfiguration.Instance.SqlConnection);
174 }
175 // 写入调试信息
176 #if (DEBUG)
177 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
178 #endif
179 return this.mySqlConnection;
180 }
181 #endregion
182
183 public IDbConnection Open(String connectionString)#region public IDbConnection Open(String connectionString)
184 /**//// <summary>
185 /// 获得新的数据库连接
186 /// </summary>
187 /// <param name="connectionString">数据库连接字符串</param>
188 /// <returns>数据库连接</returns>
189 public IDbConnection Open(String connectionString)
190 {
191 // 写入调试信息
192 #if (DEBUG)
193 int milliStart = Environment.TickCount;
194 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
195 #endif
196 // 这里数据库连接打开的时候,就判断注册属性的有效性
197 if (!BaseCodeChange.Instance.CheckRegister())
198 {
199 // 若没有进行注册,让程序无法打开数据库比较好。
200 this.mySqlConnection.ConnectionString = String.Empty;
201 // 抛出异常信息显示给客户
202 throw new Exception(BaseConfiguration.Instance.ExceptionInfo);
203 }
204 if (this.mySqlConnection.State == ConnectionState.Closed)
205 {
206 this.mySqlConnection.ConnectionString = connectionString;
207 this.mySqlConnection.Open();
208 // 创建对象
209 this.mySqlCommand = new SqlCommand(String.Empty, this.mySqlConnection);
210 this.mySqlDataAdapter = new SqlDataAdapter(String.Empty, this.mySqlConnection);
211 // 写入调试信息
212 #if (DEBUG)
213 int milliEnd = Environment.TickCount;
214 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
215 #endif
216 }
217 return this.mySqlConnection;
218 }
219 #endregion
220
221
222 public bool TestConn(String dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)#region public bool TestConn(String dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)
223 /**//// <summary>
224 /// 测试数据库连接是否成功,这里抛出异常进行加强,充分使用系统的异常功能。
225 /// </summary>
226 /// <param name="dataBaseType">数据库类别</param>
227 /// <param name="dataBase">数据库名称</param>
228 /// <param name="userName">用户名</param>
229 /// <param name="password">密码</param>
230 /// <param name="workstation">服务器名称</param>
231 /// <param name="trustLink">是否信任的连接</param>
232 /// <returns>是否连接成功</returns>
233 public bool TestConn(DataBaseType dataBaseType, String dataBase, String userName, String password, String workstation, bool trustLink)
234 {
235 // 写入调试信息
236 #if (DEBUG)
237 int milliStart = Environment.TickCount;
238 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
239 #endif
240 bool returnValue = false; // 连接是否成功
241 String connectionString = String.Empty; // 连接字符传
242 SqlConnection DbConnection = null; // 数据库连接
243 connectionString = BaseConfiguration.Instance.GetOleDbConnection(dataBaseType, dataBase, userName, password, workstation, trustLink);
244 DbConnection = (SqlConnection)this.Open(connectionString);
245 try
246 {
247 if (DbConnection.State == ConnectionState.Closed)
248 {
249 DbConnection.Open();
250 }
251 DbConnection.Close();
252 returnValue = true;
253 }
254 catch (SqlException mySqlException)
255 {
256 DbConnection = null;
257 throw mySqlException;
258 }
259 // 写入调试信息
260 #if (DEBUG)
261 int milliEnd = Environment.TickCount;
262 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
263 #endif
264 return returnValue;
265 }
266 #endregion
267
268 public String SqlSafe(String value) 检查参数的安全性#region public String SqlSafe(String value) 检查参数的安全性
269 /**//// <summary>
270 /// 检查参数的安全性
271 /// </summary>
272 /// <param name="value">参数</param>
273 /// <returns>安全的参数</returns>
274 public String SqlSafe(String value)
275 {
276 value = value.Replace("'", "''");
277 // value = value.Replace("%", "'%");
278 return value;
279 }
280 #endregion
281
282 public DbParameter[] GetParameters(String[] targetFileds, Object[] targetValues)#region public DbParameter[] GetParameters(String[] targetFileds, Object[] targetValues)
283 /**//// <summary>
284 /// 获取参数
285 /// </summary>
286 /// <param name="targetFiled">目标字段</param>
287 /// <param name="targetValue">值</param>
288 /// <returns>参数集</returns>
289 public DbParameter[] GetParameters(String[] targetFileds, Object[] myTargetValues)
290 {
291 DbParameter[] myDbParameters = new DbParameter[0];
292 if (targetFileds != null && myTargetValues != null)
293 {
294 myDbParameters = new DbParameter[targetFileds.Length];
295 for (int i = 0; i < targetFileds.Length; i++)
296 {
297 myDbParameters[i] = this.GetParameter(targetFileds[i], myTargetValues[i]);
298 }
299 }
300 return myDbParameters;
301 }
302 #endregion
303
304 public DbParameter GetParameter(String targetFiled, Object targetValue)#region public DbParameter GetParameter(String targetFiled, Object targetValue)
305 /**//// <summary>
306 /// 获取参数
307 /// </summary>
308 /// <param name="targetFiled">目标字段</param>
309 /// <param name="targetValue">值</param>
310 /// <returns>参数</returns>
311 public DbParameter GetParameter(String targetFiled, Object myTargetValue)
312 {
313 SqlParameter myDbParameter = null;
314 if (myTargetValue is String)
315 {
316 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
317 }
318 else
319 {
320 if (myTargetValue is Boolean)
321 {
322 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Bit);
323 }
324 else
325 {
326 if (myTargetValue is DateTime)
327 {
328 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.DateTime);
329 }
330 else
331 {
332 if (myTargetValue is int)
333 {
334 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Int);
335 }
336 else
337 {
338 if (myTargetValue is Int64)
339 {
340 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.BigInt);
341 }
342 else
343 {
344 if (myTargetValue is Double)
345 {
346 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Decimal);
347 }
348 else
349 {
350 if (myTargetValue is Byte[])
351 {
352 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.Binary);
353 }
354 else
355 {
356 myDbParameter = new SqlParameter("@" + targetFiled, SqlDbType.VarChar);
357 }
358 }
359 }
360 }
361 }
362 }
363 }
364
365 myDbParameter.Value = myTargetValue;
366 return myDbParameter;
367 }
368 #endregion
369
370 public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)#region public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)
371 /**//// <summary>
372 /// 添加参数
373 /// </summary>
374 /// <param name="myTargetDbCommand">数据库命令</param>
375 /// <param name="targetFiled">目标字段</param>
376 /// <param name="targetValue">值</param>
377 public void AddParameter(IDbCommand myTargetDbCommand, String targetFiled, Object myTargetValue)
378 {
379 ((SqlCommand)myTargetDbCommand).Parameters.Add(this.GetParameter(targetFiled, myTargetValue));
380 }
381 #endregion
382
383 public String GetWhereString(String[] names, ref Object[] values, String relation) 获得条件语句#region public String GetWhereString(String[] names, ref Object[] values, String relation) 获得条件语句
384 /**//// <summary>
385 /// 获得条件语句
386 /// </summary>
387 /// <param name="names">字段名</param>
388 /// <param name="values">字段值</param>
389 /// <param name="relation">逻辑关系</param>
390 /// <returns>字符串</returns>
391 public String GetWhereString(String[] names, ref Object[] values, String relation)
392 {
393 String returnValue = String.Empty;
394 String subSqlQuery = String.Empty;
395 for (int i = 0; i < names.Length; i++)
396 {
397 if ((names[i] != null) && (names[i].Length > 0))
398 {
399 if (values[i] == null || String.IsNullOrEmpty(values[i].ToString()))
400 {
401 subSqlQuery = " (" + names[i] + " IS NULL) ";
402 }
403 else
404 {
405 // 这里操作,就会有些重复了,不应该进行处理
406 // values[i] = this.SqlSafe(values[i].ToString());
407 subSqlQuery = " (" + names[i] + " = " + this.GetParameter(names[i]) + ") ";
408
409 if ((values[i].ToString().IndexOf('[') >= 0) || (values[i].ToString().IndexOf(']') >= 0))
410 {
411 values[i] = values[i].ToString().Replace("[", "/[");
412 values[i] = values[i].ToString().Replace("]", "/]");
413 values[i] = this.SqlSafe(values[i].ToString());
414 subSqlQuery = " (" + names[i] + " LIKE '" + values[i] + "' ESCAPE '/') ";
415 values[i] = null;
416 // subSqlQuery = " (" + names[i] + " LIKE ? ESCAPE '/') ";
417 }
418 }
419 returnValue += subSqlQuery + relation;
420 }
421 }
422 if (returnValue.Length > 0)
423 {
424 returnValue = returnValue.Substring(0, returnValue.Length - relation.Length - 1);
425 }
426 return returnValue;
427 }
428 #endregion
429
430 String GetParameter(String parameter) 获得参数Sql表达式#region String GetParameter(String parameter) 获得参数Sql表达式
431 /**//// <summary>
432 /// 获得参数Sql表达式
433 /// </summary>
434 /// <param name="parameter">参数名称</param>
435 /// <returns>字符串</returns>
436 public String GetParameter(String parameter)
437 {
438 return " @" + parameter + " ";
439 }
440 #endregion
441
442 public String PlusSign() 字符串相加符号#region public String PlusSign() 字符串相加符号
443 /**//// <summary>
444 /// 字符串相加符号
445 /// </summary>
446 /// <returns>字符加</returns>
447 public String PlusSign()
448 {
449 return " + ";
450 }
451 #endregion
452
453
454 public IDataReader ExecuteReader(String sqlQuery)#region public IDataReader ExecuteReader(String sqlQuery)
455 /**//// <summary>
456 /// 执行查询
457 /// </summary>
458 /// <param name="sqlQuery">sql查询</param>
459 /// <returns>结果集流</returns>
460 public IDataReader ExecuteReader(String sqlQuery)
461 {
462 // 写入调试信息
463 #if (DEBUG)
464 int milliStart = Environment.TickCount;
465 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
466 #endif
467 this.mySqlCommand.CommandType = CommandType.Text;
468 this.mySqlCommand.CommandText = sqlQuery;
469 if (this.InTransaction)
470 {
471 this.mySqlCommand.Transaction = this.mySqlTransaction;
472 }
473 SqlDataReader mySqlDataReader = this.mySqlCommand.ExecuteReader();
474 // 写入调试信息
475 #if (DEBUG)
476 int milliEnd = Environment.TickCount;
477 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
478 #endif
479 // 写入日志
480 this.WriteLog(sqlQuery);
481 return mySqlDataReader;
482 }
483 #endregion
484
485 public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters);#region public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters);
486 /**//// <summary>
487 /// 执行查询
488 /// </summary>
489 /// <param name="sqlQuery">sql查询</param>
490 /// <param name="myDbParameterCollection">参数集</param>
491 /// <returns>结果集流</returns>
492 public IDataReader ExecuteReader(String sqlQuery, DbParameter[] myDbParameters)
493 {
494 return this.ExecuteReader(CommandType.Text, sqlQuery, myDbParameters);
495 }
496 #endregion
497
498 public IDataReader ExecuteReader(String sqlQuery, String name, Object value)#region public IDataReader ExecuteReader(String sqlQuery, String name, Object value)
499 /**//// <summary>
500 /// 执行查询
501 /// </summary>
502 /// <param name="myDataSet">数据集</param>
503 /// <param name="sqlQuery">sql查询</param>
504 /// <param name="name">参数名</param>
505 /// <param name="value">参数值</param>
506 /// <returns>结果集流</returns>
507 public IDataReader ExecuteReader(String sqlQuery, String name, Object value)
508 {
509 String[] names = new String[1] { name };
510 Object[] values = new Object[1] { value };
511 return this.ExecuteReader(CommandType.Text, sqlQuery, names, values);
512 }
513 #endregion
514
515 public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)#region public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)
516 /**//// <summary>
517 /// 执行查询
518 /// </summary>
519 /// <param name="sqlQuery">sql查询</param>
520 /// <param name="names">参数名</param>
521 /// <param name="values">参数值</param>
522 /// <returns>结果集流</returns>
523 public IDataReader ExecuteReader(String sqlQuery, String[] names, Object[] values)
524 {
525 return this.ExecuteReader(CommandType.Text, sqlQuery, names, values);
526 }
527 #endregion
528
529 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
530 /**//// <summary>
531 /// 执行查询
532 /// </summary>
533 /// <param name="myCommandType">命令分类</param>
534 /// <param name="sqlQuery">sql查询</param>
535 /// <param name="myDbParameterCollection">参数集</param>
536 /// <returns>结果集流</returns>
537 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
538 {
539 // 写入调试信息
540 #if (DEBUG)
541 int milliStart = Environment.TickCount;
542 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
543 #endif
544 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
545 this.mySqlCommand.CommandType = myCommandType;
546 if (mySqlTransaction != null)
547 {
548 this.mySqlCommand.Transaction = mySqlTransaction;
549 }
550 if (myDbParameters != null)
551 {
552 this.mySqlCommand.Parameters.Clear();
553 for (int i = 0; i < myDbParameters.Length; i++)
554 {
555 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
556 }
557 }
558 SqlDataReader mySqlDataReader = this.mySqlCommand.ExecuteReader();
559 this.mySqlCommand.Parameters.Clear();
560 // 写入调试信息
561 #if (DEBUG)
562 int milliEnd = Environment.TickCount;
563 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
564 #endif
565 // 写入日志
566 this.WriteLog(sqlQuery);
567 return mySqlDataReader;
568 }
569 #endregion
570
571 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
572 /**//// <summary>
573 /// 执行查询
574 /// </summary>
575 /// <param name="myCommandType">命令分类</param>
576 /// <param name="sqlQuery">sql查询</param>
577 /// <param name="names">参数名</param>
578 /// <param name="values">参数值</param>
579 /// <returns>结果集流</returns>
580 public IDataReader ExecuteReader(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
581 {
582 return this.ExecuteReader(myCommandType, sqlQuery, this.GetParameters(names, values)); ;
583 }
584 #endregion
585
586
587 public int ExecuteNonQuery(String sqlQuery)#region public int ExecuteNonQuery(String sqlQuery)
588 /**//// <summary>
589 /// 执行查询, SQL BUILDER 用了这个东西?参数需要保存, 不能丢失.
590 /// </summary>
591 /// <param name="sqlQuery">sql查询</param>
592 /// <returns>影响行数</returns>
593 public int ExecuteNonQuery(String sqlQuery)
594 {
595 // 写入调试信息
596 #if (DEBUG)
597 int milliStart = Environment.TickCount;
598 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
599 #endif
600 this.mySqlCommand.CommandType = CommandType.Text;
601 this.mySqlCommand.CommandText = sqlQuery;
602 if (this.InTransaction)
603 {
604 this.mySqlCommand.Transaction = this.mySqlTransaction;
605 }
606 int returnValue = this.mySqlCommand.ExecuteNonQuery();
607 // 写入调试信息
608 #if (DEBUG)
609 int milliEnd = Environment.TickCount;
610 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
611 #endif
612 // 写入日志
613 this.WriteLog(sqlQuery);
614 return returnValue;
615 }
616 #endregion
617
618 public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters);#region public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters);
619 /**//// <summary>
620 /// 执行查询
621 /// </summary>
622 /// <param name="sqlQuery">sql查询</param>
623 /// <param name="myDbParameterCollection">参数集</param>
624 /// <returns>影响行数</returns>
625 public int ExecuteNonQuery(String sqlQuery, DbParameter[] myDbParameters)
626 {
627 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, myDbParameters);
628 }
629 #endregion
630
631 public int ExecuteNonQuery(String sqlQuery, String name, Object value)#region public int ExecuteNonQuery(String sqlQuery, String name, Object value)
632 /**//// <summary>
633 /// 执行查询
634 /// </summary>
635 /// <param name="myDataSet">数据集</param>
636 /// <param name="sqlQuery">sql查询</param>
637 /// <param name="name">参数名</param>
638 /// <param name="value">参数值</param>
639 /// <returns>影响行数</returns>
640 public int ExecuteNonQuery(String sqlQuery, String name, Object value)
641 {
642 String[] names = new String[1] { name };
643 Object[] values = new Object[1] { value };
644 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, names, values);
645 }
646 #endregion
647
648 public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)
649 /**//// <summary>
650 /// 执行查询
651 /// </summary>
652 /// <param name="sqlQuery">sql查询</param>
653 /// <param name="names">参数名</param>
654 /// <param name="values">参数值</param>
655 /// <returns>影响行数</returns>
656 public int ExecuteNonQuery(String sqlQuery, String[] names, Object[] values)
657 {
658 return this.ExecuteNonQuery(CommandType.Text, sqlQuery, names, values);
659 }
660 #endregion
661
662 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
663 /**//// <summary>
664 /// 执行查询
665 /// </summary>
666 /// <param name="myCommandType">命令分类</param>
667 /// <param name="sqlQuery">sql查询</param>
668 /// <param name="myDbParameterCollection">参数集</param>
669 /// <returns>影响行数</returns>
670 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
671 {
672 SqlTransaction mySqlTransaction = null;
673 if (this.InTransaction)
674 {
675 mySqlTransaction = this.mySqlTransaction;
676 }
677 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, myDbParameters);
678 }
679 #endregion
680
681 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
682 /**//// <summary>
683 /// 执行查询
684 /// </summary>
685 /// <param name="myCommandType">命令分类</param>
686 /// <param name="sqlQuery">sql查询</param>
687 /// <param name="names">参数名</param>
688 /// <param name="values">参数值</param>
689 /// <returns>影响行数</returns>
690 public int ExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
691 {
692 SqlTransaction mySqlTransaction = null;
693 if (this.InTransaction)
694 {
695 mySqlTransaction = this.mySqlTransaction;
696 }
697 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, names, values);
698 }
699 #endregion
700
701 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
702 /**//// <summary>
703 /// 执行查询
704 /// </summary>
705 /// <param name="mySqlTransaction">数据库事务</param>
706 /// <param name="myCommandType">命令分类</param>
707 /// <param name="sqlQuery">sql查询</param>
708 /// <param name="myDbParameterCollection">参数集</param>
709 /// <returns>影响行数</returns>
710 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
711 {
712 // 写入调试信息
713 #if (DEBUG)
714 int milliStart = Environment.TickCount;
715 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
716 #endif
717 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
718 this.mySqlCommand.CommandType = myCommandType;
719 if (mySqlTransaction != null)
720 {
721 this.mySqlCommand.Transaction = (SqlTransaction)mySqlTransaction;
722 }
723 if (myDbParameters != null)
724 {
725 this.mySqlCommand.Parameters.Clear();
726 for (int i = 0; i < myDbParameters.Length; i++)
727 {
728 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
729 }
730 }
731 int returnValue = this.mySqlCommand.ExecuteNonQuery();
732 this.mySqlCommand.Parameters.Clear();
733 // 写入调试信息
734 #if (DEBUG)
735 int milliEnd = Environment.TickCount;
736 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
737 #endif
738 // 写入日志
739 this.WriteLog(sqlQuery);
740 return returnValue;
741 }
742 #endregion
743
744 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
745 /**//// <summary>
746 /// 执行查询
747 /// </summary>
748 /// <param name="mySqlTransaction">数据库事务</param>
749 /// <param name="myCommandType">命令分类</param>
750 /// <param name="sqlQuery">sql查询</param>
751 /// <param name="names">参数名</param>
752 /// <param name="values">参数值</param>
753 /// <returns>影响行数</returns>
754 public int ExecuteNonQuery(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
755 {
756 return this.ExecuteNonQuery(mySqlTransaction, myCommandType, sqlQuery, this.GetParameters(names, values));
757 }
758 #endregion
759
760
761 public Object ExecuteScalar(String sqlQuery)#region public Object ExecuteScalar(String sqlQuery)
762 /**//// <summary>
763 /// 执行查询
764 /// </summary>
765 /// <param name="sqlQuery">sql查询</param>
766 /// <returns>object</returns>
767 public Object ExecuteScalar(String sqlQuery)
768 {
769 return this.ExecuteScalar(CommandType.Text, sqlQuery, null, null);
770 }
771 #endregion
772
773 public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)
774 /**//// <summary>
775 /// 执行查询
776 /// </summary>
777 /// <param name="sqlQuery">sql查询</param>
778 /// <param name="myDbParameterCollection">参数集</param>
779 /// <returns>Object</returns>
780 public Object ExecuteScalar(String sqlQuery, DbParameter[] myDbParameters)
781 {
782 return this.ExecuteScalar(CommandType.Text, sqlQuery, myDbParameters);
783 }
784 #endregion
785
786 public Object ExecuteScalar(String sqlQuery, String name, Object value)#region public Object ExecuteScalar(String sqlQuery, String name, Object value)
787 /**//// <summary>
788 /// 执行查询
789 /// </summary>
790 /// <param name="sqlQuery">sql查询</param>
791 /// <param name="name">参数名</param>
792 /// <param name="value">参数值</param>
793 /// <returns>Object</returns>
794 public Object ExecuteScalar(DataSet myDataSet, String sqlQuery, String name, Object value)
795 {
796 String[] names = new String[1] { name };
797 Object[] values = new Object[1] { value };
798 return this.ExecuteScalar(CommandType.Text, sqlQuery, names, values);
799 }
800 #endregion
801
802 public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)
803 /**//// <summary>
804 /// 执行查询
805 /// </summary>
806 /// <param name="sqlQuery">sql查询</param>
807 /// <param name="names">参数名</param>
808 /// <param name="values">参数值</param>
809 /// <returns>影响行数</returns>
810 public Object ExecuteScalar(String sqlQuery, String[] names, Object[] values)
811 {
812 return this.ExecuteScalar(CommandType.Text, sqlQuery, names, values);
813 }
814 #endregion
815
816 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
817 /**//// <summary>
818 /// 执行查询
819 /// </summary>
820 /// <param name="myCommandType">命令分类</param>
821 /// <param name="sqlQuery">sql查询</param>
822 /// <param name="myDbParameterCollection">参数集</param>
823 /// <returns>Object</returns>
824 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
825 {
826 SqlTransaction mySqlTransaction = null;
827 if (this.InTransaction)
828 {
829 mySqlTransaction = this.mySqlTransaction;
830 }
831 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, myDbParameters);
832 }
833 #endregion
834
835 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
836 /**//// <summary>
837 /// 执行查询
838 /// </summary>
839 /// <param name="myCommandType">命令分类</param>
840 /// <param name="sqlQuery">sql查询</param>
841 /// <param name="names">参数名</param>
842 /// <param name="values">参数值</param>
843 /// <returns>影响行数</returns>
844 public Object ExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
845 {
846 SqlTransaction mySqlTransaction = null;
847 if (this.InTransaction)
848 {
849 mySqlTransaction = this.mySqlTransaction;
850 }
851 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, names, values);
852 }
853 #endregion
854
855 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
856 /**//// <summary>
857 /// 执行查询
858 /// </summary>
859 /// <param name="mySqlTransaction">数据库事务</param>
860 /// <param name="myCommandType">命令分类</param>
861 /// <param name="sqlQuery">sql查询</param>
862 /// <param name="myDbParameterCollection">参数集</param>
863 /// <returns>Object</returns>
864 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
865 {
866 // 写入调试信息
867 #if (DEBUG)
868 int milliStart = Environment.TickCount;
869 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
870 #endif
871 this.mySqlCommand = new SqlCommand(sqlQuery, this.mySqlConnection);
872 this.mySqlCommand.CommandType = myCommandType;
873 if (mySqlTransaction != null)
874 {
875 this.mySqlCommand.Transaction = (SqlTransaction)mySqlTransaction;
876 }
877 if (myDbParameters != null)
878 {
879 this.mySqlCommand.Parameters.Clear();
880 for (int i = 0; i < myDbParameters.Length; i++)
881 {
882 this.mySqlCommand.Parameters.Add(myDbParameters[i]);
883 }
884 }
885 Object returnValue = this.mySqlCommand.ExecuteScalar();
886 this.mySqlCommand.Parameters.Clear();
887 // 写入调试信息
888 #if (DEBUG)
889 int milliEnd = Environment.TickCount;
890 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
891 #endif
892 // 写入日志
893 this.WriteLog(sqlQuery);
894 return returnValue;
895 }
896 #endregion
897
898 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
899 /**//// <summary>
900 /// 执行查询
901 /// </summary>
902 /// <param name="mySqlTransaction">数据库事务</param>
903 /// <param name="myCommandType">命令分类</param>
904 /// <param name="sqlQuery">sql查询</param>
905 /// <param name="names"></param>
906 /// <param name="values"></param>
907 /// <returns>影响行数</returns>
908 public Object ExecuteScalar(IDbTransaction mySqlTransaction, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
909 {
910 return this.ExecuteScalar(mySqlTransaction, myCommandType, sqlQuery, this.GetParameters(names, values));
911 }
912 #endregion
913
914
915 public DataTable Fill(DataTable myDataTable, String sqlQuery)#region public DataTable Fill(DataTable myDataTable, String sqlQuery)
916 /**//// <summary>
917 /// 填充数据表
918 /// </summary>
919 /// <param name="myDataTable">目标数据表</param>
920 /// <param name="sqlQuery">查询</param>
921 /// <returns>数据表</returns>
922 public DataTable Fill(DataTable myDataTable, String sqlQuery)
923 {
924 return this.Fill(myDataTable, CommandType.Text, sqlQuery, null, null);
925 }
926 #endregion
927
928 public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)
929 /**//// <summary>
930 /// 填充数据表
931 /// </summary>
932 /// <param name="myDataTable">目标数据表</param>
933 /// <param name="sqlQuery">sql查询</param>
934 /// <param name="myDbParameterCollection">参数集</param>
935 /// <returns>数据表</returns>
936 public DataTable Fill(DataTable myDataTable, String sqlQuery, DbParameter[] myDbParameters)
937 {
938 return this.Fill(myDataTable, CommandType.Text, sqlQuery, myDbParameters);
939 }
940 #endregion
941
942 public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)
943 /**//// <summary>
944 /// 填充数据表
945 /// </summary>
946 /// <param name="myDataSet">目标数据表</param>
947 /// <param name="sqlQuery">sql查询</param>
948 /// <param name="name">参数名</param>
949 /// <param name="value">参数值</param>
950 /// <returns>数据表</returns>
951 public DataTable Fill(DataTable myDataTable, String sqlQuery, String name, Object value)
952 {
953 String[] names = new String[1] { name };
954 Object[] values = new Object[1] { value };
955 return this.Fill(myDataTable, CommandType.Text, sqlQuery, names, values);
956 }
957 #endregion
958
959 public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)#region public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)
960 /**//// <summary>
961 /// 填充数据表
962 /// </summary>
963 /// <param name="myDataSet">目标数据表</param>
964 /// <param name="sqlQuery">sql查询</param>
965 /// <param name="names">参数名</param>
966 /// <param name="values">参数值</param>
967 /// <returns>数据表</returns>
968 public DataTable Fill(DataTable myDataTable, String sqlQuery, String[] names, Object[] values)
969 {
970 return this.Fill(myDataTable, CommandType.Text, sqlQuery, names, values);
971 }
972 #endregion
973
974 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)#region public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
975 /**//// <summary>
976 /// 填充数据表
977 /// </summary>
978 /// <param name="myDataSet">目标数据表</param>
979 /// <param name="myCommandType">命令分类</param>
980 /// <param name="sqlQuery">sql查询</param>
981 /// <param name="myDbParameterCollection">参数集</param>
982 /// <returns>数据表</returns>
983 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, DbParameter[] myDbParameters)
984 {
985 // 写入调试信息
986 #if (DEBUG)
987 int milliStart = Environment.TickCount;
988 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
989 #endif
990 this.mySqlDataAdapter = new SqlDataAdapter(sqlQuery, this.mySqlConnection);
991 this.mySqlDataAdapter.SelectCommand.CommandType = myCommandType;
992 if (this.InTransaction)
993 {
994 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
995 }
996 if (myDbParameters != null)
997 {
998 this.mySqlCommand.Parameters.Clear();
999 for (int i = 0; i < myDbParameters.Length; i++)
1000 {
1001 this.mySqlDataAdapter.SelectCommand.Parameters.Add(myDbParameters[i]);
1002 }
1003 }
1004 this.mySqlDataAdapter.Fill(myDataTable);
1005 this.mySqlDataAdapter.SelectCommand.Parameters.Clear();
1006 // 写入调试信息
1007 #if (DEBUG)
1008 int milliEnd = Environment.TickCount;
1009 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1010 #endif
1011 // 写入日志
1012 this.WriteLog(sqlQuery);
1013 return myDataTable;
1014 }
1015 #endregion
1016
1017 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)#region public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1018 /**//// <summary>
1019 /// 填充数据表
1020 /// </summary>
1021 /// <param name="myDataSet">目标数据表</param>
1022 /// <param name="myCommandType">命令分类</param>
1023 /// <param name="sqlQuery">sql查询</param>
1024 /// <param name="names">参数名</param>
1025 /// <param name="values">参数值</param>
1026 /// <returns>数据表</returns>
1027 public DataTable Fill(DataTable myDataTable, CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1028 {
1029 return this.Fill(myDataTable, myCommandType, sqlQuery, this.GetParameters(names, values));
1030 }
1031 #endregion
1032
1033
1034 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)
1035 /**//// <summary>
1036 /// 填充数据集
1037 /// </summary>
1038 /// <param name="myDataSet">目标数据集</param>
1039 /// <param name="sqlQuery">查询</param>
1040 /// <param name="tableName">填充表</param>
1041 /// <returns>数据集</returns>
1042 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName)
1043 {
1044 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, null, null);
1045 }
1046 #endregion
1047
1048 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1049 /**//// <summary>
1050 /// 填充数据集
1051 /// </summary>
1052 /// <param name="myDataSet">数据集</param>
1053 /// <param name="sqlQuery">sql查询</param>
1054 /// <param name="tableName">填充表</param>
1055 /// <param name="myDbParameterCollection">参数集</param>
1056 /// <returns>数据集</returns>
1057 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1058 {
1059 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, myDbParameters);
1060 }
1061 #endregion
1062
1063 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)
1064 /**//// <summary>
1065 /// 填充数据集
1066 /// </summary>
1067 /// <param name="myDataSet">数据集</param>
1068 /// <param name="sqlQuery">sql查询</param>
1069 /// <param name="tableName">填充表</param>
1070 /// <param name="name">参数名</param>
1071 /// <param name="value">参数值</param>
1072 /// <returns>数据集</returns>
1073 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String name, Object value)
1074 {
1075 String[] names = new String[1] { name };
1076 Object[] values = new Object[1] { value };
1077 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, names, values);
1078 }
1079 #endregion
1080
1081 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)#region public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)
1082 /**//// <summary>
1083 /// 填充数据集
1084 /// </summary>
1085 /// <param name="myDataSet">数据集</param>
1086 /// <param name="sqlQuery">sql查询</param>
1087 /// <param name="tableName">填充表</param>
1088 /// <param name="names">参数名</param>
1089 /// <param name="values">参数值</param>
1090 /// <returns>数据集</returns>
1091 public DataSet Fill(DataSet myDataSet, String sqlQuery, String tableName, String[] names, Object[] values)
1092 {
1093 return this.Fill(myDataSet, CommandType.Text, sqlQuery, tableName, names, values);
1094 }
1095 #endregion
1096
1097 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)#region public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1098 /**//// <summary>
1099 /// 填充数据集
1100 /// </summary>
1101 /// <param name="myDataSet">数据集</param>
1102 /// <param name="myCommandType">命令分类</param>
1103 /// <param name="sqlQuery">sql查询</param>
1104 /// <param name="tableName">填充表</param>
1105 /// <param name="myDbParameterCollection">参数集</param>
1106 /// <returns>数据集</returns>
1107 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, DbParameter[] myDbParameters)
1108 {
1109 // 写入调试信息
1110 #if (DEBUG)
1111 int milliStart = Environment.TickCount;
1112 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1113 #endif
1114 this.mySqlDataAdapter = new SqlDataAdapter(sqlQuery, this.mySqlConnection);
1115 this.mySqlDataAdapter.SelectCommand.CommandType = myCommandType;
1116 if (this.InTransaction)
1117 {
1118 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
1119 }
1120 if (myDbParameters != null)
1121 {
1122 this.mySqlCommand.Parameters.Clear();
1123 for (int i = 0; i < myDbParameters.Length; i++)
1124 {
1125 this.mySqlDataAdapter.SelectCommand.Parameters.Add(myDbParameters[i]);
1126 }
1127 }
1128 this.mySqlDataAdapter.Fill(myDataSet, tableName);
1129 this.mySqlDataAdapter.SelectCommand.Parameters.Clear();
1130 // 写入调试信息
1131 #if (DEBUG)
1132 int milliEnd = Environment.TickCount;
1133 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1134 #endif
1135 // 写入日志
1136 this.WriteLog(sqlQuery);
1137 return myDataSet;
1138 }
1139 #endregion
1140
1141 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)#region public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)
1142 /**//// <summary>
1143 /// 填充数据集
1144 /// </summary>
1145 /// <param name="myDataSet">数据集</param>
1146 /// <param name="myCommandType">命令分类</param>
1147 /// <param name="sqlQuery">sql查询</param>
1148 /// <param name="tableName">填充表</param>
1149 /// <param name="names">参数名</param>
1150 /// <param name="values">参数值</param>
1151 /// <returns>数据集</returns>
1152 public DataSet Fill(DataSet myDataSet, CommandType myCommandType, String sqlQuery, String tableName, String[] names, Object[] values)
1153 {
1154 return this.Fill(myDataSet, myCommandType, sqlQuery, tableName, this.GetParameters(names, values));
1155 }
1156 #endregion
1157
1158
1159 public int ExecuteProcedure(String procedureName)#region public int ExecuteProcedure(String procedureName)
1160 /**//// <summary>
1161 /// 执行数据库查询
1162 /// </summary>
1163 /// <param name="procedureName">存储过程</param>
1164 /// <returns>int</returns>
1165 public int ExecuteProcedure(String procedureName)
1166 {
1167 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, null, null);
1168 }
1169 #endregion
1170
1171 public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)#region public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)
1172 /**//// <summary>
1173 /// 执行存储过程
1174 /// </summary>
1175 /// <param name="procedureName">存储过程名</param>
1176 /// <param name="myDbParameterCollection">参数集</param>
1177 /// <returns>影响行数</returns>
1178 public int ExecuteProcedure(String procedureName, DbParameter[] myDbParameters)
1179 {
1180 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, myDbParameters);
1181 }
1182 #endregion
1183
1184 public int ExecuteProcedure(String procedureName, String name, String value) 执行存储过程#region public int ExecuteProcedure(String procedureName, String name, String value) 执行存储过程
1185 /**//// <summary>
1186 /// 执行存储过程
1187 /// </summary>
1188 /// <param name="procedureName">存储过程名</param>
1189 /// <param name="name">参数名</param>
1190 /// <param name="value">参数值</param>
1191 /// <returns>影响行数</returns>
1192 public int ExecuteProcedure(String procedureName, String name, String value)
1193 {
1194 String[] names = new String[1];
1195 Object[] values = new Object[1];
1196 int returnValue = 0;
1197 names[0] = name;
1198 values[0] = value;
1199 returnValue = this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, names, values);
1200 return returnValue;
1201 }
1202 #endregion
1203
1204 public int ExecuteProcedure(String procedureName, String[] names, Object[] values)#region public int ExecuteProcedure(String procedureName, String[] names, Object[] values)
1205 /**//// <summary>
1206 /// 执行数据库脚本
1207 /// </summary>
1208 /// <param name="procedureName">存储过程</param>
1209 /// <param name="names">参数名</param>
1210 /// <param name="values">参数值</param>
1211 /// <returns>影响行数</returns>
1212 public int ExecuteProcedure(String procedureName, String[] names, Object[] values)
1213 {
1214 return this.ExecuteNonQuery(CommandType.StoredProcedure, procedureName, names, values);
1215 }
1216 #endregion
1217
1218 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)#region public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)
1219 /**//// <summary>
1220 /// 执行数据库脚本
1221 /// </summary>
1222 /// <param name="myDataSet">数据集</param>
1223 /// <param name="procedureName">存储过程</param>
1224 /// <param name="tableName">填充表</param>
1225 /// <param name="myDbParameterCollection">参数集</param>
1226 /// <returns>数据集</returns>
1227 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, DbParameter[] myDbParameters)
1228 {
1229 return this.Fill(myDataSet, CommandType.StoredProcedure, procedureName, tableName, myDbParameters);
1230 }
1231 #endregion
1232
1233 public DataSet ExecuteProcedureForDataSet(ref DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)#region public DataSet ExecuteProcedureForDataSet(ref DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)
1234 /**//// <summary>
1235 /// 执行数据库脚本
1236 /// </summary>
1237 /// <param name="myDataSet">数据集</param>
1238 /// <param name="procedureName">存储过程</param>
1239 /// <param name="tableName">填充表</param>
1240 /// <param name="names">参数名</param>
1241 /// <param name="values">参数值</param>
1242 /// <returns>影响行数</returns>
1243 public DataSet ExecuteProcedureForDataSet(DataSet myDataSet, String procedureName, String tableName, String[] names, Object[] values)
1244 {
1245 return this.Fill(myDataSet, CommandType.StoredProcedure, procedureName, tableName, names, values);
1246 }
1247 #endregion
1248
1249
1250 public IDbTransaction BeginTransaction()#region public IDbTransaction BeginTransaction()
1251 /**//// <summary>
1252 /// 事务开始
1253 /// </summary>
1254 /// <returns>事务</returns>
1255 public IDbTransaction BeginTransaction()
1256 {
1257 // 写入调试信息
1258 #if (DEBUG)
1259 int milliStart = Environment.TickCount;
1260 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1261 #endif
1262 if (!this.InTransaction)
1263 {
1264 this.InTransaction = true;
1265 this.mySqlTransaction = this.mySqlConnection.BeginTransaction();
1266 this.mySqlCommand.Transaction = this.mySqlTransaction;
1267 this.mySqlDataAdapter.SelectCommand.Transaction = this.mySqlTransaction;
1268 }
1269 // 写入调试信息
1270 #if (DEBUG)
1271 int milliEnd = Environment.TickCount;
1272 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1273 #endif
1274 return this.mySqlTransaction;
1275 }
1276 #endregion
1277
1278 public void CommitTransaction()#region public void CommitTransaction()
1279 /**//// <summary>
1280 /// 递交事务
1281 /// </summary>
1282 public void CommitTransaction()
1283 {
1284 // 写入调试信息
1285 #if (DEBUG)
1286 int milliStart = Environment.TickCount;
1287 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1288 #endif
1289 if (this.InTransaction)
1290 {
1291 // 事务已经完成了,一定要更新标志信息
1292 this.InTransaction = false;
1293 this.mySqlTransaction.Commit();
1294 }
1295 // 写入调试信息
1296 #if (DEBUG)
1297 int milliEnd = Environment.TickCount;
1298 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1299 #endif
1300 }
1301 #endregion
1302
1303 public void RollbackTransaction()#region public void RollbackTransaction()
1304 /**//// <summary>
1305 /// 回滚事务
1306 /// </summary>
1307 public void RollbackTransaction()
1308 {
1309 // 写入调试信息
1310 #if (DEBUG)
1311 int milliStart = Environment.TickCount;
1312 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1313 #endif
1314 if (this.InTransaction)
1315 {
1316 this.InTransaction = false;
1317 this.mySqlTransaction.Rollback();
1318 }
1319 // 写入调试信息
1320 #if (DEBUG)
1321 int milliEnd = Environment.TickCount;
1322 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1323 #endif
1324 }
1325 #endregion
1326
1327
1328 public delegate int Asyncdelegate();
1329
1330 private void CallbackMethod(IAsyncResult myIAsyncResult) 异步调用的挂接代理#region private void CallbackMethod(IAsyncResult myIAsyncResult) 异步调用的挂接代理
1331 /**//// <summary>
1332 /// 异步调用的挂接代理
1333 /// </summary>
1334 /// <param name="myIAsyncResult">回调</param>
1335 private void CallbackMethod(IAsyncResult myIAsyncResult)
1336 {
1337 Asyncdelegate myAsyncdelegate = (Asyncdelegate)myIAsyncResult.AsyncState;
1338 myAsyncdelegate.EndInvoke(myIAsyncResult);
1339 }
1340 #endregion
1341
1342 // 定义与方法同签名的委托
1343 private delegate int DelegateExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values);
1344
1345 private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1346 /**//// <summary>
1347 /// 异步调用数据库查询
1348 /// </summary>
1349 /// <param name="myCommandType">命令分类</param>
1350 /// <param name="sqlQuery">sql查询</param>
1351 /// <param name="names">参数名</param>
1352 /// <param name="values">参数值</param>
1353 /// <returns>影响行数</returns>
1354 private int AsyncExecuteNonQuery(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1355 {
1356 int returnValue = 0;
1357 this.Open();
1358 returnValue = this.ExecuteNonQuery(myCommandType, sqlQuery, names, values);
1359 this.Close();
1360 return returnValue;
1361 }
1362 #endregion
1363
1364 public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1365 /**//// <summary>
1366 /// 异步调用数据库查询
1367 /// </summary>
1368 /// <param name="myCommandType">命令分类</param>
1369 /// <param name="sqlQuery">sql查询</param>
1370 /// <param name="names">参数名</param>
1371 /// <param name="values">参数值</param>
1372 public void ExecuteNonQueryAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1373 {
1374 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1375
1376 DelegateExecuteNonQuery myDelegateExecuteNonQuery = new DelegateExecuteNonQuery(AsyncExecuteNonQuery);
1377 myDelegateExecuteNonQuery.BeginInvoke(myCommandType, sqlQuery, names, values, myAsyncCallback, null);
1378 }
1379 #endregion
1380
1381 // 定义与方法同签名的委托
1382 private delegate Object DelegateExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values);
1383
1384 private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1385 /**//// <summary>
1386 /// 异步调用数据库查询
1387 /// </summary>
1388 /// <param name="myCommandType">命令分类</param>
1389 /// <param name="sqlQuery">sql查询</param>
1390 /// <param name="names">参数名</param>
1391 /// <param name="values">参数值</param>
1392 /// <returns>对象</returns>
1393 private Object AsyncExecuteScalar(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1394 {
1395 Object returnValue = null;
1396 this.Open();
1397 returnValue = this.ExecuteScalar(myCommandType, sqlQuery, names, values);
1398 this.Close();
1399 return returnValue;
1400 }
1401 #endregion
1402
1403 public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values) 异步调用数据库查询
1404 /**//// <summary>
1405 /// 异步调用数据库查询
1406 /// </summary>
1407 /// <param name="myCommandType">命令分类</param>
1408 /// <param name="sqlQuery">sql查询</param>
1409 /// <param name="names">参数名</param>
1410 /// <param name="values">参数值</param>
1411 public void ExecuteScalarAsync(CommandType myCommandType, String sqlQuery, String[] names, Object[] values)
1412 {
1413 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1414
1415 DelegateExecuteScalar myDelegateExecuteScalar = new DelegateExecuteScalar(AsyncExecuteScalar);
1416 myDelegateExecuteScalar.BeginInvoke(myCommandType, sqlQuery, names, values, myAsyncCallback, null);
1417 }
1418 #endregion
1419
1420 // 定义与方法同签名的委托
1421 private delegate int DelegateExecuteProcedure(String procedureName, String[] names, Object[] values);
1422
1423 private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values) 异步调用数据库查询#region private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values) 异步调用数据库查询
1424 /**//// <summary>
1425 /// 异步调用数据库查询
1426 /// </summary>
1427 /// <param name="procedureName">存储过程</param>
1428 /// <param name="names">参数名</param>
1429 /// <param name="values">参数值</param>
1430 /// <returns>影响行数</returns>
1431 private int AsyncExecuteProcedure(String procedureName, String[] names, Object[] values)
1432 {
1433 int returnValue = 0;
1434 this.Open();
1435 returnValue = this.ExecuteProcedure(procedureName, names, values);
1436 this.Close();
1437 return returnValue;
1438 }
1439 #endregion
1440
1441 public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values) 异步调用数据库查询#region public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values) 异步调用数据库查询
1442 /**//// <summary>
1443 /// 异步调用数据库查询
1444 /// </summary>
1445 /// <param name="procedureName">存储过程</param>
1446 /// <param name="names">参数名</param>
1447 /// <param name="values">参数值</param>
1448 public void ExecuteProcedureAsync(String procedureName, String[] names, Object[] values)
1449 {
1450 AsyncCallback myAsyncCallback = new AsyncCallback(CallbackMethod);
1451
1452 DelegateExecuteProcedure myDelegateExecuteProcedure = new DelegateExecuteProcedure(AsyncExecuteProcedure);
1453 myDelegateExecuteProcedure.BeginInvoke(procedureName, names, values, myAsyncCallback, null);
1454 }
1455 #endregion
1456
1457
1458 public void WriteLog(String sqlQuery) 写入SQL查询语句日志#region public void WriteLog(String sqlQuery) 写入SQL查询语句日志
1459 /**//// <summary>
1460 /// 写入SQL查询语日志
1461 /// </summary>
1462 /// <param name="sqlQuery">SQL查询语</param>
1463 public void WriteLog(String sqlQuery)
1464 {
1465 this.WriteLog(DateTime.Now.ToString(BaseSystemInfo.DateFormat) + "_" + this.FileName, sqlQuery);
1466 // 将调试信息输出到屏幕上
1467 #if DEBUG
1468 System.Console.WriteLine(sqlQuery);
1469 #endif
1470 }
1471 #endregion
1472
1473 public void WriteLog(String fileName, String sqlQuery) 写入SQL查询语句日志#region public void WriteLog(String fileName, String sqlQuery) 写入SQL查询语句日志
1474 /**//// <summary>
1475 /// 写入SQL查询语句
1476 /// </summary>
1477 /// <param name="fileName">文件名</param>
1478 /// <param name="sqlQuery">异常</param>
1479 public void WriteLog(String fileName, String sqlQuery)
1480 {
1481 String returnValue = String.Empty;
1482 // 系统里应该可以配置是否记录异常现象
1483 if (!BaseSystemInfo.LogSQL)
1484 {
1485 return;
1486 }
1487 // 将异常信息写入本地文件中
1488 String logDirectory = BaseSystemInfo.StartupPath + @"\\Log\\Query";
1489 if (!System.IO.Directory.Exists(logDirectory))
1490 {
1491 System.IO.Directory.CreateDirectory(logDirectory);
1492 }
1493 String writerFileName = logDirectory + "\\" + fileName;
1494 if (!File.Exists(writerFileName))
1495 {
1496 FileStream myFileStream = new FileStream(writerFileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
1497 myFileStream.Close();
1498 }
1499 StreamWriter myStreamWriter = new StreamWriter(writerFileName, true, Encoding.Default);
1500 myStreamWriter.WriteLine(DateTime.Now.ToString(BaseSystemInfo.DateTimeFormat) + " " + sqlQuery);
1501 myStreamWriter.Close();
1502 }
1503 #endregion
1504
1505
1506 public void Close()#region public void Close()
1507 /**//// <summary>
1508 /// 关闭数据库连接
1509 /// </summary>
1510 public void Close()
1511 {
1512 // 写入调试信息
1513 #if (DEBUG)
1514 int milliStart = Environment.TickCount;
1515 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " :Begin: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1516 #endif
1517 if (this.mySqlConnection != null)
1518 {
1519 this.mySqlConnection.Close();
1520 this.mySqlConnection.Dispose();
1521 }
1522 // 写入调试信息
1523 #if (DEBUG)
1524 int milliEnd = Environment.TickCount;
1525 Trace.WriteLine(DateTime.Now.ToString(BaseSystemInfo.TimeFormat) + " Ticks: " + TimeSpan.FromMilliseconds(milliEnd - milliStart).ToString() + " :End: " + MethodBase.GetCurrentMethod().ReflectedType.Name + "." + MethodBase.GetCurrentMethod().Name);
1526 #endif
1527 }
1528 #endregion
1529
1530 private void Dispose(bool disposing)#region private void Dispose(bool disposing)
1531 /**//// <summary>
1532 /// 卸载
1533 /// </summary>
1534 /// <param name="disposing">卸载</param>
1535 private void Dispose(bool disposing)
1536 {
1537 if (disposing)
1538 {
1539 this.mySqlCommand.Dispose();
1540 this.mySqlDataAdapter.Dispose();
1541 this.mySqlTransaction.Dispose();
1542 this.mySqlConnection.Dispose();
1543 }
1544 }
1545 #endregion
1546
1547 public void Dispose()#region public void Dispose()
1548 /**//// <summary>
1549 /// 卸载
1550 /// </summary>
1551 public void Dispose()
1552 {
1553 Dispose(true);
1554 GC.SuppressFinalize(this);
1555 }
1556 #endregion
1557 }
1558}
将权限管理、工作流管理做到我能力的极致,一个人只能做好那么很少的几件事情。