第一步:DBUtility层 用于底层操作SqlHelper(一)

2008-01-12 17:13

// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//==============================================================================

using System;
using System.Data;
using System.Xml;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace Buygo.DBUtility
{
///


/// The SqlHelper class is intended to encapsulate high performance, scalable best practices for
/// common uses of SqlClient.
///
public sealed class SqlHelper
{
        public static readonly string ConnectionStringLocalBuygoMember = ConfigurationSettings.AppSettings["BuygoMember"].ToString();
        public static readonly string ConnectionStringLocalFriednDB = ConfigurationSettings.AppSettings["FriednDB"].ToString();

   #region private utility methods & constructors

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

   ///


   /// This method is used to attach array of SqlParameters to a SqlCommand.
   ///
   /// This method will assign a value of DbNull to any parameter with a direction of
   /// InputOutput and a value of null.
   ///
   /// This behavior will prevent default values from being used, but
   /// this will be the less common case than an intended pure output parameter (derived as InputOutput)
   /// where the user provided no input value.
   ///
   /// The command to which the parameters will be added
   /// an array of SqlParameters tho be added to command
   private static void AttachParameters(SqlCommand command, SqlParameter[] commandParameters)
   {
    foreach (SqlParameter p in commandParameters)
    {
     //check for derived output value with no value assigned
     if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
     {
      p.Value = DBNull.Value;
     }
     command.Parameters.Add(p);
    }
   }

   ///


   /// This method assigns an array of values to an array of SqlParameters.
   ///
   /// array of SqlParameters to be assigned values
   /// array of objects holding the values to be assigned
   private static void AssignParameterValues(SqlParameter[] commandParameters, object[] parameterValues)
   {
    if ((commandParameters == null) || (parameterValues == null))
    {
     //do nothing if we get no data
     return;
    }

    // we must have the same number of values as we pave parameters to put them in
    if (commandParameters.Length != parameterValues.Length)
    {
     throw new ArgumentException("Parameter count does not match Parameter Value count.");
    }

    //iterate through the SqlParameters, assigning the values from the corresponding position in the
    //value array
    for (int i = 0, j = commandParameters.Length; i < j; i++)
    {
     commandParameters[i].Value = parameterValues[i];
    }
   }

   ///


   /// This method opens (if necessary) and assigns a connection, transaction, command type and parameters
   /// to the provided command.
   ///
   /// the SqlCommand to be prepared
   /// a valid SqlConnection, on which to execute this command
   /// a valid SqlTransaction, or 'null'
   /// the CommandType (stored procedure, text, etc.)
   /// the stored procedure name or T-SQL command
   /// an array of SqlParameters to be associated with the command or 'null' if no parameters are required
   private static void PrepareCommand(SqlCommand command, SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters)
   {
    //if the provided connection is not open, we will open it
    if (connection.State != ConnectionState.Open)
    {
     connection.Open();
    }

    //associate the connection with the command
    command.Connection = connection;

    //set the command text (stored procedure name or SQL statement)
    command.CommandText 

你可能感兴趣的:(parameters,command,dataset,string,database,null)