微软官方的SqlHelper
using System;
using System.Data;????
using System.Xml;????
using System.Data.SqlClient;????
using System.Collections;????
using System.Configuration;
namespace BookDAL???
{????
??? /// <summary>????
??? /// SqlServer數(shù)據(jù)訪問幫助類????
??? /// </summary>????
??? public sealed class SqlHelper????
??? {????
??????? #region 私有構(gòu)造函數(shù)和方法
??????? private SqlHelper() { }
??????? /// <summary>???
??????? /// 將SqlParameter參數(shù)數(shù)組(參數(shù)值)分配給SqlCommand命令.????
??????? /// 這個方法將給任何一個參數(shù)分配DBNull.Value;????
??????? /// 該操作將阻止默認值的使用.????
??????? /// </summary>????
??????? /// <param name="command">命令名</param>????
??????? /// <param name="commandParameters">SqlParameters數(shù)組</param>????
??????? private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)????
??????? {????
??????????? if (command == null) throw new ArgumentNullException("command");????
??????????? if (commandParameters != null)????
??????????? {????
??????????????? foreach (SqlParameter p in commandParameters)????
??????????????? {????
??????????????????? if (p != null)????
??????????????????? {????
??????????????????????? // 檢查未分配值的輸出參數(shù),將其分配以DBNull.Value.????
??????????????????????? if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) &&????
??????????????????????????? (p.Value == null))????
??????????????????????? {????
??????????????????????????? p.Value = DBNull.Value;????
??????????????????????? }????
??????????????????????? command.Parameters.Add(p);????
??????????????????? }????
??????????????? }????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 將DataRow類型的列值分配到SqlParameter參數(shù)數(shù)組.????
??????? /// </summary>????
??????? /// <param name="commandParameters">要分配值的SqlParameter參數(shù)數(shù)組</param>????
??????? /// <param name="dataRow">將要分配給存儲過程參數(shù)的DataRow</param>????
??????? private static void AssignParameterValues(SqlParameter[] commandParameters, DataRow dataRow)????
??????? {????
??????????? if ((commandParameters == null) || (dataRow == null))????
??????????? {????
??????????????? return;????
??????????? }
??????????? int i = 0;???
??????????? // 設(shè)置參數(shù)值????
??????????? foreach (SqlParameter commandParameter in commandParameters)????
??????????? {????
??????????????? // 創(chuàng)建參數(shù)名稱,如果不存在,只拋出一個異常.????
??????????????? if (commandParameter.ParameterName == null ||????
??????????????????? commandParameter.ParameterName.Length <= 1)????
??????????????????? throw new Exception(????
??????????????????????? string.Format("請?zhí)峁﹨?shù){0}一個有效的名稱{1}.", i, commandParameter.ParameterName));????
??????????????? // 從dataRow的表中獲取為參數(shù)數(shù)組中數(shù)組名稱的列的索引.????
??????????????? // 如果存在和參數(shù)名稱相同的列,則將列值賦給當前名稱的參數(shù).????
??????????????? if (dataRow.Table.Columns.IndexOf(commandParameter.ParameterName.Substring(1)) != -1)????
??????????????????? commandParameter.Value = dataRow[commandParameter.ParameterName.Substring(1)];????
??????????????? i++;????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 將一個對象數(shù)組分配給SqlParameter參數(shù)數(shù)組.????
??????? /// </summary>????
??????? /// <param name="commandParameters">要分配值的SqlParameter參數(shù)數(shù)組</param>????
??????? /// <param name="parameterValues">將要分配給存儲過程參數(shù)的對象數(shù)組</param>????
??????? private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)????
??????? {????
??????????? if ((commandParameters == null) || (parameterValues == null))????
??????????? {????
??????????????? return;????
??????????? }
??????????? // 確保對象數(shù)組個數(shù)與參數(shù)個數(shù)匹配,如果不匹配,拋出一個異常.???
??????????? if (commandParameters.Length != parameterValues.Length)????
??????????? {????
??????????????? throw new ArgumentException("參數(shù)值個數(shù)與參數(shù)不匹配.");????
??????????? }
??????????? // 給參數(shù)賦值???
??????????? for (int i = 0, j = commandParameters.Length; i < j; i++)????
??????????? {????
??????????????? // If the current array value derives from IDbDataParameter, then assign its Value property????
??????????????? if (parameterValues[i] is IDbDataParameter)????
??????????????? {????
??????????????????? IDbDataParameter paramInstance = (IDbDataParameter)parameterValues[i];????
??????????????????? if (paramInstance.Value == null)????
??????????????????? {????
??????????????????????? commandParameters[i].Value = DBNull.Value;????
??????????????????? }????
??????????????????? else????
??????????????????? {????
??????????????????????? commandParameters[i].Value = paramInstance.Value;????
??????????????????? }????
??????????????? }????
??????????????? else if (parameterValues[i] == null)????
??????????????? {????
??????????????????? commandParameters[i].Value = DBNull.Value;????
??????????????? }????
??????????????? else????
??????????????? {????
??????????????????? commandParameters[i].Value = parameterValues[i];????
??????????????? }????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 預處理用戶提供的命令,數(shù)據(jù)庫連接/事務(wù)/命令類型/參數(shù)????
??????? /// </summary>????
??????? /// <param name="command">要處理的SqlCommand</param>????
??????? /// <param name="connection">數(shù)據(jù)庫連接</param>????
??????? /// <param name="transaction">一個有效的事務(wù)或者是null值</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本, 其它.)</param>????
??????? /// <param name="commandText">存儲過程名或都T-SQL命令文本</param>????
??????? /// <param name="commandParameters">和命令相關(guān)聯(lián)的SqlParameter參數(shù)數(shù)組,如果沒有參數(shù)為'null'</param>????
??????? /// <param name="mustCloseConnection"><c>true</c> 如果連接是打開的,則為true,其它情況下為false.</param>????
??????? private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, out bool mustCloseConnection)????
??????? {????
??????????? if (command == null) throw new ArgumentNullException("command");????
??????????? if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
??????????? // If the provided connection is not open, we will open it???
??????????? if (connection.State != ConnectionState.Open)????
??????????? {????
??????????????? mustCloseConnection = true;????
??????????????? connection.Open();????
??????????? }????
??????????? else????
??????????? {????
??????????????? mustCloseConnection = false;????
??????????? }
??????????? // 給命令分配一個數(shù)據(jù)庫連接.???
??????????? command.Connection = connection;
??????????? // 設(shè)置命令文本(存儲過程名或SQL語句)???
??????????? command.CommandText = commandText;
??????????? // 分配事務(wù)???
??????????? if (transaction != null)????
??????????? {????
??????????????? if (transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????????? command.Transaction = transaction;????
??????????? }
??????????? // 設(shè)置命令類型.???
??????????? command.CommandType = commandType;
??????????? // 分配命令參數(shù)???
??????????? if (commandParameters != null)????
??????????? {????
??????????????? AttachParameters(command, commandParameters);????
??????????? }????
??????????? return;????
??????? }
??????? #endregion 私有構(gòu)造函數(shù)和方法結(jié)束
??????? #region 數(shù)據(jù)庫連接???
??????? /// <summary>????
??????? /// 一個有效的數(shù)據(jù)庫連接字符串????
??????? /// </summary>????
??????? /// <returns></returns>????
??????? public static string GetConnSting()????
??????? {????
??????????? return ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;????
??????? }????
??????? /// <summary>????
??????? /// 一個有效的數(shù)據(jù)庫連接對象????
??????? /// </summary>????
??????? /// <returns></returns>????
??????? public static SqlConnection GetConnection()????
??????? {????
??????????? SqlConnection Connection = new SqlConnection(SqlHelper.GetConnSting());????
??????????? return Connection;????
??????? }????
??????? #endregion
??????? #region ExecuteNonQuery命令
??????? /// <summary>???
??????? /// 執(zhí)行指定連接字符串,類型的SqlCommand.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本, 其它.)</param>????
??????? /// <param name="commandText">存儲過程名稱或SQL語句</param>????
??????? /// <returns>返回命令影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteNonQuery(connectionString, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接字符串,類型的SqlCommand.如果沒有提供參數(shù),不返回結(jié)果.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本, 其它.)</param>????
??????? /// <param name="commandText">存儲過程名稱或SQL語句</param>????
??????? /// <param name="commandParameters">SqlParameter參數(shù)數(shù)組</param>????
??????? /// <returns>返回命令影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
??????????? using (SqlConnection connection = new SqlConnection(connectionString))???
??????????? {????
??????????????? connection.Open();
??????????????? return ExecuteNonQuery(connection, commandType, commandText, commandParameters);???
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接字符串的存儲過程,將對象數(shù)組的值賦給存儲過程參數(shù),????
??????? /// 此方法需要在參數(shù)緩存方法中探索參數(shù)并生成參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 這個方法沒有提供訪問輸出參數(shù)和返回值.????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串/param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配到存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回受影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果存在參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從探索存儲過程參數(shù)(加載到緩存)并分配給存儲過程參數(shù)數(shù)組.????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)情況下????
??????????????? return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型(存儲過程,命令文本或其它.)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteNonQuery(connection, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型(存儲過程,命令文本或其它.)</param>????
??????? /// <param name="commandText">T存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // Finally, execute the command???
??????????? int retval = cmd.ExecuteNonQuery();
??????????? // 清除參數(shù),以便再次使用.???
??????????? cmd.Parameters.Clear();????
??????????? if (mustCloseConnection)????
??????????????? connection.Close();????
??????????? return retval;????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,將對象數(shù)組的值賦給存儲過程參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(SqlConnection connection, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù)????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 給存儲過程分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行帶事務(wù)的SqlCommand.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例.:?
??????? ///? int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型(存儲過程,命令文本或其它.)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回影響的行數(shù)/returns>????
??????? public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteNonQuery(transaction, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行帶事務(wù)的SqlCommand(指定參數(shù)).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型(存儲過程,命令文本或其它.)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
??????????? // 預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 執(zhí)行???
??????????? int retval = cmd.ExecuteNonQuery();
??????????? // 清除參數(shù)集,以便再次使用.???
??????????? cmd.Parameters.Clear();????
??????????? return retval;????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行帶事務(wù)的SqlCommand(指定參數(shù)值).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值????
??????? /// 示例:?
??????? ///? int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回受影響的行數(shù)</returns>????
??????? public static int ExecuteNonQuery(SqlTransaction transaction, string spName, params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion ExecuteNonQuery方法結(jié)束
??????? #region ExecuteDataset方法
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteDataset(connectionString, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:????
??????? ///? DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamters參數(shù)數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");
??????????? // 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.???
??????????? using (SqlConnection connection = new SqlConnection(connectionString))????
??????????? {????
??????????????? connection.Open();
??????????????? // 調(diào)用指定數(shù)據(jù)庫連接字符串重載方法.???
??????????????? return ExecuteDataset(connection, commandType, commandText, commandParameters);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,直接提供參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值.????
??????? /// 示例:????
??????? ///? DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))???
??????????? {????
??????????????? // 從緩存中檢索存儲過程參數(shù)????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 給存儲過程參數(shù)分配值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteDataset(connection, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,指定存儲過程參數(shù),返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");
??????????? // 預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 創(chuàng)建SqlDataAdapter和DataSet.???
??????????? using (SqlDataAdapter da = new SqlDataAdapter(cmd))????
??????????? {????
??????????????? DataSet ds = new DataSet();
??????????????? // 填充DataSet.???
??????????????? da.Fill(ds);
??????????????? cmd.Parameters.Clear();
??????????????? if (mustCloseConnection)???
??????????????????? connection.Close();
??????????????? return ds;???
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,指定參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸入?yún)?shù)和返回值.????
??????? /// 示例.:?
??????? ///? DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))???
??????????? {????
??????????????? // 比緩存中加載存儲過程參數(shù)????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 給存儲過程參數(shù)分配值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteDataset(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定事務(wù)的命令,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="transaction">事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteDataset(transaction, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定事務(wù)的命令,指定參數(shù),返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
??????????? // 預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 創(chuàng)建 DataAdapter & DataSet???
??????????? using (SqlDataAdapter da = new SqlDataAdapter(cmd))????
??????????? {????
??????????????? DataSet ds = new DataSet();????
??????????????? da.Fill(ds);????
??????????????? cmd.Parameters.Clear();????
??????????????? return ds;????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定事務(wù)的命令,指定參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸入?yún)?shù)和返回值.????
??????? /// 示例.:?
??????? ///? DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">事務(wù)</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet</returns>????
??????? public static DataSet ExecuteDataset(SqlTransaction transaction, string spName, params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))???
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù)????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 給存儲過程參數(shù)分配值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion ExecuteDataset數(shù)據(jù)集命令結(jié)束
??????? #region ExecuteReader 數(shù)據(jù)閱讀器
??????? /// <summary>???
??????? /// 枚舉,標識數(shù)據(jù)庫連接是由SqlHelper提供還是由調(diào)用者提供????
??????? /// </summary>????
??????? private enum SqlConnectionOwnership????
??????? {????
??????????? /// <summary>由SqlHelper提供連接</summary>????
??????????? Internal,????
??????????? /// <summary>由調(diào)用者提供連接</summary>????
??????????? External????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的數(shù)據(jù)閱讀器.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 如果是SqlHelper打開連接,當連接關(guān)閉DataReader也將關(guān)閉.????
??????? /// 如果是調(diào)用都打開連接,DataReader由調(diào)用都管理.????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="transaction">一個有效的事務(wù),或者為 'null'</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParameters參數(shù)數(shù)組,如果沒有參數(shù)則為'null'</param>????
??????? /// <param name="connectionOwnership">標識數(shù)據(jù)庫連接對象是由調(diào)用者提供還是由SqlHelper提供</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");
??????????? bool mustCloseConnection = false;???
??????????? // 創(chuàng)建命令????
??????????? SqlCommand cmd = new SqlCommand();????
??????????? try????
??????????? {????
??????????????? PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????????? // 創(chuàng)建數(shù)據(jù)閱讀器???
??????????????? SqlDataReader dataReader;
??????????????? if (connectionOwnership == SqlConnectionOwnership.External)???
??????????????? {????
??????????????????? dataReader = cmd.ExecuteReader();????
??????????????? }????
??????????????? else????
??????????????? {????
??????????????????? dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);????
??????????????? }
??????????????? // 清除參數(shù),以便再次使用..???
??????????????? // HACK: There is a problem here, the output parameter values are fletched????
??????????????? // when the reader is closed, so if the parameters are detached from the command????
??????????????? // then the SqlReader can磘 set its values.????
??????????????? // When this happen, the parameters can磘 be used again in other command.????
??????????????? bool canClear = true;????
??????????????? foreach (SqlParameter commandParameter in cmd.Parameters)????
??????????????? {????
??????????????????? if (commandParameter.Direction != ParameterDirection.Input)????
??????????????????????? canClear = false;????
??????????????? }
??????????????? if (canClear)???
??????????????? {????
??????????????????? cmd.Parameters.Clear();????
??????????????? }
??????????????? return dataReader;???
??????????? }????
??????????? catch????
??????????? {????
??????????????? if (mustCloseConnection)????
??????????????????? connection.Close();????
??????????????? throw;????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的數(shù)據(jù)閱讀器.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteReader(connectionString, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的數(shù)據(jù)閱讀器,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組(new SqlParameter("@prodid", 24))</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? SqlConnection connection = null;????
??????????? try????
??????????? {????
??????????????? connection = new SqlConnection(connectionString);????
??????????????? connection.Open();
??????????????? return ExecuteReader(connection, null, commandType, commandText, commandParameters, SqlConnectionOwnership.Internal);???
??????????? }????
??????????? catch????
??????????? {????
??????????????? // If we fail to return the SqlDatReader, we need to close the connection ourselves????
??????????????? if (connection != null) connection.Close();????
??????????????? throw;????
??????????? }
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的數(shù)據(jù)閱讀器,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))???
??????????? {????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的數(shù)據(jù)閱讀器.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名或T-SQL語句</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteReader(connection, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// [調(diào)用者方式]執(zhí)行指定數(shù)據(jù)庫連接對象的數(shù)據(jù)閱讀器,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandParameters">SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? return ExecuteReader(connection, (SqlTransaction)null, commandType, commandText, commandParameters, SqlConnectionOwnership.External);????
??????? }
??????? /// <summary>???
??????? /// [調(diào)用者方式]執(zhí)行指定數(shù)據(jù)庫連接對象的數(shù)據(jù)閱讀器,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">T存儲過程名</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlConnection connection, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))???
??????????? {????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return ExecuteReader(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// [調(diào)用者方式]執(zhí)行指定數(shù)據(jù)庫事務(wù)的數(shù)據(jù)閱讀器,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText)????
??????? {????
??????????? return ExecuteReader(transaction, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// [調(diào)用者方式]執(zhí)行指定數(shù)據(jù)庫事務(wù)的數(shù)據(jù)閱讀器,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///?? SqlDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
??????????? return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, SqlConnectionOwnership.External);???
??????? }
??????? /// <summary>???
??????? /// [調(diào)用者方式]執(zhí)行指定數(shù)據(jù)庫事務(wù)的數(shù)據(jù)閱讀器,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? SqlDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReader(SqlTransaction transaction, string spName, params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteReader(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion ExecuteReader數(shù)據(jù)閱讀器
??????? #region ExecuteScalar 返回結(jié)果集中的第一行第一列
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)????
??????? {????
??????????? // 執(zhí)行參數(shù)為空的方法????
??????????? return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,指定參數(shù),返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? // 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.????
??????????? using (SqlConnection connection = new SqlConnection(connectionString))????
??????????? {????
??????????????? connection.Open();
??????????????? // 調(diào)用指定數(shù)據(jù)庫連接字符串重載方法.???
??????????????? return ExecuteScalar(connection, commandType, commandText, commandParameters);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,指定參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)????
??????? {????
??????????? // 執(zhí)行參數(shù)為空的方法????
??????????? return ExecuteScalar(connection, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,指定參數(shù),返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理???
??????????? SqlCommand cmd = new SqlCommand();
??????????? bool mustCloseConnection = false;???
??????????? PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 執(zhí)行SqlCommand命令,并返回結(jié)果.???
??????????? object retval = cmd.ExecuteScalar();
??????????? // 清除參數(shù),以便再次使用.???
??????????? cmd.Parameters.Clear();
??????????? if (mustCloseConnection)???
??????????????? connection.Close();
??????????? return retval;???
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,指定參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlConnection connection, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteScalar(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)????
??????? {????
??????????? // 執(zhí)行參數(shù)為空的方法????
??????????? return ExecuteScalar(transaction, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,指定參數(shù),返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 執(zhí)行SqlCommand命令,并返回結(jié)果.???
??????????? object retval = cmd.ExecuteScalar();
??????????? // 清除參數(shù),以便再次使用.???
??????????? cmd.Parameters.Clear();????
??????????? return retval;????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,指定參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalar(SqlTransaction transaction, string spName, params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // PPull the parameters for this stored procedure from the parameter cache ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion ExecuteScalar
??????? #region ExecuteXmlReader XML閱讀器???
??????? /// <summary>????
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句 using "FOR XML AUTO"</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)????
??????? {????
??????????? // 執(zhí)行參數(shù)為空的方法????
??????????? return ExecuteXmlReader(connection, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句 using "FOR XML AUTO"</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");
??????????? bool mustCloseConnection = false;???
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理????
??????????? SqlCommand cmd = new SqlCommand();????
??????????? try????
??????????? {????
??????????????? PrepareCommand(cmd, connection, (SqlTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection);
??????????????? // 執(zhí)行命令???
??????????????? XmlReader retval = cmd.ExecuteXmlReader();
??????????????? // 清除參數(shù),以便再次使用.???
??????????????? cmd.Parameters.Clear();
??????????????? return retval;???
??????????? }????
??????????? catch????
??????????? {????
??????????????? if (mustCloseConnection)????
??????????????????? connection.Close();????
??????????????? throw;????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(conn, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱 using "FOR XML AUTO"</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlConnection connection, string spName, params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders");????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句 using "FOR XML AUTO"</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText)????
??????? {????
??????????? // 執(zhí)行參數(shù)為空的方法????
??????????? return ExecuteXmlReader(transaction, commandType, commandText, (SqlParameter[])null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句 using "FOR XML AUTO"</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理???
??????????? SqlCommand cmd = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 執(zhí)行命令???
??????????? XmlReader retval = cmd.ExecuteXmlReader();
??????????? // 清除參數(shù),以便再次使用.???
??????????? cmd.Parameters.Clear();????
??????????? return retval;????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的SqlCommand命令,并產(chǎn)生一個XmlReader對象做為結(jié)果集返回,指定參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? XmlReader r = ExecuteXmlReader(trans, "GetOrders", 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet.</returns>????
??????? public static XmlReader ExecuteXmlReader(SqlTransaction transaction, string spName, params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? return ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion ExecuteXmlReader 閱讀器結(jié)束
??????? #region FillDataset 填充數(shù)據(jù)集???
??????? /// <summary>????
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)</param>????
??????? public static void FillDataset(string connectionString, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");
??????????? // 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.???
??????????? using (SqlConnection connection = new SqlConnection(connectionString))????
??????????? {????
??????????????? connection.Open();
??????????????? // 調(diào)用指定數(shù)據(jù)庫連接字符串重載方法.???
??????????????? FillDataset(connection, commandType, commandText, dataSet, tableNames);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集.指定命令參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? public static void FillDataset(string connectionString, CommandType commandType,????
??????????? string commandText, DataSet dataSet, string[] tableNames,????
??????????? params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");????
??????????? // 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.????
??????????? using (SqlConnection connection = new SqlConnection(connectionString))????
??????????? {????
??????????????? connection.Open();
??????????????? // 調(diào)用指定數(shù)據(jù)庫連接字符串重載方法.???
??????????????? FillDataset(connection, commandType, commandText, dataSet, tableNames, commandParameters);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接字符串的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,指定存儲過程參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? FillDataset(connString, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, 24);????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>???
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? public static void FillDataset(string connectionString, string spName,????
??????????? DataSet dataSet, string[] tableNames,????
??????????? params object[] parameterValues)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");????
??????????? // 創(chuàng)建并打開數(shù)據(jù)庫連接對象,操作完成釋放對象.????
??????????? using (SqlConnection connection = new SqlConnection(connectionString))????
??????????? {????
??????????????? connection.Open();
??????????????? // 調(diào)用指定數(shù)據(jù)庫連接字符串重載方法.???
??????????????? FillDataset(connection, spName, dataSet, tableNames, parameterValues);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>???
??????? public static void FillDataset(SqlConnection connection, CommandType commandType,????
??????????? string commandText, DataSet dataSet, string[] tableNames)????
??????? {????
??????????? FillDataset(connection, commandType, commandText, dataSet, tableNames, null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(conn, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? public static void FillDataset(SqlConnection connection, CommandType commandType,????
??????????? string commandText, DataSet dataSet, string[] tableNames,????
??????????? params SqlParameter[] commandParameters)????
??????? {????
??????????? FillDataset(connection, null, commandType, commandText, dataSet, tableNames, commandParameters);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫連接對象的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,指定存儲過程參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? FillDataset(conn, "GetOrders", ds, new string[] {"orders"}, 24, 36);????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? public static void FillDataset(SqlConnection connection, string spName,????
??????????? DataSet dataSet, string[] tableNames,????
??????????? params object[] parameterValues)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? FillDataset(connection, CommandType.StoredProcedure, spName, dataSet, tableNames);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"});????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? public static void FillDataset(SqlTransaction transaction, CommandType commandType,????
??????????? string commandText,????
??????????? DataSet dataSet, string[] tableNames)????
??????? {????
??????????? FillDataset(transaction, commandType, commandText, dataSet, tableNames, null);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,指定參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? public static void FillDataset(SqlTransaction transaction, CommandType commandType,????
??????????? string commandText, DataSet dataSet, string[] tableNames,????
??????????? params SqlParameter[] commandParameters)????
??????? {????
??????????? FillDataset(transaction.Connection, transaction, commandType, commandText, dataSet, tableNames, commandParameters);????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定數(shù)據(jù)庫事務(wù)的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,指定存儲過程參數(shù)值.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 此方法不提供訪問存儲過程輸出參數(shù)和返回值參數(shù).????
??????? ///????
??????? /// 示例:?
??????? ///? FillDataset(trans, "GetOrders", ds, new string[]{"orders"}, 24, 36);????
??????? /// </remarks>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? /// <param name="parameterValues">分配給存儲過程輸入?yún)?shù)的對象數(shù)組</param>????
??????? public static void FillDataset(SqlTransaction transaction, string spName,????
??????????? DataSet dataSet, string[] tableNames,????
??????????? params object[] parameterValues)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果有參數(shù)值???
??????????? if ((parameterValues != null) && (parameterValues.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 給存儲過程參數(shù)賦值???
??????????????? AssignParameterValues(commandParameters, parameterValues);
??????????????? // 調(diào)用重載方法???
??????????????? FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames, commandParameters);????
??????????? }????
??????????? else????
??????????? {????
??????????????? // 沒有參數(shù)值????
??????????????? FillDataset(transaction, CommandType.StoredProcedure, spName, dataSet, tableNames);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// [私有方法][內(nèi)部調(diào)用]執(zhí)行指定數(shù)據(jù)庫連接對象/事務(wù)的命令,映射數(shù)據(jù)表并填充數(shù)據(jù)集,DataSet/TableNames/SqlParameters.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? FillDataset(conn, trans, CommandType.StoredProcedure, "GetOrders", ds, new string[] {"orders"}, new SqlParameter("@prodid", 24));????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="transaction">一個有效的連接事務(wù)</param>????
??????? /// <param name="commandType">命令類型 (存儲過程,命令文本或其它)</param>????
??????? /// <param name="commandText">存儲過程名稱或T-SQL語句</param>????
??????? /// <param name="dataSet">要填充結(jié)果集的DataSet實例</param>????
??????? /// <param name="tableNames">表映射的數(shù)據(jù)表數(shù)組????
??????? /// 用戶定義的表名 (可有是實際的表名.)????
??????? /// </param>????
??????? /// <param name="commandParameters">分配給命令的SqlParamter參數(shù)數(shù)組</param>????
??????? private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType,????
??????????? string commandText, DataSet dataSet, string[] tableNames,????
??????????? params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (dataSet == null) throw new ArgumentNullException("dataSet");
??????????? // 創(chuàng)建SqlCommand命令,并進行預處理???
??????????? SqlCommand command = new SqlCommand();????
??????????? bool mustCloseConnection = false;????
??????????? PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection);
??????????? // 執(zhí)行命令???
??????????? using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))????
??????????? {
??????????????? // 追加表映射???
??????????????? if (tableNames != null && tableNames.Length > 0)????
??????????????? {????
??????????????????? string tableName = "Table";????
??????????????????? for (int index = 0; index < tableNames.Length; index++)????
??????????????????? {????
??????????????????????? 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");????
??????????????????????? dataAdapter.TableMappings.Add(tableName, tableNames[index]);????
??????????????????????? tableName += (index + 1).ToString();????
??????????????????? }????
??????????????? }
??????????????? // 填充數(shù)據(jù)集使用默認表名稱???
??????????????? dataAdapter.Fill(dataSet);
??????????????? // 清除參數(shù),以便再次使用.???
??????????????? command.Parameters.Clear();????
??????????? }
??????????? if (mustCloseConnection)???
??????????????? connection.Close();????
??????? }????
??????? #endregion
??????? #region UpdateDataset 更新數(shù)據(jù)集???
??????? /// <summary>????
??????? /// 執(zhí)行數(shù)據(jù)集更新到數(shù)據(jù)庫,指定inserted, updated, or deleted命令.????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? UpdateDataset(conn, insertCommand, deleteCommand, updateCommand, dataSet, "Order");????
??????? /// </remarks>????
??????? /// <param name="insertCommand">[追加記錄]一個有效的T-SQL語句或存儲過程</param>????
??????? /// <param name="deleteCommand">[刪除記錄]一個有效的T-SQL語句或存儲過程</param>????
??????? /// <param name="updateCommand">[更新記錄]一個有效的T-SQL語句或存儲過程</param>????
??????? /// <param name="dataSet">要更新到數(shù)據(jù)庫的DataSet</param>????
??????? /// <param name="tableName">要更新到數(shù)據(jù)庫的DataTable</param>????
??????? public static void UpdateDataset(SqlCommand insertCommand, SqlCommand deleteCommand, SqlCommand updateCommand, DataSet dataSet, string tableName)????
??????? {????
??????????? if (insertCommand == null) throw new ArgumentNullException("insertCommand");????
??????????? if (deleteCommand == null) throw new ArgumentNullException("deleteCommand");????
??????????? if (updateCommand == null) throw new ArgumentNullException("updateCommand");????
??????????? if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");
??????????? // 創(chuàng)建SqlDataAdapter,當操作完成后釋放.???
??????????? using (SqlDataAdapter dataAdapter = new SqlDataAdapter())????
??????????? {????
??????????????? // 設(shè)置數(shù)據(jù)適配器命令????
??????????????? dataAdapter.UpdateCommand = updateCommand;????
??????????????? dataAdapter.InsertCommand = insertCommand;????
??????????????? dataAdapter.DeleteCommand = deleteCommand;
??????????????? // 更新數(shù)據(jù)集改變到數(shù)據(jù)庫???
??????????????? dataAdapter.Update(dataSet, tableName);
??????????????? // 提交所有改變到數(shù)據(jù)集.???
??????????????? dataSet.AcceptChanges();????
??????????? }????
??????? }????
??????? #endregion
??????? #region CreateCommand 創(chuàng)建一條SqlCommand命令???
??????? /// <summary>????
??????? /// 創(chuàng)建SqlCommand命令,指定數(shù)據(jù)庫連接對象,存儲過程名和參數(shù).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 示例:?
??????? ///? SqlCommand command = CreateCommand(conn, "AddCustomer", "CustomerID", "CustomerName");????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="sourceColumns">源表的列名稱數(shù)組</param>????
??????? /// <returns>返回SqlCommand命令</returns>????
??????? public static SqlCommand CreateCommand(SqlConnection connection, string spName, params string[] sourceColumns)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 創(chuàng)建命令???
??????????? SqlCommand cmd = new SqlCommand(spName, connection);????
??????????? cmd.CommandType = CommandType.StoredProcedure;
??????????? // 如果有參數(shù)值???
??????????? if ((sourceColumns != null) && (sourceColumns.Length > 0))????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 將源表的列到映射到DataSet命令中.???
??????????????? for (int index = 0; index < sourceColumns.Length; index++)????
??????????????????? commandParameters[index].SourceColumn = sourceColumns[index];
??????????????? // Attach the discovered parameters to the SqlCommand object???
??????????????? AttachParameters(cmd, commandParameters);????
??????????? }
??????????? return cmd;???
??????? }????
??????? #endregion
??????? #region ExecuteNonQueryTypedParams 類型化參數(shù)(DataRow)???
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接字符串的存儲過程,使用DataRow做為參數(shù)值,返回受影響的行數(shù).????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQueryTypedParams(String connectionString, String spName, DataRow dataRow)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接對象的存儲過程,使用DataRow做為參數(shù)值,返回受影響的行數(shù).????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQueryTypedParams(SqlConnection connection, String spName, DataRow dataRow)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫事物的存儲過程,使用DataRow做為參數(shù)值,返回受影響的行數(shù).????
??????? /// </summary>????
??????? /// <param name="transaction">一個有效的連接事務(wù) object</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回影響的行數(shù)</returns>????
??????? public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // Sf the row has values, the store procedure parameters must be initialized???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }????
??????? #endregion
??????? #region ExecuteDatasetTypedParams 類型化參數(shù)(DataRow)???
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接字符串的存儲過程,使用DataRow做為參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet.</returns>????
??????? public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? //如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接對象的存儲過程,使用DataRow做為參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet.</returns>????
??????? ///????
??????? public static DataSet ExecuteDatasetTypedParams(SqlConnection connection, String spName, DataRow dataRow)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫事務(wù)的存儲過程,使用DataRow做為參數(shù)值,返回DataSet.????
??????? /// </summary>????
??????? /// <param name="transaction">一個有效的連接事務(wù) object</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回一個包含結(jié)果集的DataSet.</returns>????
??????? public static DataSet ExecuteDatasetTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? #endregion
??????? #region ExecuteReaderTypedParams 類型化參數(shù)(DataRow)???
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接字符串的存儲過程,使用DataRow做為參數(shù)值,返回DataReader.????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReaderTypedParams(String connectionString, String spName, DataRow dataRow)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接對象的存儲過程,使用DataRow做為參數(shù)值,返回DataReader.????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫事物的存儲過程,使用DataRow做為參數(shù)值,返回DataReader.????
??????? /// </summary>????
??????? /// <param name="transaction">一個有效的連接事務(wù) object</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回包含結(jié)果集的SqlDataReader</returns>????
??????? public static SqlDataReader ExecuteReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }????
??????? #endregion
??????? #region ExecuteScalarTypedParams 類型化參數(shù)(DataRow)???
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接字符串的存儲過程,使用DataRow做為參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalarTypedParams(String connectionString, String spName, DataRow dataRow)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接對象的存儲過程,使用DataRow做為參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalarTypedParams(SqlConnection connection, String spName, DataRow dataRow)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫事務(wù)的存儲過程,使用DataRow做為參數(shù)值,返回結(jié)果集中的第一行第一列.????
??????? /// </summary>????
??????? /// <param name="transaction">一個有效的連接事務(wù) object</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回結(jié)果集中的第一行第一列</returns>????
??????? public static object ExecuteScalarTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }????
??????? #endregion
??????? #region ExecuteXmlReaderTypedParams 類型化參數(shù)(DataRow)???
??????? /// <summary>????
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫連接對象的存儲過程,使用DataRow做為參數(shù)值,返回XmlReader類型的結(jié)果集.????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReaderTypedParams(SqlConnection connection, String spName, DataRow dataRow)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// 執(zhí)行指定連接數(shù)據(jù)庫事務(wù)的存儲過程,使用DataRow做為參數(shù)值,返回XmlReader類型的結(jié)果集.????
??????? /// </summary>????
??????? /// <param name="transaction">一個有效的連接事務(wù) object</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="dataRow">使用DataRow作為參數(shù)值</param>????
??????? /// <returns>返回XmlReader結(jié)果集對象.</returns>????
??????? public static XmlReader ExecuteXmlReaderTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)????
??????? {????
??????????? if (transaction == null) throw new ArgumentNullException("transaction");????
??????????? if (transaction != null && transaction.Connection == null) throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? // 如果row有值,存儲過程必須初始化.???
??????????? if (dataRow != null && dataRow.ItemArray.Length > 0)????
??????????? {????
??????????????? // 從緩存中加載存儲過程參數(shù),如果緩存中不存在則從數(shù)據(jù)庫中檢索參數(shù)信息并加載到緩存中. ()????
??????????????? SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
??????????????? // 分配參數(shù)值???
??????????????? AssignParameterValues(commandParameters, dataRow);
??????????????? return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);???
??????????? }????
??????????? else????
??????????? {????
??????????????? return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);????
??????????? }????
??????? }????
??????? #endregion
??? }
??? /// <summary>???
??? /// SqlHelperParameterCache提供緩存存儲過程參數(shù),并能夠在運行時從存儲過程中探索參數(shù).????
??? /// </summary>????
??? public sealed class SqlHelperParameterCache????
??? {????
??????? #region 私有方法,字段,構(gòu)造函數(shù)????
??????? // 私有構(gòu)造函數(shù),妨止類被實例化.????
??????? private SqlHelperParameterCache() { }
??????? // 這個方法要注意???
??????? private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
??????? /// <summary>???
??????? /// 探索運行時的存儲過程,返回SqlParameter參數(shù)數(shù)組.????
??????? /// 初始化參數(shù)值為 DBNull.Value.????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接</param>????
??????? /// <param name="spName">存儲過程名稱</param>????
??????? /// <param name="includeReturnValueParameter">是否包含返回值參數(shù)</param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? private static SqlParameter[] DiscoverSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? SqlCommand cmd = new SqlCommand(spName, connection);???
??????????? cmd.CommandType = CommandType.StoredProcedure;
??????????? connection.Open();???
??????????? // 檢索cmd指定的存儲過程的參數(shù)信息,并填充到cmd的Parameters參數(shù)集中.????
??????????? SqlCommandBuilder.DeriveParameters(cmd);????
??????????? connection.Close();????
??????????? // 如果不包含返回值參數(shù),將參數(shù)集中的每一個參數(shù)刪除.????
??????????? if (!includeReturnValueParameter)????
??????????? {????
??????????????? cmd.Parameters.RemoveAt(0);????
??????????? }
??????????? // 創(chuàng)建參數(shù)數(shù)組???
??????????? SqlParameter[] discoveredParameters = new SqlParameter[cmd.Parameters.Count];????
??????????? // 將cmd的Parameters參數(shù)集復制到discoveredParameters數(shù)組.????
??????????? cmd.Parameters.CopyTo(discoveredParameters, 0);
??????????? // 初始化參數(shù)值為 DBNull.Value.???
??????????? foreach (SqlParameter discoveredParameter in discoveredParameters)????
??????????? {????
??????????????? discoveredParameter.Value = DBNull.Value;????
??????????? }????
??????????? return discoveredParameters;????
??????? }
??????? /// <summary>???
??????? /// SqlParameter參數(shù)數(shù)組的深層拷貝.????
??????? /// </summary>????
??????? /// <param name="originalParameters">原始參數(shù)數(shù)組</param>????
??????? /// <returns>返回一個同樣的參數(shù)數(shù)組</returns>????
??????? private static SqlParameter[] CloneParameters(SqlParameter[] originalParameters)????
??????? {????
??????????? SqlParameter[] clonedParameters = new SqlParameter[originalParameters.Length];
??????????? for (int i = 0, j = originalParameters.Length; i < j; i++)???
??????????? {????
??????????????? clonedParameters[i] = (SqlParameter)((ICloneable)originalParameters[i]).Clone();????
??????????? }
??????????? return clonedParameters;???
??????? }
??????? #endregion 私有方法,字段,構(gòu)造函數(shù)結(jié)束
??????? #region 緩存方法
??????? /// <summary>???
??????? /// 追加參數(shù)數(shù)組到緩存.????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符串</param>????
??????? /// <param name="commandText">存儲過程名或SQL語句</param>????
??????? /// <param name="commandParameters">要緩存的參數(shù)數(shù)組</param>????
??????? public static void CacheParameterSet(string connectionString, string commandText, params SqlParameter[] commandParameters)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
??????????? string hashKey = connectionString + ":" + commandText;
??????????? paramCache[hashKey] = commandParameters;???
??????? }
??????? /// <summary>???
??????? /// 從緩存中獲取參數(shù)數(shù)組.????
??????? /// </summary>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符</param>????
??????? /// <param name="commandText">存儲過程名或SQL語句</param>????
??????? /// <returns>參數(shù)數(shù)組</returns>????
??????? public static SqlParameter[] GetCachedParameterSet(string connectionString, string commandText)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
??????????? string hashKey = connectionString + ":" + commandText;
??????????? SqlParameter[] cachedParameters = paramCache[hashKey] as SqlParameter[];???
??????????? if (cachedParameters == null)????
??????????? {????
??????????????? return null;????
??????????? }????
??????????? else????
??????????? {????
??????????????? return CloneParameters(cachedParameters);????
??????????? }????
??????? }
??????? #endregion 緩存方法結(jié)束
??????? #region 檢索指定的存儲過程的參數(shù)集
??????? /// <summary>???
??????? /// 返回指定的存儲過程的參數(shù)集????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 這個方法將查詢數(shù)據(jù)庫,并將信息存儲到緩存.????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? public static SqlParameter[] GetSpParameterSet(string connectionString, string spName)????
??????? {????
??????????? return GetSpParameterSet(connectionString, spName, false);????
??????? }
??????? /// <summary>???
??????? /// 返回指定的存儲過程的參數(shù)集????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 這個方法將查詢數(shù)據(jù)庫,并將信息存儲到緩存.????
??????? /// </remarks>????
??????? /// <param name="connectionString">一個有效的數(shù)據(jù)庫連接字符.</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="includeReturnValueParameter">是否包含返回值參數(shù)</param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? public static SqlParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)????
??????? {????
??????????? if (connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? using (SqlConnection connection = new SqlConnection(connectionString))???
??????????? {????
??????????????? return GetSpParameterSetInternal(connection, spName, includeReturnValueParameter);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// [內(nèi)部]返回指定的存儲過程的參數(shù)集(使用連接對象).????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 這個方法將查詢數(shù)據(jù)庫,并將信息存儲到緩存.????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接字符</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName)????
??????? {????
??????????? return GetSpParameterSet(connection, spName, false);????
??????? }
??????? /// <summary>???
??????? /// [內(nèi)部]返回指定的存儲過程的參數(shù)集(使用連接對象)????
??????? /// </summary>????
??????? /// <remarks>????
??????? /// 這個方法將查詢數(shù)據(jù)庫,并將信息存儲到緩存.????
??????? /// </remarks>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="includeReturnValueParameter">????
??????? /// 是否包含返回值參數(shù)????
??????? /// </param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName, bool includeReturnValueParameter)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? using (SqlConnection clonedConnection = (SqlConnection)((ICloneable)connection).Clone())????
??????????? {????
??????????????? return GetSpParameterSetInternal(clonedConnection, spName, includeReturnValueParameter);????
??????????? }????
??????? }
??????? /// <summary>???
??????? /// [私有]返回指定的存儲過程的參數(shù)集(使用連接對象)????
??????? /// </summary>????
??????? /// <param name="connection">一個有效的數(shù)據(jù)庫連接對象</param>????
??????? /// <param name="spName">存儲過程名</param>????
??????? /// <param name="includeReturnValueParameter">是否包含返回值參數(shù)</param>????
??????? /// <returns>返回SqlParameter參數(shù)數(shù)組</returns>????
??????? private static SqlParameter[] GetSpParameterSetInternal(SqlConnection connection, string spName, bool includeReturnValueParameter)????
??????? {????
??????????? if (connection == null) throw new ArgumentNullException("connection");????
??????????? if (spName == null || spName.Length == 0) throw new ArgumentNullException("spName");
??????????? string hashKey = connection.ConnectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter" : "");
??????????? SqlParameter[] cachedParameters;
??????????? cachedParameters = paramCache[hashKey] as SqlParameter[];???
??????????? if (cachedParameters == null)????
??????????? {????
??????????????? SqlParameter[] spParameters = DiscoverSpParameterSet(connection, spName, includeReturnValueParameter);????
??????????????? paramCache[hashKey] = spParameters;????
??????????????? cachedParameters = spParameters;????
??????????? }
??????????? return CloneParameters(cachedParameters);???
??????? }
??????? #endregion 參數(shù)集檢索結(jié)束
??? }???
}
如果要獲取連接數(shù)據(jù)連接對象或字符串的話,先要修改SQLHelper類中GetConnSting() 方法中的ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;才能調(diào)用。
轉(zhuǎn)載于:https://www.cnblogs.com/java20130723/archive/2012/10/27/3211506.html
總結(jié)
以上是生活随笔為你收集整理的微软官方的SqlHelper的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 体验最火的敏捷-SCRUM!(网络直播课
- 下一篇: hdu 1159 Common Subs