快速拥有各种数据访问SqlHelper

Posted 黄子清

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速拥有各种数据访问SqlHelper相关的知识,希望对你有一定的参考价值。

常加班食不按时,偶得清闲嘴溃疡。

美食一顿成泡汤,自此自认忙命人。

 这就是此情此景的我,回来聊代码。

列举ADO.NET中的五个主要对象,并简单描述?

答:Connection连接对象,Command执行命令和存储过程,DataReader向前只读的数据流,DataAdapter适配器,支持增删查询,DataSet数据级对象,相当与内存里的一张或多张表。

还记得吗?它就是C#面试题精选100题之一。回头再看,是蛮重要。只要简单修改这个几个对象,分分钟钟拥有不同数据库访问Helper类。下面我们已mysql数据库为列。

首先去微软官网下载公开的SqlHelper。然后查找上面5个对象依次替换:

SqlConnection替换成MySqlConnection

SqlCommand 替换成 MySqlCommand

SqlDataReader 替换成 MySqlDataReader

SqlDataAdapter 替换成 MySqlDataAdapter

DataSet还是那个DataSet 就这样一个访问MySql数据库的Helper类完成。同理Order数据库替换 SqlConnection 为OrderConnection。

到这里可能有人会说,现在还搞这个已经过时了,用netframwork只要修改个配置就解决。条条大路通罗马。你说的没错。最后还是把替换后的代码分享出来。

   1 using System;
   2 using System.Collections;
   3 using System.Collections.Generic;
   4 using System.Configuration;
   5 using System.Data;
   6 using System.Linq;
   7 using System.Text;
   8 using System.Threading.Tasks;
   9 using System.Xml;
  10 using MySql.Data;
  11 using MySql.Data.MySqlClient;
  12 
  13 namespace MySqlDAL
  14 {
  15     public sealed class MySqlHelper
  16     {
  17         public static string connectionString = System.Configuration.ConfigurationManager.AppSettings["connectionString"];
  18         #region 私有构造函数和方法
  19 
  20         private MySqlHelper() { }
  21 
  22         /// <summary> 
  23         /// 将MySqlParameter参数数组(参数值)分配给MySqlCommand命令. 
  24         /// 这个方法将给任何一个参数分配DBNull.Value; 
  25         /// 该操作将阻止默认值的使用. 
  26         /// </summary> 
  27         /// <param name="command">命令名</param> 
  28         /// <param name="commandParameters">MySqlParameters数组</param> 
  29         private static void AttachParameters(MySqlCommand command, MySqlParameter[] commandParameters)
  30         {
  31             if (command == null) throw new ArgumentNullException("command");
  32             if (commandParameters != null)
  33             {
  34                 foreach (MySqlParameter p in commandParameters)
  35                 {
  36                     if (p != null)
  37                     {
  38                         // 检查未分配值的输出参数,将其分配以DBNull.Value. 
  39                         if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) &&
  40                             (p.Value == null))
  41                         {
  42                             p.Value = DBNull.Value;
  43                         }
  44                         command.Parameters.Add(p);
  45                     }
  46                 }
  47             }
  48         }
  49 
  50         /// <summary> 
  51         /// 将DataRow类型的列值分配到MySqlParameter参数数组. 
  52         /// </summary> 
  53         /// <param name="commandParameters">要分配值的MySqlParameter参数数组</param> 
  54         /// <param name="dataRow">将要分配给存储过程参数的DataRow</param> 
  55         private static void AssignParameterValues(MySqlParameter[] commandParameters, DataRow dataRow)
  56         {
  57             if ((commandParameters == null) || (dataRow == null))
  58             {
  59                 return;
  60             }
  61 
  62             int i = 0;
  63             // 设置参数值 
  64             foreach (MySqlParameter commandParameter in commandParameters)
  65             {
  66                 // 创建参数名称,如果不存在,只抛出一个异常. 
  67                 if (commandParameter.ParameterName == null ||
  68                     commandParameter.ParameterName.Length <= 1)
  69                     throw new Exception(
  70                         string.Format("请提供参数{0}一个有效的名称{1}.", i, commandParameter.ParameterName));
  71                 // 从dataRow的表中获取为参数数组中数组名称的列的索引. 
  72                 // 如果存在和参数名称相同的列,则将列值赋给当前名称的参数. 
  73                 if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1)
  74                     commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];
  75                 i++;
  76             }
  77         }
  78 
  79         /// <summary> 
  80         /// 将一个对象数组分配给MySqlParameter参数数组. 
  81         /// </summary> 
  82         /// <param name="commandParameters">要分配值的MySqlParameter参数数组</param> 
  83         /// <param name="parameterValues">将要分配给存储过程参数的对象数组</param> 
  84         private static void AssignParameterValues(MySqlParameter[] commandParameters, object[] parameterValues)
  85         {
  86             if ((commandParameters == null) || (parameterValues == null))
  87             {
  88                 return;
  89             }
  90 
  91             // 确保对象数组个数与参数个数匹配,如果不匹配,抛出一个异常. 
  92             if (commandParameters.Length != parameterValues.Length)
  93             {
  94                 throw new ArgumentException("参数值个数与参数不匹配.");
  95             }
  96 
  97             // 给参数赋值 
  98             for (int i = 0, j = commandParameters.Length; i < j; i++)
  99             {
 100                 // If the current array value derives from IDbDataParameter, then assign its Value property 
 101                 if (parameterValues[i] is IDbDataParameter)
 102                 {
 103                     IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];
 104                     if (paramInstance.Value == null)
 105                     {
 106                         commandParameters[i].Value = DBNull.Value;
 107                     }
 108                     else
 109                     {
 110                         commandParameters[i].Value = paramInstance.Value;
 111                     }
 112                 }
 113                 else if (parameterValues[i] == null)
 114                 {
 115                     commandParameters[i].Value = DBNull.Value;
 116                 }
 117                 else
 118                 {
 119                     commandParameters[i].Value = parameterValues[i];
 120                 }
 121             }
 122         }
 123 
 124         /// <summary> 
 125         /// 预处理用户提供的命令,数据库连接/事务/命令类型/参数 
 126         /// </summary> 
 127         /// <param name="command">要处理的MySqlCommand</param> 
 128         /// <param name="connection">数据库连接</param> 
 129         /// <param name="transaction">一个有效的事务或者是null值</param> 
 130         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 
 131         /// <param name="commandText">存储过程名或都T-SQL命令文本</param> 
 132         /// <param name="commandParameters">和命令相关联的MySqlParameter参数数组,如果没有参数为‘null‘</param> 
 133         /// <param name="mustCloseConnection"><c>true</c> 如果连接是打开的,则为true,其它情况下为false.</param> 
 134         private static void PrepareCommand(MySqlCommand command, MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, MySqlParameter[] commandParameters, out bool mustCloseConnection)
 135         {
 136             if (command == null) throw new ArgumentNullException("command"); 
 137             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
 138 
 139             // If the provided connection is not open, we will open it 
 140             if (connection.State != ConnectionState.Open)
 141             {
 142                 mustCloseConnection = true;
 143                 connection.Open();
 144             }
 145             else
 146             {
 147                 mustCloseConnection = false;
 148             }
 149 
 150             // 给命令分配一个数据库连接. 
 151             command.Connection = connection;
 152 
 153             // 设置命令文本(存储过程名或SQL语句) 
 154             command.CommandText = commandText;
 155 
 156             // 分配事务 
 157             if (transaction != null)
 158             {
 159                 if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 160                 command.Transaction = transaction;
 161             }
 162 
 163             // 设置命令类型. 
 164             command.CommandType = commandType;
 165 
 166             // 分配命令参数 
 167             if (commandParameters != null)
 168             {
 169                 AttachParameters(command, commandParameters);
 170             }
 171             return;
 172         }
 173 
 174         #endregion 私有构造函数和方法结束
 175 
 176         #region 数据库连接
 177         /// <summary> 
 178         /// 一个有效的数据库连接字符串 
 179         /// </summary> 
 180         /// <returns></returns> 
 181         public static string GetConnSting()
 182         {
 183             return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
 184         }
 185         /// <summary> 
 186         /// 一个有效的数据库连接对象 
 187         /// </summary> 
 188         /// <returns></returns> 
 189         public static MySqlConnection GetConnection()
 190         {
 191             MySqlConnection Connection = new MySqlConnection(MySqlHelper.GetConnSting());
 192             return Connection;
 193         }
 194         #endregion
 195 
 196         #region ExecuteNonQuery命令
 197 
 198         /// <summary> 
 199         /// 执行指定连接字符串,类型的MySqlCommand. 
 200         /// </summary> 
 201         /// <remarks> 
 202         /// 示例:  
 203         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders"); 
 204         /// </remarks> 
 205         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 206         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 
 207         /// <param name="commandText">存储过程名称或SQL语句</param> 
 208         /// <returns>返回命令影响的行数</returns> 
 209         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
 210         {
 211             return ExecuteNonQuery(connectionString, commandType, commandText, (MySqlParameter[])null);
 212         }
 213 
 214         /// <summary> 
 215         /// 执行指定连接字符串,类型的MySqlCommand.如果没有提供参数,不返回结果. 
 216         /// </summary> 
 217         /// <remarks> 
 218         /// 示例:  
 219         ///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 
 220         /// </remarks> 
 221         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 222         /// <param name="commandType">命令类型 (存储过程,命令文本, 其它.)</param> 
 223         /// <param name="commandText">存储过程名称或SQL语句</param> 
 224         /// <param name="commandParameters">MySqlParameter参数数组</param> 
 225         /// <returns>返回命令影响的行数</returns> 
 226         public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 227         {
 228             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 229 
 230             using (MySqlConnection connection = new MySqlConnection(connectionString))
 231             {
 232                 connection.Open();
 233 
 234                 return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
 235             }
 236         }
 237 
 238         /// <summary> 
 239         /// 执行指定连接字符串的存储过程,将对象数组的值赋给存储过程参数, 
 240         /// 此方法需要在参数缓存方法中探索参数并生成参数. 
 241         /// </summary> 
 242         /// <remarks> 
 243         /// 这个方法没有提供访问输出参数和返回值. 
 244         /// 示例:  
 245         ///  int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36); 
 246         /// </remarks> 
 247         /// <param name="connectionString">一个有效的数据库连接字符串/param> 
 248         /// <param name="spName">存储过程名称</param> 
 249         /// <param name="parameterValues">分配到存储过程输入参数的对象数组</param> 
 250         /// <returns>返回受影响的行数</returns> 
 251         public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
 252         {
 253             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 254             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 255 
 256             // 如果存在参数值 
 257             if ((parameterValues != null) && (parameterValues.Length > 0))
 258             {
 259                 // 从探索存储过程参数(加载到缓存)并分配给存储过程参数数组. 
 260                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 261 
 262                 // 给存储过程参数赋值 
 263                 AssignParameterValues(commandParameters, parameterValues);
 264 
 265                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 266             }
 267             else
 268             {
 269                 // 没有参数情况下 
 270                 return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
 271             }
 272         }
 273 
 274         /// <summary> 
 275         /// 执行指定数据库连接对象的命令 
 276         /// </summary> 
 277         /// <remarks> 
 278         /// 示例:  
 279         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders"); 
 280         /// </remarks> 
 281         /// <param name="connection">一个有效的数据库连接对象</param> 
 282         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 
 283         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 284         /// <returns>返回影响的行数</returns> 
 285         public static int ExecuteNonQuery(MySqlConnection connection, CommandType commandType, string commandText)
 286         {
 287             return ExecuteNonQuery(connection, commandType, commandText, (MySqlParameter[])null);
 288         }
 289 
 290         /// <summary> 
 291         /// 执行指定数据库连接对象的命令 
 292         /// </summary> 
 293         /// <remarks> 
 294         /// 示例:  
 295         ///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24)); 
 296         /// </remarks> 
 297         /// <param name="connection">一个有效的数据库连接对象</param> 
 298         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 
 299         /// <param name="commandText">T存储过程名称或T-SQL语句</param> 
 300         /// <param name="commandParameters">SqlParamter参数数组</param> 
 301         /// <returns>返回影响的行数</returns> 
 302         public static int ExecuteNonQuery(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 303         {
 304             if (connection == null) throw new ArgumentNullException("connection");
 305 
 306             // 创建MySqlCommand命令,并进行预处理 
 307             MySqlCommand cmd = new MySqlCommand();
 308             bool mustCloseConnection = false;
 309             PrepareCommand(cmd, connection, (MySqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
 310 
 311             // Finally, execute the command 
 312             int retval = cmd.ExecuteNonQuery();
 313 
 314             // 清除参数,以便再次使用. 
 315             cmd.Parameters.Clear();
 316             if (mustCloseConnection)
 317                 connection.Close();
 318             return retval;
 319         }
 320 
 321         /// <summary> 
 322         /// 执行指定数据库连接对象的命令,将对象数组的值赋给存储过程参数. 
 323         /// </summary> 
 324         /// <remarks> 
 325         /// 此方法不提供访问存储过程输出参数和返回值 
 326         /// 示例:  
 327         ///  int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36); 
 328         /// </remarks> 
 329         /// <param name="connection">一个有效的数据库连接对象</param> 
 330         /// <param name="spName">存储过程名</param> 
 331         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 332         /// <returns>返回影响的行数</returns> 
 333         public static int ExecuteNonQuery(MySqlConnection connection, string spName, params object[] parameterValues)
 334         {
 335             if (connection == null) throw new ArgumentNullException("connection");
 336             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 337 
 338             // 如果有参数值 
 339             if ((parameterValues != null) && (parameterValues.Length > 0))
 340             {
 341                 // 从缓存中加载存储过程参数 
 342                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
 343 
 344                 // 给存储过程分配参数值 
 345                 AssignParameterValues(commandParameters, parameterValues);
 346 
 347                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
 348             }
 349             else
 350             {
 351                 return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
 352             }
 353         }
 354 
 355         /// <summary> 
 356         /// 执行带事务的MySqlCommand. 
 357         /// </summary> 
 358         /// <remarks> 
 359         /// 示例.:  
 360         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders"); 
 361         /// </remarks> 
 362         /// <param name="transaction">一个有效的数据库连接对象</param> 
 363         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 
 364         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 365         /// <returns>返回影响的行数/returns> 
 366         public static int ExecuteNonQuery(MySqlTransaction transaction, CommandType commandType, string commandText)
 367         {
 368             return ExecuteNonQuery(transaction, commandType, commandText, (MySqlParameter[])null);
 369         }
 370 
 371         /// <summary> 
 372         /// 执行带事务的MySqlCommand(指定参数). 
 373         /// </summary> 
 374         /// <remarks> 
 375         /// 示例:  
 376         ///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 377         /// </remarks> 
 378         /// <param name="transaction">一个有效的数据库连接对象</param> 
 379         /// <param name="commandType">命令类型(存储过程,命令文本或其它.)</param> 
 380         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 381         /// <param name="commandParameters">SqlParamter参数数组</param> 
 382         /// <returns>返回影响的行数</returns> 
 383         public static int ExecuteNonQuery(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 384         {
 385             if (transaction == null) throw new ArgumentNullException("transaction");
 386             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 387 
 388             // 预处理 
 389             MySqlCommand cmd = new MySqlCommand();
 390             bool mustCloseConnection = false;
 391             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 392 
 393             // 执行 
 394             int retval = cmd.ExecuteNonQuery();
 395 
 396             // 清除参数集,以便再次使用. 
 397             cmd.Parameters.Clear();
 398             return retval;
 399         }
 400 
 401         /// <summary> 
 402         /// 执行带事务的MySqlCommand(指定参数值). 
 403         /// </summary> 
 404         /// <remarks> 
 405         /// 此方法不提供访问存储过程输出参数和返回值 
 406         /// 示例:  
 407         ///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36); 
 408         /// </remarks> 
 409         /// <param name="transaction">一个有效的数据库连接对象</param> 
 410         /// <param name="spName">存储过程名</param> 
 411         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 412         /// <returns>返回受影响的行数</returns> 
 413         public static int ExecuteNonQuery(MySqlTransaction transaction, string spName, params object[] parameterValues)
 414         {
 415             if (transaction == null) throw new ArgumentNullException("transaction");
 416             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 417             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 418 
 419             // 如果有参数值 
 420             if ((parameterValues != null) && (parameterValues.Length > 0))
 421             {
 422                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
 423                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 424 
 425                 // 给存储过程参数赋值 
 426                 AssignParameterValues(commandParameters, parameterValues);
 427 
 428                 // 调用重载方法 
 429                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
 430             }
 431             else
 432             {
 433                 // 没有参数值 
 434                 return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
 435             }
 436         }
 437 
 438         #endregion ExecuteNonQuery方法结束
 439 
 440         #region ExecuteDataset方法
 441 
 442         /// <summary> 
 443         /// 执行指定数据库连接字符串的命令,返回DataSet. 
 444         /// </summary> 
 445         /// <remarks> 
 446         /// 示例:  
 447         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders"); 
 448         /// </remarks> 
 449         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 450         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 451         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 452         /// <returns>返回一个包含结果集的DataSet</returns> 
 453         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
 454         { 
 455             return ExecuteDataset(connectionString, commandType, commandText, (MySqlParameter[])null);
 456         }
 457 
 458         /// <summary> 
 459         /// 执行指定数据库连接字符串的命令,返回DataSet. 
 460         /// </summary> 
 461         /// <remarks> 
 462         /// 示例: 
 463         ///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 464         /// </remarks> 
 465         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 466         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 467         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 468         /// <param name="commandParameters">SqlParamters参数数组</param> 
 469         /// <returns>返回一个包含结果集的DataSet</returns> 
 470         public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 471         {
 472             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 473 
 474             // 创建并打开数据库连接对象,操作完成释放对象. 
 475             using (MySqlConnection connection = new MySqlConnection(connectionString))
 476             {
 477                 connection.Open();
 478 
 479                 // 调用指定数据库连接字符串重载方法. 
 480                 return ExecuteDataset(connection, commandType, commandText, commandParameters);
 481             }
 482         }
 483 
 484         /// <summary> 
 485         /// 执行指定数据库连接字符串的命令,直接提供参数值,返回DataSet. 
 486         /// </summary> 
 487         /// <remarks> 
 488         /// 此方法不提供访问存储过程输出参数和返回值. 
 489         /// 示例: 
 490         ///  DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36); 
 491         /// </remarks> 
 492         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 493         /// <param name="spName">存储过程名</param> 
 494         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 495         /// <returns>返回一个包含结果集的DataSet</returns> 
 496         public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
 497         {
 498             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 499             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 500 
 501             if ((parameterValues != null) && (parameterValues.Length > 0))
 502             {
 503                 // 从缓存中检索存储过程参数 
 504                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 505 
 506                 // 给存储过程参数分配值 
 507                 AssignParameterValues(commandParameters, parameterValues);
 508 
 509                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 510             }
 511             else
 512             {
 513                 return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
 514             }
 515         }
 516 
 517         /// <summary> 
 518         /// 执行指定数据库连接对象的命令,返回DataSet. 
 519         /// </summary> 
 520         /// <remarks> 
 521         /// 示例:  
 522         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders"); 
 523         /// </remarks> 
 524         /// <param name="connection">一个有效的数据库连接对象</param> 
 525         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 526         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 527         /// <returns>返回一个包含结果集的DataSet</returns> 
 528         public static DataSet ExecuteDataset(MySqlConnection connection, CommandType commandType, string commandText)
 529         {
 530             return ExecuteDataset(connection, commandType, commandText, (MySqlParameter[])null);
 531         }
 532 
 533         /// <summary> 
 534         /// 执行指定数据库连接对象的命令,指定存储过程参数,返回DataSet. 
 535         /// </summary> 
 536         /// <remarks> 
 537         /// 示例:  
 538         ///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 539         /// </remarks> 
 540         /// <param name="connection">一个有效的数据库连接对象</param> 
 541         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 542         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 543         /// <param name="commandParameters">SqlParamter参数数组</param> 
 544         /// <returns>返回一个包含结果集的DataSet</returns> 
 545         public static DataSet ExecuteDataset(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 546         {
 547             if (connection == null) throw new ArgumentNullException("connection");
 548 
 549             // 预处理 
 550             MySqlCommand cmd = new MySqlCommand();
 551             bool mustCloseConnection = false;
 552             PrepareCommand(cmd, connection, (MySqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
 553 
 554             // 创建MySqlDataAdapter和DataSet. 
 555             using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
 556             {
 557                 DataSet ds = new DataSet();
 558 
 559                 // 填充DataSet. 
 560                 da.Fill(ds);
 561 
 562                 cmd.Parameters.Clear();
 563 
 564                 if (mustCloseConnection)
 565                     connection.Close();
 566 
 567                 return ds;
 568             }
 569         }
 570 
 571         /// <summary> 
 572         /// 执行指定数据库连接对象的命令,指定参数值,返回DataSet. 
 573         /// </summary> 
 574         /// <remarks> 
 575         /// 此方法不提供访问存储过程输入参数和返回值. 
 576         /// 示例.:  
 577         ///  DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36); 
 578         /// </remarks> 
 579         /// <param name="connection">一个有效的数据库连接对象</param> 
 580         /// <param name="spName">存储过程名</param> 
 581         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 582         /// <returns>返回一个包含结果集的DataSet</returns> 
 583         public static DataSet ExecuteDataset(MySqlConnection connection, string spName, params object[] parameterValues)
 584         {
 585             if (connection == null) throw new ArgumentNullException("connection");
 586             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 587 
 588             if ((parameterValues != null) && (parameterValues.Length > 0))
 589             {
 590                 // 比缓存中加载存储过程参数 
 591                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
 592 
 593                 // 给存储过程参数分配值 
 594                 AssignParameterValues(commandParameters, parameterValues);
 595 
 596                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
 597             }
 598             else
 599             {
 600                 return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
 601             }
 602         }
 603 
 604         /// <summary> 
 605         /// 执行指定事务的命令,返回DataSet. 
 606         /// </summary> 
 607         /// <remarks> 
 608         /// 示例:  
 609         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders"); 
 610         /// </remarks> 
 611         /// <param name="transaction">事务</param> 
 612         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 613         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 614         /// <returns>返回一个包含结果集的DataSet</returns> 
 615         public static DataSet ExecuteDataset(MySqlTransaction transaction, CommandType commandType, string commandText)
 616         {
 617             return ExecuteDataset(transaction, commandType, commandText, (MySqlParameter[])null);
 618         }
 619 
 620         /// <summary> 
 621         /// 执行指定事务的命令,指定参数,返回DataSet. 
 622         /// </summary> 
 623         /// <remarks> 
 624         /// 示例:  
 625         ///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 626         /// </remarks> 
 627         /// <param name="transaction">事务</param> 
 628         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 629         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 630         /// <param name="commandParameters">SqlParamter参数数组</param> 
 631         /// <returns>返回一个包含结果集的DataSet</returns> 
 632         public static DataSet ExecuteDataset(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 633         {
 634             if (transaction == null) throw new ArgumentNullException("transaction");
 635             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 636 
 637             // 预处理 
 638             MySqlCommand cmd = new MySqlCommand();
 639             bool mustCloseConnection = false;
 640             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 641 
 642             // 创建 DataAdapter & DataSet 
 643             using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
 644             {
 645                 DataSet ds = new DataSet();
 646                 da.Fill(ds);
 647                 cmd.Parameters.Clear();
 648                 return ds;
 649             }
 650         }
 651 
 652         /// <summary> 
 653         /// 执行指定事务的命令,指定参数值,返回DataSet. 
 654         /// </summary> 
 655         /// <remarks> 
 656         /// 此方法不提供访问存储过程输入参数和返回值. 
 657         /// 示例.:  
 658         ///  DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36); 
 659         /// </remarks> 
 660         /// <param name="transaction">事务</param> 
 661         /// <param name="spName">存储过程名</param> 
 662         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 663         /// <returns>返回一个包含结果集的DataSet</returns> 
 664         public static DataSet ExecuteDataset(MySqlTransaction transaction, string spName, params object[] parameterValues)
 665         {
 666             if (transaction == null) throw new ArgumentNullException("transaction");
 667             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 668             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 669 
 670             if ((parameterValues != null) && (parameterValues.Length > 0))
 671             {
 672                 // 从缓存中加载存储过程参数 
 673                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 674 
 675                 // 给存储过程参数分配值 
 676                 AssignParameterValues(commandParameters, parameterValues);
 677 
 678                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
 679             }
 680             else
 681             {
 682                 return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
 683             }
 684         }
 685 
 686         #endregion ExecuteDataset数据集命令结束
 687 
 688         #region ExecuteReader 数据阅读器
 689 
 690         /// <summary> 
 691         /// 枚举,标识数据库连接是由MySqlHelper提供还是由调用者提供 
 692         /// </summary> 
 693         private enum MySqlConnectionOwnership
 694         {
 695             /// <summary>由MySqlHelper提供连接</summary> 
 696             Internal,
 697             /// <summary>由调用者提供连接</summary> 
 698             External
 699         }
 700 
 701         /// <summary> 
 702         /// 执行指定数据库连接对象的数据阅读器. 
 703         /// </summary> 
 704         /// <remarks> 
 705         /// 如果是MySqlHelper打开连接,当连接关闭DataReader也将关闭. 
 706         /// 如果是调用都打开连接,DataReader由调用都管理. 
 707         /// </remarks> 
 708         /// <param name="connection">一个有效的数据库连接对象</param> 
 709         /// <param name="transaction">一个有效的事务,或者为 ‘null‘</param> 
 710         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 711         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 712         /// <param name="commandParameters">MySqlParameters参数数组,如果没有参数则为‘null‘</param> 
 713         /// <param name="connectionOwnership">标识数据库连接对象是由调用者提供还是由MySqlHelper提供</param> 
 714         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 715         private static MySqlDataReader ExecuteReader(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType, string commandText, MySqlParameter[] commandParameters, MySqlConnectionOwnership connectionOwnership)
 716         {
 717             if (connection == null) throw new ArgumentNullException("connection");
 718 
 719             bool mustCloseConnection = false;
 720             // 创建命令 
 721             MySqlCommand cmd = new MySqlCommand();
 722             try
 723             {
 724                 PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
 725 
 726                 // 创建数据阅读器 
 727                 MySqlDataReader dataReader;
 728 
 729                 if (connectionOwnership == MySqlConnectionOwnership.External)
 730                 {
 731                     dataReader = cmd.ExecuteReader();
 732                 }
 733                 else
 734                 {
 735                     dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
 736                 }
 737 
 738                 // 清除参数,以便再次使用.. 
 739                 // HACK: There is a problem here, the output parameter values are fletched 
 740                 // when the reader is closed, so if the parameters are detached from the command 
 741                 // then the SqlReader can磘 set its values. 
 742                 // When this happen, the parameters can磘 be used again in other command. 
 743                 bool canClear = true;
 744                 foreach (MySqlParameter commandParameter in cmd.Parameters)
 745                 {
 746                     if (commandParameter.Direction != ParameterDirection.Input)
 747                         canClear = false;
 748                 }
 749 
 750                 if (canClear)
 751                 {
 752                     cmd.Parameters.Clear();
 753                 }
 754 
 755                 return dataReader;
 756             }
 757             catch
 758             {
 759                 if (mustCloseConnection)
 760                     connection.Close();
 761                 throw;
 762             }
 763         }
 764 
 765         /// <summary> 
 766         /// 执行指定数据库连接字符串的数据阅读器. 
 767         /// </summary> 
 768         /// <remarks> 
 769         /// 示例:  
 770         ///  MySqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders"); 
 771         /// </remarks> 
 772         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 773         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 774         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 775         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 776         public static MySqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
 777         {
 778             return ExecuteReader(connectionString, commandType, commandText, (MySqlParameter[])null);
 779         }
 780 
 781         /// <summary> 
 782         /// 执行指定数据库连接字符串的数据阅读器,指定参数. 
 783         /// </summary> 
 784         /// <remarks> 
 785         /// 示例:  
 786         ///  MySqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 787         /// </remarks> 
 788         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 789         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 790         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 791         /// <param name="commandParameters">SqlParamter参数数组(new MySqlParameter("@prodid", 24))</param> 
 792         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 793         public static MySqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 794         {
 795             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 796             MySqlConnection connection = null;
 797             try
 798             {
 799                 connection = new MySqlConnection(connectionString);
 800                 connection.Open();
 801 
 802                 return ExecuteReader(connection, null, commandType, commandText, commandParameters, MySqlConnectionOwnership.Internal);
 803             }
 804             catch
 805             {
 806                 // If we fail to return the SqlDatReader, we need to close the connection ourselves 
 807                 if (connection != null) connection.Close();
 808                 throw;
 809             }
 810 
 811         }
 812 
 813         /// <summary> 
 814         /// 执行指定数据库连接字符串的数据阅读器,指定参数值. 
 815         /// </summary> 
 816         /// <remarks> 
 817         /// 此方法不提供访问存储过程输出参数和返回值参数. 
 818         /// 示例:  
 819         ///  MySqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36); 
 820         /// </remarks> 
 821         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 822         /// <param name="spName">存储过程名</param> 
 823         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 824         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 825         public static MySqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
 826         {
 827             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
 828             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 829 
 830             if ((parameterValues != null) && (parameterValues.Length > 0))
 831             {
 832                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
 833 
 834                 AssignParameterValues(commandParameters, parameterValues);
 835 
 836                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
 837             }
 838             else
 839             {
 840                 return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
 841             }
 842         }
 843 
 844         /// <summary> 
 845         /// 执行指定数据库连接对象的数据阅读器. 
 846         /// </summary> 
 847         /// <remarks> 
 848         /// 示例:  
 849         ///  MySqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders"); 
 850         /// </remarks> 
 851         /// <param name="connection">一个有效的数据库连接对象</param> 
 852         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 853         /// <param name="commandText">存储过程名或T-SQL语句</param> 
 854         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 855         public static MySqlDataReader ExecuteReader(MySqlConnection connection, CommandType commandType, string commandText)
 856         {
 857             return ExecuteReader(connection, commandType, commandText, (MySqlParameter[])null);
 858         }
 859 
 860         /// <summary> 
 861         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数. 
 862         /// </summary> 
 863         /// <remarks> 
 864         /// 示例:  
 865         ///  MySqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 866         /// </remarks> 
 867         /// <param name="connection">一个有效的数据库连接对象</param> 
 868         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 869         /// <param name="commandText">命令类型 (存储过程,命令文本或其它)</param> 
 870         /// <param name="commandParameters">SqlParamter参数数组</param> 
 871         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 872         public static MySqlDataReader ExecuteReader(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 873         {
 874             return ExecuteReader(connection, (MySqlTransaction)null, commandType, commandText, commandParameters, MySqlConnectionOwnership.External);
 875         }
 876 
 877         /// <summary> 
 878         /// [调用者方式]执行指定数据库连接对象的数据阅读器,指定参数值. 
 879         /// </summary> 
 880         /// <remarks> 
 881         /// 此方法不提供访问存储过程输出参数和返回值参数. 
 882         /// 示例:  
 883         ///  MySqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36); 
 884         /// </remarks> 
 885         /// <param name="connection">一个有效的数据库连接对象</param> 
 886         /// <param name="spName">T存储过程名</param> 
 887         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 888         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 889         public static MySqlDataReader ExecuteReader(MySqlConnection connection, string spName, params object[] parameterValues)
 890         {
 891             if (connection == null) throw new ArgumentNullException("connection");
 892             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 893 
 894             if ((parameterValues != null) && (parameterValues.Length > 0))
 895             {
 896                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
 897 
 898                 AssignParameterValues(commandParameters, parameterValues);
 899 
 900                 return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
 901             }
 902             else
 903             {
 904                 return ExecuteReader(connection, CommandType.StoredProcedure, spName);
 905             }
 906         }
 907 
 908         /// <summary> 
 909         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. 
 910         /// </summary> 
 911         /// <remarks> 
 912         /// 示例:  
 913         ///  MySqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders"); 
 914         /// </remarks> 
 915         /// <param name="transaction">一个有效的连接事务</param> 
 916         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 917         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 918         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 919         public static MySqlDataReader ExecuteReader(MySqlTransaction transaction, CommandType commandType, string commandText)
 920         {
 921             return ExecuteReader(transaction, commandType, commandText, (MySqlParameter[])null);
 922         }
 923 
 924         /// <summary> 
 925         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数. 
 926         /// </summary> 
 927         /// <remarks> 
 928         /// 示例:  
 929         ///   MySqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new MySqlParameter("@prodid", 24)); 
 930         /// </remarks> 
 931         /// <param name="transaction">一个有效的连接事务</param> 
 932         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 933         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 934         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
 935         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 936         public static MySqlDataReader ExecuteReader(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
 937         {
 938             if (transaction == null) throw new ArgumentNullException("transaction");
 939             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 940 
 941             return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, MySqlConnectionOwnership.External);
 942         }
 943 
 944         /// <summary> 
 945         /// [调用者方式]执行指定数据库事务的数据阅读器,指定参数值. 
 946         /// </summary> 
 947         /// <remarks> 
 948         /// 此方法不提供访问存储过程输出参数和返回值参数. 
 949         /// 
 950         /// 示例:  
 951         ///  MySqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36); 
 952         /// </remarks> 
 953         /// <param name="transaction">一个有效的连接事务</param> 
 954         /// <param name="spName">存储过程名称</param> 
 955         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
 956         /// <returns>返回包含结果集的MySqlDataReader</returns> 
 957         public static MySqlDataReader ExecuteReader(MySqlTransaction transaction, string spName, params object[] parameterValues)
 958         {
 959             if (transaction == null) throw new ArgumentNullException("transaction");
 960             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
 961             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
 962 
 963             // 如果有参数值 
 964             if ((parameterValues != null) && (parameterValues.Length > 0))
 965             {
 966                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
 967 
 968                 AssignParameterValues(commandParameters, parameterValues);
 969 
 970                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
 971             }
 972             else
 973             {
 974                 // 没有参数值 
 975                 return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
 976             }
 977         }
 978 
 979         #endregion ExecuteReader数据阅读器
 980 
 981         #region ExecuteScalar 返回结果集中的第一行第一列
 982 
 983         /// <summary> 
 984         /// 执行指定数据库连接字符串的命令,返回结果集中的第一行第一列. 
 985         /// </summary> 
 986         /// <remarks> 
 987         /// 示例:  
 988         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount"); 
 989         /// </remarks> 
 990         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
 991         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
 992         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
 993         /// <returns>返回结果集中的第一行第一列</returns> 
 994         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
 995         {
 996             // 执行参数为空的方法 
 997             return ExecuteScalar(connectionString, commandType, commandText, (MySqlParameter[])null);
 998         }
 999 
1000         /// <summary> 
1001         /// 执行指定数据库连接字符串的命令,指定参数,返回结果集中的第一行第一列. 
1002         /// </summary> 
1003         /// <remarks> 
1004         /// 示例:  
1005         ///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new MySqlParameter("@prodid", 24)); 
1006         /// </remarks> 
1007         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1008         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1009         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1010         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1011         /// <returns>返回结果集中的第一行第一列</returns> 
1012         public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
1013         {
1014             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1015             // 创建并打开数据库连接对象,操作完成释放对象. 
1016             using (MySqlConnection connection = new MySqlConnection(connectionString))
1017             {
1018                 connection.Open();
1019 
1020                 // 调用指定数据库连接字符串重载方法. 
1021                 return ExecuteScalar(connection, commandType, commandText, commandParameters);
1022             }
1023         }
1024 
1025         /// <summary> 
1026         /// 执行指定数据库连接字符串的命令,指定参数值,返回结果集中的第一行第一列. 
1027         /// </summary> 
1028         /// <remarks> 
1029         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1030         /// 
1031         /// 示例:  
1032         ///  int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36); 
1033         /// </remarks> 
1034         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1035         /// <param name="spName">存储过程名称</param> 
1036         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1037         /// <returns>返回结果集中的第一行第一列</returns> 
1038         public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
1039         {
1040             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1041             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1042 
1043             // 如果有参数值 
1044             if ((parameterValues != null) && (parameterValues.Length > 0))
1045             {
1046                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1047                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1048 
1049                 // 给存储过程参数赋值 
1050                 AssignParameterValues(commandParameters, parameterValues);
1051 
1052                 // 调用重载方法 
1053                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1054             }
1055             else
1056             {
1057                 // 没有参数值 
1058                 return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
1059             }
1060         }
1061 
1062         /// <summary> 
1063         /// 执行指定数据库连接对象的命令,返回结果集中的第一行第一列. 
1064         /// </summary> 
1065         /// <remarks> 
1066         /// 示例:  
1067         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount"); 
1068         /// </remarks> 
1069         /// <param name="connection">一个有效的数据库连接对象</param> 
1070         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1071         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1072         /// <returns>返回结果集中的第一行第一列</returns> 
1073         public static object ExecuteScalar(MySqlConnection connection, CommandType commandType, string commandText)
1074         {
1075             // 执行参数为空的方法 
1076             return ExecuteScalar(connection, commandType, commandText, (MySqlParameter[])null);
1077         }
1078 
1079         /// <summary> 
1080         /// 执行指定数据库连接对象的命令,指定参数,返回结果集中的第一行第一列. 
1081         /// </summary> 
1082         /// <remarks> 
1083         /// 示例:  
1084         ///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new MySqlParameter("@prodid", 24)); 
1085         /// </remarks> 
1086         /// <param name="connection">一个有效的数据库连接对象</param> 
1087         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1088         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1089         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1090         /// <returns>返回结果集中的第一行第一列</returns> 
1091         public static object ExecuteScalar(MySqlConnection connection, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
1092         {
1093             if (connection == null) throw new ArgumentNullException("connection");
1094 
1095             // 创建MySqlCommand命令,并进行预处理 
1096             MySqlCommand cmd = new MySqlCommand();
1097 
1098             bool mustCloseConnection = false;
1099             PrepareCommand(cmd, connection, (MySqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
1100 
1101             // 执行MySqlCommand命令,并返回结果. 
1102             object retval = cmd.ExecuteScalar();
1103 
1104             // 清除参数,以便再次使用. 
1105             cmd.Parameters.Clear();
1106 
1107             if (mustCloseConnection)
1108                 connection.Close();
1109 
1110             return retval;
1111         }
1112 
1113         /// <summary> 
1114         /// 执行指定数据库连接对象的命令,指定参数值,返回结果集中的第一行第一列. 
1115         /// </summary> 
1116         /// <remarks> 
1117         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1118         /// 
1119         /// 示例:  
1120         ///  int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36); 
1121         /// </remarks> 
1122         /// <param name="connection">一个有效的数据库连接对象</param> 
1123         /// <param name="spName">存储过程名称</param> 
1124         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1125         /// <returns>返回结果集中的第一行第一列</returns> 
1126         public static object ExecuteScalar(MySqlConnection connection, string spName, params object[] parameterValues)
1127         {
1128             if (connection == null) throw new ArgumentNullException("connection");
1129             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1130 
1131             // 如果有参数值 
1132             if ((parameterValues != null) && (parameterValues.Length > 0))
1133             {
1134                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1135                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1136 
1137                 // 给存储过程参数赋值 
1138                 AssignParameterValues(commandParameters, parameterValues);
1139 
1140                 // 调用重载方法 
1141                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
1142             }
1143             else
1144             {
1145                 // 没有参数值 
1146                 return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
1147             }
1148         }
1149 
1150         /// <summary> 
1151         /// 执行指定数据库事务的命令,返回结果集中的第一行第一列. 
1152         /// </summary> 
1153         /// <remarks> 
1154         /// 示例:  
1155         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount"); 
1156         /// </remarks> 
1157         /// <param name="transaction">一个有效的连接事务</param> 
1158         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1159         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1160         /// <returns>返回结果集中的第一行第一列</returns> 
1161         public static object ExecuteScalar(MySqlTransaction transaction, CommandType commandType, string commandText)
1162         {
1163             // 执行参数为空的方法 
1164             return ExecuteScalar(transaction, commandType, commandText, (MySqlParameter[])null);
1165         }
1166 
1167         /// <summary> 
1168         /// 执行指定数据库事务的命令,指定参数,返回结果集中的第一行第一列. 
1169         /// </summary> 
1170         /// <remarks> 
1171         /// 示例:  
1172         ///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new MySqlParameter("@prodid", 24)); 
1173         /// </remarks> 
1174         /// <param name="transaction">一个有效的连接事务</param> 
1175         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1176         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1177         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1178         /// <returns>返回结果集中的第一行第一列</returns> 
1179         public static object ExecuteScalar(MySqlTransaction transaction, CommandType commandType, string commandText, params MySqlParameter[] commandParameters)
1180         {
1181             if (transaction == null) throw new ArgumentNullException("transaction");
1182             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1183 
1184             // 创建MySqlCommand命令,并进行预处理 
1185             MySqlCommand cmd = new MySqlCommand();
1186             bool mustCloseConnection = false;
1187             PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1188 
1189             // 执行MySqlCommand命令,并返回结果. 
1190             object retval = cmd.ExecuteScalar();
1191 
1192             // 清除参数,以便再次使用. 
1193             cmd.Parameters.Clear();
1194             return retval;
1195         }
1196 
1197         /// <summary> 
1198         /// 执行指定数据库事务的命令,指定参数值,返回结果集中的第一行第一列. 
1199         /// </summary> 
1200         /// <remarks> 
1201         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1202         /// 
1203         /// 示例:  
1204         ///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36); 
1205         /// </remarks> 
1206         /// <param name="transaction">一个有效的连接事务</param> 
1207         /// <param name="spName">存储过程名称</param> 
1208         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1209         /// <returns>返回结果集中的第一行第一列</returns> 
1210         public static object ExecuteScalar(MySqlTransaction transaction, string spName, params object[] parameterValues)
1211         {
1212             if (transaction == null) throw new ArgumentNullException("transaction");
1213             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1214             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1215 
1216             // 如果有参数值 
1217             if ((parameterValues != null) && (parameterValues.Length > 0))
1218             {
1219                 // PPull the parameters for this stored procedure from the parameter cache () 
1220                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1221 
1222                 // 给存储过程参数赋值 
1223                 AssignParameterValues(commandParameters, parameterValues);
1224 
1225                 // 调用重载方法 
1226                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
1227             }
1228             else
1229             {
1230                 // 没有参数值 
1231                 return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
1232             }
1233         }
1234 
1235         #endregion ExecuteScalar
1236 
1237         #region FillDataset 填充数据集
1238         /// <summary> 
1239         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集. 
1240         /// </summary> 
1241         /// <remarks> 
1242         /// 示例:  
1243         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 
1244         /// </remarks> 
1245         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1246         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1247         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1248         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1249         /// <param name="tableNames">表映射的数据表数组 
1250         /// 用户定义的表名 (可有是实际的表名.)</param> 
1251         public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)
1252         {
1253             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1254             if (dataSet == null) throw new ArgumentNullException("dataSet");
1255 
1256             // 创建并打开数据库连接对象,操作完成释放对象. 
1257             using (MySqlConnection connection = new MySqlConnection(connectionString))
1258             {
1259                 connection.Open();
1260 
1261                 // 调用指定数据库连接字符串重载方法. 
1262                 FillDataset(connection, commandType, commandText, dataSet, tableNames);
1263             }
1264         }
1265 
1266         /// <summary> 
1267         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集.指定命令参数. 
1268         /// </summary> 
1269         /// <remarks> 
1270         /// 示例:  
1271         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new MySqlParameter("@prodid", 24)); 
1272         /// </remarks> 
1273         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1274         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1275         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1276         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1277         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1278         /// <param name="tableNames">表映射的数据表数组 
1279         /// 用户定义的表名 (可有是实际的表名.) 
1280         /// </param> 
1281         public static void FillDataset(string connectionString, CommandType commandType,
1282             string commandText, DataSet dataSet, string[] tableNames,
1283             params MySqlParameter[] commandParameters)
1284         {
1285             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1286             if (dataSet == null) throw new ArgumentNullException("dataSet");
1287             // 创建并打开数据库连接对象,操作完成释放对象. 
1288             using (MySqlConnection connection = new MySqlConnection(connectionString))
1289             {
1290                 connection.Open();
1291 
1292                 // 调用指定数据库连接字符串重载方法. 
1293                 FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);
1294             }
1295         }
1296 
1297         /// <summary> 
1298         /// 执行指定数据库连接字符串的命令,映射数据表并填充数据集,指定存储过程参数值. 
1299         /// </summary> 
1300         /// <remarks> 
1301         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1302         /// 
1303         /// 示例:  
1304         ///  FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24); 
1305         /// </remarks> 
1306         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1307         /// <param name="spName">存储过程名称</param> 
1308         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1309         /// <param name="tableNames">表映射的数据表数组 
1310         /// 用户定义的表名 (可有是实际的表名.) 
1311         /// </param>    
1312         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1313         public static void FillDataset(string connectionString, string spName,
1314             DataSet dataSet, string[] tableNames,
1315             params object[] parameterValues)
1316         {
1317             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1318             if (dataSet == null) throw new ArgumentNullException("dataSet");
1319             // 创建并打开数据库连接对象,操作完成释放对象. 
1320             using (MySqlConnection connection = new MySqlConnection(connectionString))
1321             {
1322                 connection.Open();
1323 
1324                 // 调用指定数据库连接字符串重载方法. 
1325                 FillDataset(connection, spName, dataSet, tableNames, parameterValues);
1326             }
1327         }
1328 
1329         /// <summary> 
1330         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集. 
1331         /// </summary> 
1332         /// <remarks> 
1333         /// 示例:  
1334         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 
1335         /// </remarks> 
1336         /// <param name="connection">一个有效的数据库连接对象</param> 
1337         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1338         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1339         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1340         /// <param name="tableNames">表映射的数据表数组 
1341         /// 用户定义的表名 (可有是实际的表名.) 
1342         /// </param>    
1343         public static void FillDataset(MySqlConnection connection, CommandType commandType,
1344             string commandText, DataSet dataSet, string[] tableNames)
1345         {
1346             FillDataset(connection, commandType, commandText, dataSet, tableNames, null);
1347         }
1348 
1349         /// <summary> 
1350         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定参数. 
1351         /// </summary> 
1352         /// <remarks> 
1353         /// 示例:  
1354         ///  FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new MySqlParameter("@prodid", 24)); 
1355         /// </remarks> 
1356         /// <param name="connection">一个有效的数据库连接对象</param> 
1357         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1358         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1359         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1360         /// <param name="tableNames">表映射的数据表数组 
1361         /// 用户定义的表名 (可有是实际的表名.) 
1362         /// </param> 
1363         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1364         public static void FillDataset(MySqlConnection connection, CommandType commandType,
1365             string commandText, DataSet dataSet, string[] tableNames,
1366             params MySqlParameter[] commandParameters)
1367         {
1368             FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);
1369         }
1370 
1371         /// <summary> 
1372         /// 执行指定数据库连接对象的命令,映射数据表并填充数据集,指定存储过程参数值. 
1373         /// </summary> 
1374         /// <remarks> 
1375         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1376         /// 
1377         /// 示例:  
1378         ///  FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36); 
1379         /// </remarks> 
1380         /// <param name="connection">一个有效的数据库连接对象</param> 
1381         /// <param name="spName">存储过程名称</param> 
1382         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1383         /// <param name="tableNames">表映射的数据表数组 
1384         /// 用户定义的表名 (可有是实际的表名.) 
1385         /// </param> 
1386         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1387         public static void FillDataset(MySqlConnection connection, string spName,
1388             DataSet dataSet, string[] tableNames,
1389             params object[] parameterValues)
1390         {
1391             if (connection == null) throw new ArgumentNullException("connection");
1392             if (dataSet == null) throw new ArgumentNullException("dataSet");
1393             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1394 
1395             // 如果有参数值 
1396             if ((parameterValues != null) && (parameterValues.Length > 0))
1397             {
1398                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1399                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1400 
1401                 // 给存储过程参数赋值 
1402                 AssignParameterValues(commandParameters, parameterValues);
1403 
1404                 // 调用重载方法 
1405                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1406             }
1407             else
1408             {
1409                 // 没有参数值 
1410                 FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);
1411             }
1412         }
1413 
1414         /// <summary> 
1415         /// 执行指定数据库事务的命令,映射数据表并填充数据集. 
1416         /// </summary> 
1417         /// <remarks> 
1418         /// 示例:  
1419         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}); 
1420         /// </remarks> 
1421         /// <param name="transaction">一个有效的连接事务</param> 
1422         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1423         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1424         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1425         /// <param name="tableNames">表映射的数据表数组 
1426         /// 用户定义的表名 (可有是实际的表名.) 
1427         /// </param> 
1428         public static void FillDataset(MySqlTransaction transaction, CommandType commandType,
1429             string commandText,
1430             DataSet dataSet, string[] tableNames)
1431         {
1432             FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);
1433         }
1434 
1435         /// <summary> 
1436         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定参数. 
1437         /// </summary> 
1438         /// <remarks> 
1439         /// 示例:  
1440         ///  FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new MySqlParameter("@prodid", 24)); 
1441         /// </remarks> 
1442         /// <param name="transaction">一个有效的连接事务</param> 
1443         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1444         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1445         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1446         /// <param name="tableNames">表映射的数据表数组 
1447         /// 用户定义的表名 (可有是实际的表名.) 
1448         /// </param> 
1449         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1450         public static void FillDataset(MySqlTransaction transaction, CommandType commandType,
1451             string commandText, DataSet dataSet, string[] tableNames,
1452             params MySqlParameter[] commandParameters)
1453         {
1454             FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);
1455         }
1456 
1457         /// <summary> 
1458         /// 执行指定数据库事务的命令,映射数据表并填充数据集,指定存储过程参数值. 
1459         /// </summary> 
1460         /// <remarks> 
1461         /// 此方法不提供访问存储过程输出参数和返回值参数. 
1462         /// 
1463         /// 示例:  
1464         ///  FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36); 
1465         /// </remarks> 
1466         /// <param name="transaction">一个有效的连接事务</param> 
1467         /// <param name="spName">存储过程名称</param> 
1468         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1469         /// <param name="tableNames">表映射的数据表数组 
1470         /// 用户定义的表名 (可有是实际的表名.) 
1471         /// </param> 
1472         /// <param name="parameterValues">分配给存储过程输入参数的对象数组</param> 
1473         public static void FillDataset(MySqlTransaction transaction, string spName,
1474             DataSet dataSet, string[] tableNames,
1475             params object[] parameterValues)
1476         {
1477             if (transaction == null) throw new ArgumentNullException("transaction");
1478             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1479             if (dataSet == null) throw new ArgumentNullException("dataSet");
1480             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1481 
1482             // 如果有参数值 
1483             if ((parameterValues != null) && (parameterValues.Length > 0))
1484             {
1485                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1486                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1487 
1488                 // 给存储过程参数赋值 
1489                 AssignParameterValues(commandParameters, parameterValues);
1490 
1491                 // 调用重载方法 
1492                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);
1493             }
1494             else
1495             {
1496                 // 没有参数值 
1497                 FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);
1498             }
1499         }
1500 
1501         /// <summary> 
1502         /// [私有方法][内部调用]执行指定数据库连接对象/事务的命令,映射数据表并填充数据集,DataSet/TableNames/MySqlParameters. 
1503         /// </summary> 
1504         /// <remarks> 
1505         /// 示例:  
1506         ///  FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new MySqlParameter("@prodid", 24)); 
1507         /// </remarks> 
1508         /// <param name="connection">一个有效的数据库连接对象</param> 
1509         /// <param name="transaction">一个有效的连接事务</param> 
1510         /// <param name="commandType">命令类型 (存储过程,命令文本或其它)</param> 
1511         /// <param name="commandText">存储过程名称或T-SQL语句</param> 
1512         /// <param name="dataSet">要填充结果集的DataSet实例</param> 
1513         /// <param name="tableNames">表映射的数据表数组 
1514         /// 用户定义的表名 (可有是实际的表名.) 
1515         /// </param> 
1516         /// <param name="commandParameters">分配给命令的SqlParamter参数数组</param> 
1517         private static void FillDataset(MySqlConnection connection, MySqlTransaction transaction, CommandType commandType,
1518             string commandText, DataSet dataSet, string[] tableNames,
1519             params MySqlParameter[] commandParameters)
1520         {
1521             if (connection == null) throw new ArgumentNullException("connection");
1522             if (dataSet == null) throw new ArgumentNullException("dataSet");
1523 
1524             // 创建MySqlCommand命令,并进行预处理 
1525             MySqlCommand command = new MySqlCommand();
1526             bool mustCloseConnection = false;
1527             PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
1528 
1529             // 执行命令 
1530             using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command))
1531             {
1532 
1533                 // 追加表映射 
1534                 if (tableNames != null && tableNames.Length > 0)
1535                 {
1536                     string tableName = "Table";
1537                     for (int index = 0; index < tableNames.Length; index++)
1538                     {
1539                         if (tableNames[index] == null || tableNames[index].Length == 0) throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
1540                         dataAdapter.TableMappings.Add(tableName, tableNames[index]);
1541                         tableName += (index + 1).ToString();
1542                     }
1543                 }
1544 
1545                 // 填充数据集使用默认表名称 
1546                 dataAdapter.Fill(dataSet);
1547 
1548                 // 清除参数,以便再次使用. 
1549                 command.Parameters.Clear();
1550             }
1551 
1552             if (mustCloseConnection)
1553                 connection.Close();
1554         }
1555         #endregion
1556 
1557         #region UpdateDataset 更新数据集
1558         /// <summary> 
1559         /// 执行数据集更新到数据库,指定inserted, updated, or deleted命令. 
1560         /// </summary> 
1561         /// <remarks> 
1562         /// 示例:  
1563         ///  UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order"); 
1564         /// </remarks> 
1565         /// <param name="insertCommand">[追加记录]一个有效的T-SQL语句或存储过程</param> 
1566         /// <param name="deleteCommand">[删除记录]一个有效的T-SQL语句或存储过程</param> 
1567         /// <param name="updateCommand">[更新记录]一个有效的T-SQL语句或存储过程</param> 
1568         /// <param name="dataSet">要更新到数据库的DataSet</param> 
1569         /// <param name="tableName">要更新到数据库的DataTable</param> 
1570         public static void UpdateDataset(MySqlCommand insertCommand, MySqlCommand deleteCommand, MySqlCommand updateCommand, DataSet dataSet, string tableName)
1571         {
1572             if (insertCommand == null) throw new ArgumentNullException("insertCommand");
1573             if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");
1574             if (updateCommand == null) throw new ArgumentNullException("updateCommand");
1575             if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");
1576 
1577             // 创建MySqlDataAdapter,当操作完成后释放. 
1578             using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter())
1579             {
1580                 // 设置数据适配器命令 
1581                 dataAdapter.UpdateCommand = updateCommand;
1582                 dataAdapter.InsertCommand = insertCommand;
1583                 dataAdapter.DeleteCommand = deleteCommand;
1584 
1585                 // 更新数据集改变到数据库 
1586                 dataAdapter.Update(dataSet, tableName);
1587 
1588                 // 提交所有改变到数据集. 
1589                 dataSet.AcceptChanges();
1590             }
1591         }
1592         #endregion
1593 
1594         #region CreateCommand 创建一条MySqlCommand命令
1595         /// <summary> 
1596         /// 创建MySqlCommand命令,指定数据库连接对象,存储过程名和参数. 
1597         /// </summary> 
1598         /// <remarks> 
1599         /// 示例:  
1600         ///  MySqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName"); 
1601         /// </remarks> 
1602         /// <param name="connection">一个有效的数据库连接对象</param> 
1603         /// <param name="spName">存储过程名称</param> 
1604         /// <param name="sourceColumns">源表的列名称数组</param> 
1605         /// <returns>返回MySqlCommand命令</returns> 
1606         public static MySqlCommand CreateCommand(MySqlConnection connection, string spName, params string[] sourceColumns)
1607         {
1608             if (connection == null) throw new ArgumentNullException("connection");
1609             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1610 
1611             // 创建命令 
1612             MySqlCommand cmd = new MySqlCommand(spName, connection);
1613             cmd.CommandType = CommandType.StoredProcedure;
1614 
1615             // 如果有参数值 
1616             if ((sourceColumns != null) && (sourceColumns.Length > 0))
1617             {
1618                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1619                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1620 
1621                 // 将源表的列到映射到DataSet命令中. 
1622                 for (int index = 0; index < sourceColumns.Length; index++)
1623                     commandParameters[index].SourceColumn = sourceColumns[index];
1624 
1625                 // Attach the discovered parameters to the MySqlCommand object 
1626                 AttachParameters(cmd, commandParameters);
1627             }
1628 
1629             return cmd;
1630         }
1631         #endregion
1632 
1633         #region ExecuteNonQueryTypedParams 类型化参数(DataRow)
1634         /// <summary> 
1635         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回受影响的行数. 
1636         /// </summary> 
1637         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1638         /// <param name="spName">存储过程名称</param> 
1639         /// <param name="dataRow">使用DataRow作为参数值</param> 
1640         /// <returns>返回影响的行数</returns> 
1641         public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)
1642         {
1643             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1644             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1645 
1646             // 如果row有值,存储过程必须初始化. 
1647             if (dataRow != null && dataRow.ItemArray.Length > 0)
1648             {
1649                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1650                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1651 
1652                 // 分配参数值 
1653                 AssignParameterValues(commandParameters, dataRow);
1654 
1655                 return MySqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1656             }
1657             else
1658             {
1659                 return MySqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
1660             }
1661         }
1662 
1663         /// <summary> 
1664         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回受影响的行数. 
1665         /// </summary> 
1666         /// <param name="connection">一个有效的数据库连接对象</param> 
1667         /// <param name="spName">存储过程名称</param> 
1668         /// <param name="dataRow">使用DataRow作为参数值</param> 
1669         /// <returns>返回影响的行数</returns> 
1670         public static int ExecuteNonQueryTypedParams(MySqlConnection connection, String spName, DataRow dataRow)
1671         {
1672             if (connection == null) throw new ArgumentNullException("connection");
1673             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1674 
1675             // 如果row有值,存储过程必须初始化. 
1676             if (dataRow != null && dataRow.ItemArray.Length > 0)
1677             {
1678                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1679                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1680 
1681                 // 分配参数值 
1682                 AssignParameterValues(commandParameters, dataRow);
1683 
1684                 return MySqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
1685             }
1686             else
1687             {
1688                 return MySqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
1689             }
1690         }
1691 
1692         /// <summary> 
1693         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回受影响的行数. 
1694         /// </summary> 
1695         /// <param name="transaction">一个有效的连接事务 object</param> 
1696         /// <param name="spName">存储过程名称</param> 
1697         /// <param name="dataRow">使用DataRow作为参数值</param> 
1698         /// <returns>返回影响的行数</returns> 
1699         public static int ExecuteNonQueryTypedParams(MySqlTransaction transaction, String spName, DataRow dataRow)
1700         {
1701             if (transaction == null) throw new ArgumentNullException("transaction");
1702             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1703             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1704 
1705             // Sf the row has values, the store procedure parameters must be initialized 
1706             if (dataRow != null && dataRow.ItemArray.Length > 0)
1707             {
1708                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1709                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1710 
1711                 // 分配参数值 
1712                 AssignParameterValues(commandParameters, dataRow);
1713 
1714                 return MySqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
1715             }
1716             else
1717             {
1718                 return MySqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
1719             }
1720         }
1721         #endregion
1722 
1723         #region ExecuteDatasetTypedParams 类型化参数(DataRow)
1724         /// <summary> 
1725         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataSet. 
1726         /// </summary> 
1727         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1728         /// <param name="spName">存储过程名称</param> 
1729         /// <param name="dataRow">使用DataRow作为参数值</param> 
1730         /// <returns>返回一个包含结果集的DataSet.</returns> 
1731         public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)
1732         {
1733             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1734             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1735 
1736             //如果row有值,存储过程必须初始化. 
1737             if (dataRow != null && dataRow.ItemArray.Length > 0)
1738             {
1739                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1740                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1741 
1742                 // 分配参数值 
1743                 AssignParameterValues(commandParameters, dataRow);
1744 
1745                 return MySqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1746             }
1747             else
1748             {
1749                 return MySqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
1750             }
1751         }
1752 
1753         /// <summary> 
1754         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataSet. 
1755         /// </summary> 
1756         /// <param name="connection">一个有效的数据库连接对象</param> 
1757         /// <param name="spName">存储过程名称</param> 
1758         /// <param name="dataRow">使用DataRow作为参数值</param> 
1759         /// <returns>返回一个包含结果集的DataSet.</returns> 
1760         /// 
1761         public static DataSet ExecuteDatasetTypedParams(MySqlConnection connection, String spName, DataRow dataRow)
1762         {
1763             if (connection == null) throw new ArgumentNullException("connection");
1764             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1765 
1766             // 如果row有值,存储过程必须初始化. 
1767             if (dataRow != null && dataRow.ItemArray.Length > 0)
1768             {
1769                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1770                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1771 
1772                 // 分配参数值 
1773                 AssignParameterValues(commandParameters, dataRow);
1774 
1775                 return MySqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
1776             }
1777             else
1778             {
1779                 return MySqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);
1780             }
1781         }
1782 
1783         /// <summary> 
1784         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回DataSet. 
1785         /// </summary> 
1786         /// <param name="transaction">一个有效的连接事务 object</param> 
1787         /// <param name="spName">存储过程名称</param> 
1788         /// <param name="dataRow">使用DataRow作为参数值</param> 
1789         /// <returns>返回一个包含结果集的DataSet.</returns> 
1790         public static DataSet ExecuteDatasetTypedParams(MySqlTransaction transaction, String spName, DataRow dataRow)
1791         {
1792             if (transaction == null) throw new ArgumentNullException("transaction");
1793             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1794             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1795 
1796             // 如果row有值,存储过程必须初始化. 
1797             if (dataRow != null && dataRow.ItemArray.Length > 0)
1798             {
1799                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1800                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1801 
1802                 // 分配参数值 
1803                 AssignParameterValues(commandParameters, dataRow);
1804 
1805                 return MySqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
1806             }
1807             else
1808             {
1809                 return MySqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
1810             }
1811         }
1812 
1813         #endregion
1814 
1815         #region ExecuteReaderTypedParams 类型化参数(DataRow)
1816         /// <summary> 
1817         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回DataReader. 
1818         /// </summary> 
1819         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1820         /// <param name="spName">存储过程名称</param> 
1821         /// <param name="dataRow">使用DataRow作为参数值</param> 
1822         /// <returns>返回包含结果集的MySqlDataReader</returns> 
1823         public static MySqlDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow)
1824         {
1825             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1826             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1827 
1828             // 如果row有值,存储过程必须初始化. 
1829             if (dataRow != null && dataRow.ItemArray.Length > 0)
1830             {
1831                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1832                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1833 
1834                 // 分配参数值 
1835                 AssignParameterValues(commandParameters, dataRow);
1836 
1837                 return MySqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1838             }
1839             else
1840             {
1841                 return MySqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
1842             }
1843         }
1844 
1845 
1846         /// <summary> 
1847         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回DataReader. 
1848         /// </summary> 
1849         /// <param name="connection">一个有效的数据库连接对象</param> 
1850         /// <param name="spName">存储过程名称</param> 
1851         /// <param name="dataRow">使用DataRow作为参数值</param> 
1852         /// <returns>返回包含结果集的MySqlDataReader</returns> 
1853         public static MySqlDataReader ExecuteReaderTypedParams(MySqlConnection connection, String spName, DataRow dataRow)
1854         {
1855             if (connection == null) throw new ArgumentNullException("connection");
1856             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1857 
1858             // 如果row有值,存储过程必须初始化. 
1859             if (dataRow != null && dataRow.ItemArray.Length > 0)
1860             {
1861                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1862                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1863 
1864                 // 分配参数值 
1865                 AssignParameterValues(commandParameters, dataRow);
1866 
1867                 return MySqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
1868             }
1869             else
1870             {
1871                 return MySqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);
1872             }
1873         }
1874 
1875         /// <summary> 
1876         /// 执行指定连接数据库事物的存储过程,使用DataRow做为参数值,返回DataReader. 
1877         /// </summary> 
1878         /// <param name="transaction">一个有效的连接事务 object</param> 
1879         /// <param name="spName">存储过程名称</param> 
1880         /// <param name="dataRow">使用DataRow作为参数值</param> 
1881         /// <returns>返回包含结果集的MySqlDataReader</returns> 
1882         public static MySqlDataReader ExecuteReaderTypedParams(MySqlTransaction transaction, String spName, DataRow dataRow)
1883         {
1884             if (transaction == null) throw new ArgumentNullException("transaction");
1885             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1886             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1887 
1888             // 如果row有值,存储过程必须初始化. 
1889             if (dataRow != null && dataRow.ItemArray.Length > 0)
1890             {
1891                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1892                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1893 
1894                 // 分配参数值 
1895                 AssignParameterValues(commandParameters, dataRow);
1896 
1897                 return MySqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
1898             }
1899             else
1900             {
1901                 return MySqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);
1902             }
1903         }
1904         #endregion
1905 
1906         #region ExecuteScalarTypedParams 类型化参数(DataRow)
1907         /// <summary> 
1908         /// 执行指定连接数据库连接字符串的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列. 
1909         /// </summary> 
1910         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
1911         /// <param name="spName">存储过程名称</param> 
1912         /// <param name="dataRow">使用DataRow作为参数值</param> 
1913         /// <returns>返回结果集中的第一行第一列</returns> 
1914         public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow)
1915         {
1916             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
1917             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1918 
1919             // 如果row有值,存储过程必须初始化. 
1920             if (dataRow != null && dataRow.ItemArray.Length > 0)
1921             {
1922                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1923                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
1924 
1925                 // 分配参数值 
1926                 AssignParameterValues(commandParameters, dataRow);
1927 
1928                 return MySqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
1929             }
1930             else
1931             {
1932                 return MySqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
1933             }
1934         }
1935 
1936         /// <summary> 
1937         /// 执行指定连接数据库连接对象的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列.
1938         /// </summary> 
1939         /// <param name="connection">一个有效的数据库连接对象</param> 
1940         /// <param name="spName">存储过程名称</param> 
1941         /// <param name="dataRow">使用DataRow作为参数值</param> 
1942         /// <returns>返回结果集中的第一行第一列</returns> 
1943         public static object ExecuteScalarTypedParams(MySqlConnection connection, String spName, DataRow dataRow)
1944         {
1945             if (connection == null) throw new ArgumentNullException("connection");
1946             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1947 
1948             // 如果row有值,存储过程必须初始化. 
1949             if (dataRow != null && dataRow.ItemArray.Length > 0)
1950             {
1951                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1952                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(connection, spName);
1953 
1954                 // 分配参数值 
1955                 AssignParameterValues(commandParameters, dataRow);
1956 
1957                 return MySqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
1958             }
1959             else
1960             {
1961                 return MySqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);
1962             }
1963         }
1964 
1965         /// <summary> 
1966         /// 执行指定连接数据库事务的存储过程,使用DataRow做为参数值,返回结果集中的第一行第一列. 
1967         /// </summary> 
1968         /// <param name="transaction">一个有效的连接事务 object</param> 
1969         /// <param name="spName">存储过程名称</param> 
1970         /// <param name="dataRow">使用DataRow作为参数值</param> 
1971         /// <returns>返回结果集中的第一行第一列</returns> 
1972         public static object ExecuteScalarTypedParams(MySqlTransaction transaction, String spName, DataRow dataRow)
1973         {
1974             if (transaction == null) throw new ArgumentNullException("transaction");
1975             if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
1976             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
1977 
1978             // 如果row有值,存储过程必须初始化. 
1979             if (dataRow != null && dataRow.ItemArray.Length > 0)
1980             {
1981                 // 从缓存中加载存储过程参数,如果缓存中不存在则从数据库中检索参数信息并加载到缓存中. () 
1982                 MySqlParameter[] commandParameters = MySqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
1983 
1984                 // 分配参数值 
1985                 AssignParameterValues(commandParameters, dataRow);
1986 
1987                 return MySqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
1988             }
1989             else
1990             {
1991                 return MySqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
1992             }
1993         }
1994         #endregion
1995 
1996     }
1997 
1998     /// <summary> 
1999     /// MySqlHelperParameterCache提供缓存存储过程参数,并能够在运行时从存储过程中探索参数. 
2000     /// </summary> 
2001     public sealed class MySqlHelperParameterCache
2002     {
2003         #region 私有方法,字段,构造函数
2004         // 私有构造函数,妨止类被实例化. 
2005         private MySqlHelperParameterCache() { }
2006 
2007         // 这个方法要注意 
2008         private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
2009 
2010         /// <summary> 
2011         /// 探索运行时的存储过程,返回MySqlParameter参数数组. 
2012         /// 初始化参数值为 DBNull.Value. 
2013         /// </summary> 
2014         /// <param name="connection">一个有效的数据库连接</param> 
2015         /// <param name="spName">存储过程名称</param> 
2016         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 
2017         /// <returns>返回MySqlParameter参数数组</returns> 
2018         private static MySqlParameter[] DiscoverSpParameterSet(MySqlConnection connection, string spName, bool includeReturnValueParameter)
2019         {
2020             if (connection == null) throw new ArgumentNullException("connection");
2021             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2022 
2023             MySqlCommand cmd = new MySqlCommand(spName, connection);
2024             cmd.CommandType = CommandType.StoredProcedure;
2025 
2026             connection.Open();
2027             // 检索cmd指定的存储过程的参数信息,并填充到cmd的Parameters参数集中. 
2028             MySqlCommandBuilder.DeriveParameters(cmd);
2029             connection.Close();
2030             // 如果不包含返回值参数,将参数集中的每一个参数删除. 
2031             if (!includeReturnValueParameter)
2032             {
2033                 cmd.Parameters.RemoveAt(0);
2034             }
2035 
2036             // 创建参数数组 
2037             MySqlParameter[] discoveredParameters = new MySqlParameter[cmd.Parameters.Count];
2038             // 将cmd的Parameters参数集复制到discoveredParameters数组. 
2039             cmd.Parameters.CopyTo(discoveredParameters, 0);
2040 
2041             // 初始化参数值为 DBNull.Value. 
2042             foreach (MySqlParameter discoveredParameter in discoveredParameters)
2043             {
2044                 discoveredParameter.Value = DBNull.Value;
2045             }
2046             return discoveredParameters;
2047         }
2048 
2049         /// <summary> 
2050         /// MySqlParameter参数数组的深层拷贝. 
2051         /// </summary> 
2052         /// <param name="originalParameters">原始参数数组</param> 
2053         /// <returns>返回一个同样的参数数组</returns> 
2054         private static MySqlParameter[] CloneParameters(MySqlParameter[] originalParameters)
2055         {
2056             MySqlParameter[] clonedParameters = new MySqlParameter[originalParameters.Length];
2057 
2058             for (int i = 0, j = originalParameters.Length; i < j; i++)
2059             {
2060                 clonedParameters[i] = (MySqlParameter)((ICloneable)originalParameters[i]).Clone();
2061             }
2062 
2063             return clonedParameters;
2064         }
2065 
2066         #endregion 私有方法,字段,构造函数结束
2067 
2068         #region 缓存方法
2069 
2070         /// <summary> 
2071         /// 追加参数数组到缓存. 
2072         /// </summary> 
2073         /// <param name="connectionString">一个有效的数据库连接字符串</param> 
2074         /// <param name="commandText">存储过程名或SQL语句</param> 
2075         /// <param name="commandParameters">要缓存的参数数组</param> 
2076         public static void CacheParameterSet(string connectionString, string commandText, params MySqlParameter[] commandParameters)
2077         {
2078             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2079             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
2080 
2081             string hashKey = connectionString + ":" + commandText;
2082 
2083             paramCache[hashKey] = commandParameters;
2084         }
2085 
2086         /// <summary> 
2087         /// 从缓存中获取参数数组. 
2088         /// </summary> 
2089         /// <param name="connectionString">一个有效的数据库连接字符</param> 
2090         /// <param name="commandText">存储过程名或SQL语句</param> 
2091         /// <returns>参数数组</returns> 
2092         public static MySqlParameter[] GetCachedParameterSet(string connectionString, string commandText)
2093         {
2094             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2095             if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
2096 
2097             string hashKey = connectionString + ":" + commandText;
2098 
2099             MySqlParameter[] cachedParameters = paramCache[hashKey] as MySqlParameter[];
2100             if (cachedParameters == null)
2101             {
2102                 return null;
2103             }
2104             else
2105             {
2106                 return CloneParameters(cachedParameters);
2107             }
2108         }
2109 
2110         #endregion 缓存方法结束
2111 
2112         #region 检索指定的存储过程的参数集
2113 
2114         /// <summary> 
2115         /// 返回指定的存储过程的参数集 
2116         /// </summary> 
2117         /// <remarks> 
2118         /// 这个方法将查询数据库,并将信息存储到缓存. 
2119         /// </remarks> 
2120         /// <param name="connectionString">一个有效的数据库连接字符</param> 
2121         /// <param name="spName">存储过程名</param> 
2122         /// <returns>返回MySqlParameter参数数组</returns> 
2123         public static MySqlParameter[] GetSpParameterSet(string connectionString, string spName)
2124         {
2125             return GetSpParameterSet(connectionString, spName, false);
2126         }
2127 
2128         /// <summary> 
2129         /// 返回指定的存储过程的参数集 
2130         /// </summary> 
2131         /// <remarks> 
2132         /// 这个方法将查询数据库,并将信息存储到缓存. 
2133         /// </remarks> 
2134         /// <param name="connectionString">一个有效的数据库连接字符.</param> 
2135         /// <param name="spName">存储过程名</param> 
2136         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 
2137         /// <returns>返回MySqlParameter参数数组</returns> 
2138         public static MySqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
2139         {
2140             if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
2141             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2142 
2143             using (MySqlConnection connection = new MySqlConnection(connectionString))
2144             {
2145                 return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);
2146             }
2147         }
2148 
2149         /// <summary> 
2150         /// [内部]返回指定的存储过程的参数集(使用连接对象). 
2151         /// </summary> 
2152         /// <remarks> 
2153         /// 这个方法将查询数据库,并将信息存储到缓存. 
2154         /// </remarks> 
2155         /// <param name="connection">一个有效的数据库连接字符</param> 
2156         /// <param name="spName">存储过程名</param> 
2157         /// <returns>返回MySqlParameter参数数组</returns> 
2158         internal static MySqlParameter[] GetSpParameterSet(MySqlConnection connection, string spName)
2159         {
2160             return GetSpParameterSet(connection, spName, false);
2161         }
2162 
2163         /// <summary> 
2164         /// [内部]返回指定的存储过程的参数集(使用连接对象) 
2165         /// </summary> 
2166         /// <remarks> 
2167         /// 这个方法将查询数据库,并将信息存储到缓存. 
2168         /// </remarks> 
2169         /// <param name="connection">一个有效的数据库连接对象</param> 
2170         /// <param name="spName">存储过程名</param> 
2171         /// <param name="includeReturnValueParameter"> 
2172         /// 是否包含返回值参数 
2173         /// </param> 
2174         /// <returns>返回MySqlParameter参数数组</returns> 
2175         internal static MySqlParameter[] GetSpParameterSet(MySqlConnection connection, string spName, bool includeReturnValueParameter)
2176         {
2177             if (connection == null) throw new ArgumentNullException("connection");
2178             using (MySqlConnection clonedConnection = (MySqlConnection)((ICloneable)connection).Clone())
2179             {
2180                 return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);
2181             }
2182         }
2183 
2184         /// <summary> 
2185         /// [私有]返回指定的存储过程的参数集(使用连接对象) 
2186         /// </summary> 
2187         /// <param name="connection">一个有效的数据库连接对象</param> 
2188         /// <param name="spName">存储过程名</param> 
2189         /// <param name="includeReturnValueParameter">是否包含返回值参数</param> 
2190         /// <returns>返回MySqlParameter参数数组</returns> 
2191         private static MySqlParameter[] GetSpParameterSetInternal(MySqlConnection connection, string spName, bool includeReturnValueParameter)
2192         {
2193             if (connection == null) throw new ArgumentNullException("connection");
2194             if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
2195 
2196             string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
2197 
2198             MySqlParameter[] cachedParameters;
2199 
2200             cachedParameters = paramCache[hashKey] as MySqlParameter[];
2201             if (cachedParameters == null)
2202             {
2203                 MySqlParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);
2204                 paramCache[hashKey] = spParameters;
2205                 cachedParameters = spParameters;
2206             }
2207 
2208             return CloneParameters(cachedParameters);
2209         }
2210 
2211         #endregion 参数集检索结束
2212 
2213     }
2214 }

 

以上是关于快速拥有各种数据访问SqlHelper的主要内容,如果未能解决你的问题,请参考以下文章

通过sqlHelper访问数据库,查询数据

公共的数据库访问访问类 SqlHelper.cs

关于SqlHelper的详解

NET Core 实战 Dapper 扩展数据访问

SQLserver数据库操作帮助类SqlHelper

sqlhelper .cs