About DAAB Operation No.3

已经讲过SQL SERVER & ORACLE了,今天我们来说道说道OLEDB,也就是大家平时说的ODBC。它可以连接FOXPRO、ACESS、EXCEL等当前诸多的数据类型。如有任何疑问,请给我留言!

None.gif // ===============================================================================
None.gif
//  OleDbHelper based on Microsoft Data Access Application Block (DAAB) for .NET
None.gif
//   http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
None.gif
//
None.gif
//  OleDbHelper.cs
None.gif
//
None.gif
//  This file contains the implementations of the OleDbHelper and OleDbHelperParameterCache
None.gif
//  classes.
None.gif
//
None.gif
//  The DAAB for MS Ole Db access for Oracle has been tested in the context of this Nile implementation,
None.gif
//  but has not undergone the generic functional testing that the SQL version has gone through.
None.gif
//  You can use it in other .NET applications using Oracle databases.  For complete docs explaining how to use
None.gif
//  and how it's built go to the originl appblock link. 
None.gif
//  For this sample, the code resides in the Nile namespaces not the Microsoft.ApplicationBlocks namespace
None.gif
// ==============================================================================
None.gif

None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Xml;
None.gif
using  System.Data.OleDb;
None.gif
using  System.Collections;
None.gif
None.gif
None.gif
namespace  Microsoft.ApplicationBlocks.Data
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// The OleDbHelper class is intended to encapsulate high performance, scalable best practices for 
InBlock.gif    
/// common uses of OleDbClient.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class OleDbHelper
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
private utility methods & constructors#region private utility methods & constructors
InBlock.gif
InBlock.gif        
//Since this class provides only static methods, make the default constructor private to prevent 
InBlock.gif        
//instances from being created with "new OleDbHelper()".
ExpandedSubBlockStart.gifContractedSubBlock.gif
        private OleDbHelper() dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// This method is used to attach array's of OleDbParameters to an OleDbCommand.
InBlock.gif        
/// 
InBlock.gif        
/// This method will assign a value of DbNull to any parameter with a direction of
InBlock.gif        
/// InputOutput and a value of null.  
InBlock.gif        
/// 
InBlock.gif        
/// This behavior will prevent default values from being used, but
InBlock.gif        
/// this will be the less common case than an intended pure output parameter (derived as InputOutput)
InBlock.gif        
/// where the user provided no input value.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="command">The command to which the parameters will be added</param>
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">an array of OleDbParameters tho be added to command</param>

