电脑教程中文网
首页  动态网站建设学习 程序  笑话  论坛 娱乐  交友 ADSL  峄城  成功者
中文名:电脑教程中文网,收集了大量的电脑教程! 编程技术文档 游戏开发 笑话站暂时关闭 设为首页
网页设计 HTML | Dreamweaver | CSS | Firework | FrontPage WEB开发 ASP | JSP | PHP | .NET | CGI | JS | VBS | XML | IIS6 | Apache | PWS
程序设计 Java | C++ |VC++ | C# | Delphi | VB | C语言 | 汇编 | Pascal | Perl 数据库 MSSQL | MySQL | Access | VF | Oracle | DB2 | SYBASE |
办公软件 Word | Excel | WPS | PowerPoint 动画平面 Photoshop | ACDSee | 3Dmax | Flash | Coreldraw |
操作系统 Windows 2000 | Windows XP | Windows 2003 | SCO Unix | Windows Vista | unix、Linux | 综合| 服务器 | 系统安全| 黑客技术
其  他 UltraDev | DOS | UML | PWS | Powerbuilder | 开发心得 | 设计理念 | 病毒库 | 其他 | LightTPD (分类排序给您带来不便请谅解)
推  荐: Java文档500篇》《ASP.NET与相关数据库技术高级指南》《TC图形函数详解》《C函数速查手册》《C语言编程宝典之一》《MFC深入浅出》《黑客零起点》《VC++ 编程指南》《JScript 用户指南》 《CSS教程宝典》《Microsoft Jet SQL 参考》《delphi技巧集合》《MySQL 4.1.0 中文参考手册》《MySQL中文手册
【导航】 您现在的位置 : 首页 - C教程 - 《C#技术文档收集》- C#实现C/S架构下的TREEVIEW只输入表名,父ID,节点ID,节点名就得到树型结构(4)

C#实现C/S架构下的TREEVIEW只输入表名,父ID,节点ID,节点名就得到树型结构(4)