InBlock.gif        private static void AttachParameters(OleDbCommand command, OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
foreach (OleDbParameter p in commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//check for derived output value with no value assigned
InBlock.gif
                if ((p.Direction == ParameterDirection.InputOutput) && (p.Value == null))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    p.Value 
= DBNull.Value;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
InBlock.gif                command.Parameters.Add(p);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// This method assigns an array of values to an array of OleDbParameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="commandParameters">array of OleDbParameters to be assigned values</param>
ExpandedSubBlockEnd.gif        
/// <param name="parameterValues">array of objects holding the values to be assigned</param>

InBlock.gif        private static void AssignParameterValues(OleDbParameter[] commandParameters, object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ((commandParameters == null|| (parameterValues == null)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//do nothing if we get no data
InBlock.gif
                return;
ExpandedSubBlockEnd.gif            }

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

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// This method opens (if necessary) and assigns a connection, transaction, command type and parameters 
InBlock.gif        
/// to the provided command.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="command">the OleDbCommand to be prepared</param>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection, on which to execute this command</param>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction, or 'null'</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">an array of OleDbParameters to be associated with the command or 'null' if no parameters are required</param>

InBlock.gif        private static void PrepareCommand(OleDbCommand command, OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if the provided connection is not open, we will open it
InBlock.gif
            if (connection.State != ConnectionState.Open)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                connection.Open();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//associate the connection with the command
InBlock.gif
            command.Connection = connection;
InBlock.gif
InBlock.gif            
//set the command text (stored procedure name or OleDb statement)
InBlock.gif
            command.CommandText = commandText;
InBlock.gif
InBlock.gif            
//if we were provided a transaction, assign it.
InBlock.gif
            if (transaction != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                command.Transaction 
= transaction;
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
//set the command type
InBlock.gif
            command.CommandType = commandType;
InBlock.gif
InBlock.gif            
//attach the command parameters if they are provided
InBlock.gif
            if (commandParameters != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                AttachParameters(command, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 private utility methods & constructors
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecuteNonQuery#region ExecuteNonQuery
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns no resultset and takes no parameters) against the database specified in 
InBlock.gif        
/// the connection string. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteNonQuery(connectionString, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns no resultset) against the database specified in the connection string 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParameters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create & open an OleDbConnection, and dispose of it after we are done.
InBlock.gif
            using (OleDbConnection cn = new OleDbConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif
InBlock.gif                
//call the overload that takes a connection in place of the connection string
InBlock.gif
                return ExecuteNonQuery(cn, commandType, commandText, commandParameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns no resultset) against the database specified in 
InBlock.gif        
/// the connection string using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(connString, "PublishOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbDbCommand (that returns no resultset and takes no parameters) against the provided OleDbConnection. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbConnection connection, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteNonQuery(connection, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns no resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, connection, (OleDbTransaction)
null, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//finally, execute the command.
InBlock.gif
            return cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns no resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(conn, "PublishOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbConnection connection, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns no resultset and takes no parameters) against the provided OleDbTransaction. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbTransaction transaction, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteNonQuery(transaction, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns no resultset) against the specified OleDbTransaction
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//finally, execute the command.
InBlock.gif
            return cmd.ExecuteNonQuery();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns no resultset) against the specified 
InBlock.gif        
/// OleDbTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int result = ExecuteNonQuery(conn, trans, "PublishOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="spName">the name of the stored procedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an int representing the number of rows affected by the command</returns>

InBlock.gif        public static int ExecuteNonQuery(OleDbTransaction transaction, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populet the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 ExecuteNonQuery
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecuteDataSet#region ExecuteDataSet
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the database specified in 
InBlock.gif        
/// the connection string. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteDataset(connectionString, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the database specified in the connection string 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create & open an OleDbConnection, and dispose of it after we are done.
InBlock.gif
            using (OleDbConnection cn = new OleDbConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif
InBlock.gif                
//call the overload that takes a connection in place of the connection string
InBlock.gif
                return ExecuteDataset(cn, commandType, commandText, commandParameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the database specified in 
InBlock.gif        
/// the conneciton string using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(connString, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored procedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(string connectionString, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populet the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the provided OleDbConnection. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteDataset(connection, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, connection, (OleDbTransaction)
null, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//create the DataAdapter & DataSet
InBlock.gif
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif
InBlock.gif            
//fill the DataSet using default values for DataTable names, etc.
InBlock.gif
            da.Fill(ds);
InBlock.gif            
InBlock.gif            
//return the dataset
InBlock.gif
            return ds;                        
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(conn, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbConnection connection, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the provided OleDbTransaction. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbTransaction transaction, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteDataset(transaction, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the specified OleDbTransaction
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//create the DataAdapter & DataSet
InBlock.gif
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
InBlock.gif            DataSet ds 
= new DataSet();
InBlock.gif
InBlock.gif            
//fill the DataSet using default values for DataTable names, etc.
InBlock.gif
            da.Fill(ds);
InBlock.gif            
InBlock.gif            
//return the dataset
InBlock.gif
            return ds;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the specified 
InBlock.gif        
/// OleDbTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  DataSet ds = ExecuteDataset(trans, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>a dataset containing the resultset generated by the command</returns>

InBlock.gif        public static DataSet ExecuteDataset(OleDbTransaction transaction, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 ExecuteDataSet
InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecuteReader#region ExecuteReader
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// this enum is used to indicate weather the connection was provided by the caller, or created by OleDbHelper, so that
InBlock.gif        
/// we can set the appropriate CommandBehavior when calling ExecuteReader()
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private enum OleDbConnectionOwnership    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>Connection is owned and managed by OleDbHelper</summary>
InBlock.gif            Internal, 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//// <summary>Connection is owned and managed by the caller</summary>
InBlock.gif            External
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Create and prepare an OleDbCommand, and call ExecuteReader with the appropriate CommandBehavior.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
InBlock.gif        
/// 
InBlock.gif        
/// If the caller provided the connection, we want to leave it to them to manage.
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection, on which to execute this command</param>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction, or 'null'</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParameters to be associated with the command or 'null' if no parameters are required</param>
InBlock.gif        
/// <param name="connectionOwnership">indicates whether the connection parameter was provided by the caller, or created by OleDbHelper</param>
ExpandedSubBlockEnd.gif        
/// <returns>OleDbDataReader containing the results of the command</returns>

InBlock.gif        private static OleDbDataReader ExecuteReader(OleDbConnection connection, OleDbTransaction transaction, CommandType commandType, string commandText, OleDbParameter[] commandParameters, OleDbConnectionOwnership connectionOwnership)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//create a reader
InBlock.gif
            OleDbDataReader dr;
InBlock.gif
InBlock.gif            
// call ExecuteReader with the appropriate CommandBehavior
InBlock.gif
            if (connectionOwnership == OleDbConnectionOwnership.External)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dr 
= cmd.ExecuteReader();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                dr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return dr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the database specified in 
InBlock.gif        
/// the connection string. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteReader(connectionString, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the database specified in the connection string 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(connString, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParameters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create & open an OleDbbConnection
InBlock.gif
            OleDbConnection cn = new OleDbConnection(connectionString);
InBlock.gif            cn.Open();
InBlock.gif
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//call the private overload that takes an internally owned connection in place of the connection string
InBlock.gif
                return ExecuteReader(cn, null, commandType, commandText, commandParameters, OleDbConnectionOwnership.Internal);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//if we fail to return the OleDbDataReader, we neeed to close the connection ourselves
InBlock.gif
                cn.Close();
InBlock.gif                
throw;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the database specified in 
InBlock.gif        
/// the connection string using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(connString, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(string connectionString, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteReader(connectionString, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteReader(connectionString, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the provided OleDbConnection. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbConnection connection, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteReader(connection, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(conn, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call to the private overload using a null transaction value and an externally owned connection
InBlock.gif
            return ExecuteReader(connection, (OleDbTransaction)null, commandType, commandText, commandParameters, OleDbConnectionOwnership.External);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(conn, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored procedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbConnection connection, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OleDbParameter[] commandParameters 
= OleDbHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
return ExecuteReader(connection, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteReader(connection, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset and takes no parameters) against the provided OleDbTransaction. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param>  
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteReader(transaction, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute an OleDbCommand (that returns a resultset) against the specified OleDbTransaction
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///   OleDbDataReader dr = ExecuteReader(trans, CommandType.StoredProcedure, "GetOrders", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParameters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through to private overload, indicating that the connection is owned by the caller
InBlock.gif
            return ExecuteReader(transaction.Connection, transaction, commandType, commandText, commandParameters, OleDbConnectionOwnership.External);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via an OleDbCommand (that returns a resultset) against the specified
InBlock.gif        
/// OleDbTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  OleDbDataReader dr = ExecuteReader(trans, "GetOrders", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an OleDbDataReader containing the resultset generated by the command</returns>

InBlock.gif        public static OleDbDataReader ExecuteReader(OleDbTransaction transaction, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                OleDbParameter[] commandParameters 
= OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
return ExecuteReader(transaction, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteReader(transaction, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 ExecuteReader
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
ExecuteScalar#region ExecuteScalar
InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset and takes no parameters) against the database specified in 
InBlock.gif        
/// the connection string. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteScalar(connectionString, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset) against the database specified in the connection string 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(connString, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create & open a OleDbConnection, and dispose of it after we are done.
InBlock.gif
            using (OleDbConnection cn = new OleDbConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif
InBlock.gif                
//call the overload that takes a connection in place of the connection string
InBlock.gif
                return ExecuteScalar(cn, commandType, commandText, commandParameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via a OleDbCommand (that returns a 1x1 resultset) against the database specified in 
InBlock.gif        
/// the conneciton string using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populet the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset and takes no parameters) against the provided OleDbConnection. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbConnection connection, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteScalar(connection, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbConnection connection, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, connection, (OleDbTransaction)
null, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//execute the command & return the results
InBlock.gif
            return cmd.ExecuteScalar();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via a OleDbCommand (that returns a 1x1 resultset) against the specified OleDbConnection 
InBlock.gif        
/// using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(conn, "GetOrderCount", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connection">a valid OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbConnection connection, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populet the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset and takes no parameters) against the provided OleDbTransaction. 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount");
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbTransaction transaction, CommandType commandType, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//pass through the call providing null for the set of OleDbParameters
InBlock.gif
            return ExecuteScalar(transaction, commandType, commandText, (OleDbParameter[])null);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a OleDbCommand (that returns a 1x1 resultset) against the specified OleDbTransaction
InBlock.gif        
/// using the provided parameters.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", new OleDbParameter("@prodid", 24));
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
InBlock.gif        
/// <param name="commandParameters">an array of OleDbParamters used to execute the command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbTransaction transaction, CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//create a command and prepare it for execution
InBlock.gif
            OleDbCommand cmd = new OleDbCommand();
InBlock.gif            PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
InBlock.gif            
InBlock.gif            
//execute the command & return the results
InBlock.gif
            return cmd.ExecuteScalar();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Execute a stored procedure via a OleDbCommand (that returns a 1x1 resultset) against the specified
InBlock.gif        
/// OleDbTransaction using the provided parameter values.  This method will query the database to discover the parameters for the 
InBlock.gif        
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method provides no access to output parameters or the stored procedure's return value parameter.
InBlock.gif        
/// 
InBlock.gif        
/// e.g.:  
InBlock.gif        
///  int orderCount = (int)ExecuteScalar(trans, "GetOrderCount", 24, 36);
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="transaction">a valid OleDbTransaction</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>

InBlock.gif        public static object ExecuteScalar(OleDbTransaction transaction, string spName, params object[] parameterValues)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//if we got parameter values, we need to figure out where they go
InBlock.gif
            if ((parameterValues != null&& (parameterValues.Length > 0)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//pull the parameters for this stored procedure from the parameter cache (or discover them & populet the cache)
InBlock.gif
                OleDbParameter[] commandParameters = OleDbHelperParameterCache.GetSpParameterSet(transaction.Connection.ConnectionString, spName);
InBlock.gif
InBlock.gif                
//assign the provided values to these parameters based on parameter order
InBlock.gif
                AssignParameterValues(commandParameters, parameterValues);
InBlock.gif
InBlock.gif                
//call the overload that takes an array of OleDbParameters
InBlock.gif
                return ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
ExpandedSubBlockEnd.gif            }

InBlock.gif                
//otherwise we can just call the SP without params
InBlock.gif
            else 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 ExecuteScalar
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// OleDbHelperParameterCache provides functions to leverage a static cache of procedure parameters, and the
InBlock.gif    
/// ability to discover parameters for stored procedures at run-time.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class OleDbHelperParameterCache
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
private methods, variables, and constructors#region private methods, variables, and constructors
InBlock.gif
InBlock.gif        
//Since this class provides only static methods, make the default constructor private to prevent 
InBlock.gif        
//instances from being created with "new OleDbHelperParameterCache()".
ExpandedSubBlockStart.gifContractedSubBlock.gif
        private OleDbHelperParameterCache() dot.gif{}
InBlock.gif
InBlock.gif        
private static Hashtable paramCache = Hashtable.Synchronized(new Hashtable());
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// resolve at run-time the appropriate set of OleDbParameters for a stored procedure
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">weather or not to onclude ther return value parameter</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        private static OleDbParameter[] DiscoverSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (OleDbConnection cn = new OleDbConnection(connectionString)) 
InBlock.gif            
using (OleDbCommand cmd = new OleDbCommand(spName,cn))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cn.Open();
InBlock.gif                cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif                OleDbCommandBuilder.DeriveParameters(cmd);
InBlock.gif
InBlock.gif                
if (!includeReturnValueParameter) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if (ParameterDirection.ReturnValue == cmd.Parameters[0].Direction)
InBlock.gif                        cmd.Parameters.RemoveAt(
0);
ExpandedSubBlockEnd.gif                }

InBlock.gif            
InBlock.gif                OleDbParameter[] discoveredParameters 
= new OleDbParameter[cmd.Parameters.Count];
InBlock.gif
InBlock.gif                cmd.Parameters.CopyTo(discoveredParameters, 
0);
InBlock.gif
InBlock.gif                
return discoveredParameters;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//deep copy of cached OleDbParameter array
InBlock.gif
        private static OleDbParameter[] CloneParameters(OleDbParameter[] originalParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OleDbParameter[] clonedParameters 
= new OleDbParameter[originalParameters.Length];
InBlock.gif
InBlock.gif            
for (int i = 0, j = originalParameters.Length; i < j; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                clonedParameters[i] 
= (OleDbParameter)((ICloneable)originalParameters[i]).Clone();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return clonedParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 private methods, variables, and constructors
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
caching functions#region caching functions
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// add parameter array to the cache
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">an array of OleDbParamters to be cached</param>

InBlock.gif        public static void CacheParameterSet(string connectionString, string commandText, params OleDbParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string hashKey = connectionString + ":" + commandText;
InBlock.gif
InBlock.gif            paramCache[hashKey] 
= commandParameters;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// retrieve a parameter array from the cache
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="commandText">the stored procedure name or T-OleDb command</param>
ExpandedSubBlockEnd.gif        
/// <returns>an array of OleDbParameters</returns>

InBlock.gif        public static OleDbParameter[] GetCachedParameterSet(string connectionString, string commandText)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string hashKey = connectionString + ":" + commandText;
InBlock.gif
InBlock.gif            OleDbParameter[] cachedParameters 
= (OleDbParameter[])paramCache[hashKey];
InBlock.gif            
InBlock.gif            
if (cachedParameters == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{            
InBlock.gif                
return null;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return CloneParameters(cachedParameters);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 caching functions
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Parameter Discovery Functions#region Parameter Discovery Functions
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Retrieves the set of OleDbParameters appropriate for the stored procedure
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method will query the database for this information, and then store it in a cache for future requests.
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for a OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored prcedure</param>
ExpandedSubBlockEnd.gif        
/// <returns>an array of OleDbParameters</returns>

InBlock.gif        public static OleDbParameter[] GetSpParameterSet(string connectionString, string spName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return GetSpParameterSet(connectionString, spName, true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Retrieves the set of OleDbParameters appropriate for the stored procedure
InBlock.gif        
/// </summary>
InBlock.gif        
/// <remarks>
InBlock.gif        
/// This method will query the database for this information, and then store it in a cache for future requests.
InBlock.gif        
/// </remarks>
InBlock.gif        
/// <param name="connectionString">a valid connection string for an OleDbConnection</param>
InBlock.gif        
/// <param name="spName">the name of the stored procedure</param>
InBlock.gif        
/// <param name="includeReturnValueParameter">a bool value indicating weather the return value parameter should be included in the results</param>
ExpandedSubBlockEnd.gif        
/// <returns>an array of OleDbParameters</returns>

InBlock.gif        public static OleDbParameter[] GetSpParameterSet(string connectionString, string spName, bool includeReturnValueParameter)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string hashKey = connectionString + ":" + spName + (includeReturnValueParameter ? ":include ReturnValue Parameter":"");
InBlock.gif
InBlock.gif            OleDbParameter[] cachedParameters;
InBlock.gif            
InBlock.gif            cachedParameters 
= (OleDbParameter[])paramCache[hashKey];
InBlock.gif
InBlock.gif            
if (cachedParameters == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{            
InBlock.gif                cachedParameters 
= (OleDbParameter[])(paramCache[hashKey] = DiscoverSpParameterSet(connectionString, spName, includeReturnValueParameter));
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
return CloneParameters(cachedParameters);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion
 Parameter Discovery Functions
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/kerr/archive/2006/03/10/347155.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值