日期:2005-9-6 14:14:50    作者:佚名   人气:   来源:网络




   return cmd;
  }
  #endregion

  #region ExecuteNonQueryTypedParams
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns no resultset) against the database specified in
  /// the connection string using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An int representing the number of rows affected by the command</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" );
   
   // If the row has values, the store procedure parameters must be initialized
   if (dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
                               
    return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified SqlConnection
  /// using the dataRow column values as the stored procedure's parameters values. 
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An int representing the number of rows affected by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if (dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
                               
    return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified
  /// SqlTransaction using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="transaction">A valid SqlTransaction object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An int representing the number of rows affected by the command</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)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
                               
    return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
   }
  }
  #endregion

  #region ExecuteDatasetTypedParams
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in
  /// the connection string using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A dataset containing the resultset generated by the command</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" );

   //If the row has values, the store procedure parameters must be initialized
   if ( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection
  /// using the dataRow column values as the store procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A dataset containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteDataset(connection, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on row values.
  /// </summary>
  /// <param name="transaction">A valid SqlTransaction object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A dataset containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
   }
  }

  #endregion

  #region ExecuteReaderTypedParams
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in
  /// the connection string using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A SqlDataReader containing the resultset generated by the command</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" );
   
   // If the row has values, the store procedure parameters must be initialized
   if ( dataRow != null && dataRow.ItemArray.Length > 0 )
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
   }
  }

               
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A SqlDataReader containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteReader(connection, CommandType.StoredProcedure, spName);
   }
  }
       
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="transaction">A valid SqlTransaction object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>A SqlDataReader containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0 )
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteReader(transaction, CommandType.StoredProcedure, spName);
   }
  }
  #endregion

  #region ExecuteScalarTypedParams
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the database specified in
  /// the connection string using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An object containing the value in the 1x1 resultset generated by the command</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" );
   
   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An object containing the value in the 1x1 resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteScalar(connection, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="transaction">A valid SqlTransaction object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An object containing the value in the 1x1 resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
   }
  }
  #endregion

  #region ExecuteXmlReaderTypedParams
  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An XmlReader containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteXmlReader(connection, CommandType.StoredProcedure, spName);
   }
  }

  /// <summary>
  /// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlTransaction
  /// using the dataRow column values as the stored procedure's parameters values.
  /// This method will query the database to discover the parameters for the
  /// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
  /// </summary>
  /// <param name="transaction">A valid SqlTransaction object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="dataRow">The dataRow used to hold the stored procedure's parameter values.</param>
  /// <returns>An XmlReader containing the resultset generated by the command</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" );

   // If the row has values, the store procedure parameters must be initialized
   if( dataRow != null && dataRow.ItemArray.Length > 0)
   {
    // Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
    SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
               
    // Set the parameters values
    AssignParameterValues(commandParameters, dataRow);
               
    return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
   }
   else
   {
    return SqlHelper.ExecuteXmlReader(transaction, CommandType.StoredProcedure, spName);
   }
  }
  #endregion

 }

 /// <summary>
 /// SqlHelperParameterCache provides functions to leverage a static cache of procedure parameters, and the
 /// ability to discover parameters for stored procedures at run-time.
 /// </summary>
 public sealed class SqlHelperParameterCache
 {
  #region private methods, variables, and constructors

  //Since this class provides only static methods, make the default constructor private to prevent
  //instances from being created with "new SqlHelperParameterCache()"
  private SqlHelperParameterCache() {}

  private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());

  /// <summary>
  /// Resolve at run time the appropriate set of SqlParameters for a stored procedure
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="includeReturnValueParameter">Whether or not to include their return value parameter</param>
  /// <returns>The parameter array discovered.</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();
   SqlCommandBuilder.DeriveParameters(cmd);
   connection.Close();

   if (!includeReturnValueParameter)
   {
    cmd.Parameters.RemoveAt(0);
   }
               
   SqlParameter[] discoveredParameters = new SqlParameter[cmd.Parameters.Count];

   cmd.Parameters.CopyTo(discoveredParameters, 0);

   // Init the parameters with a DBNull value
   foreach (SqlParameter discoveredParameter in discoveredParameters)
   {
    discoveredParameter.Value = DBNull.Value;
   }
   return discoveredParameters;
  }

  /// <summary>
  /// Deep copy of cached SqlParameter array
  /// </summary>
  /// <param name="originalParameters"></param>
  /// <returns></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 private methods, variables, and constructors

  #region caching functions

  /// <summary>
  /// Add parameter array to the cache
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="commandText">The stored procedure name or T-SQL command</param>
  /// <param name="commandParameters">An array of SqlParamters to be cached</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>
  /// Retrieve a parameter array from the cache
  /// </summary>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="commandText">The stored procedure name or T-SQL command</param>
  /// <returns>An array of SqlParamters</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 caching functions

  #region Parameter Discovery Functions

  /// <summary>
  /// Retrieves the set of SqlParameters appropriate for the stored procedure
  /// </summary>
  /// <remarks>
  /// This method will query the database for this information, and then store it in a cache for future requests.
  /// </remarks>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <returns>An array of SqlParameters</returns>
  public static SqlParameter[] GetSpParameterSet(string connectionString, string spName)
  {
   return GetSpParameterSet(connectionString, spName, false);
  }

  /// <summary>
  /// Retrieves the set of SqlParameters appropriate for the stored procedure
  /// </summary>
  /// <remarks>
  /// This method will query the database for this information, and then store it in a cache for future requests.
  /// </remarks>
  /// <param name="connectionString">A valid connection string for a SqlConnection</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="includeReturnValueParameter">A bool value indicating whether the return value parameter should be included in the results</param>
  /// <returns>An array of SqlParameters</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>
  /// Retrieves the set of SqlParameters appropriate for the stored procedure
  /// </summary>
  /// <remarks>
  /// This method will query the database for this information, and then store it in a cache for future requests.
  /// </remarks>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <returns>An array of SqlParameters</returns>
  internal static SqlParameter[] GetSpParameterSet(SqlConnection connection, string spName)
  {
   return GetSpParameterSet(connection, spName, false);
  }

  /// <summary>
  /// Retrieves the set of SqlParameters appropriate for the stored procedure
  /// </summary>
  /// <remarks>
  /// This method will query the database for this information, and then store it in a cache for future requests.
  /// </remarks>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="includeReturnValueParameter">A bool value indicating whether the return value parameter should be included in the results</param>
  /// <returns>An array of SqlParameters</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>
  /// Retrieves the set of SqlParameters appropriate for the stored procedure
  /// </summary>
  /// <param name="connection">A valid SqlConnection object</param>
  /// <param name="spName">The name of the stored procedure</param>
  /// <param name="includeReturnValueParameter">A bool value indicating whether the return value parameter should be included in the results</param>
  /// <returns>An array of SqlParameters</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 Parameter Discovery Functions

 }
}

 




网站首页 - 友情链接 - 公司简介 - 联系方式 - 广告投放 - 客户服务 - 错误报告 - 免责声明 - About us
CLDE.NET电脑教程中文网版权所有 未经许可禁止镜象和复制本站资料 MSN:CLDE_NET@hotmail.com
技术支持:CLDE.NET信息中心 鲁ICP备05039940号 友情链接QQ:784079(隐)