📄 adohelper.cs
字号:
/// </code></example>
/// <param name="transaction">A valid IDbTransaction</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <returns>A DataSet containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction.Connection is null</exception>
public virtual DataSet ExecuteDataset(IDbTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteDataset(transaction, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the specified IDbTransaction
/// using the provided parameters.
/// </summary>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new IDataParameter("@prodid", 24));
/// </code></example>
/// <param name="transaction">A valid IDbTransaction</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <param name="commandParameters">An array of IDataParameters used to execute the command</param>
/// <returns>A DataSet containing the resultset generated by the command</returns>
/// <exception cref="System.InvalidOperationException">Thrown if any of the IDataParameters.ParameterNames are null, or if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction.Connection is null</exception>
public virtual DataSet ExecuteDataset(IDbTransaction transaction, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rolled back or commited, please provide an open transaction.", "transaction" );
// Create a command and prepare it for execution
IDbCommand cmd = transaction.Connection.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );
CleanParameterSyntax(cmd);
return ExecuteDataset(cmd);
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns a resultset) against the specified
/// IDbTransaction using the provided parameter values. This method will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored procedure's return value parameter.
/// </remarks>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(tran, "GetOrders", 24, 36);
/// </code></example>
/// <param name="transaction">A valid IDbTransaction</param>
/// <param name="spName">The name of the stored procedure</param>
/// <param name="parameterValues">An array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>A DataSet containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if spName is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction.Connection is null</exception>
public virtual DataSet ExecuteDataset(IDbTransaction transaction, string spName, params object[] parameterValues)
{
if( transaction == null ) throw new ArgumentNullException( "transaction" );
if( transaction != null && transaction.Connection == null ) throw new ArgumentException( "The transaction was rolled back or commited, please provide an open transaction.", "transaction" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );
// If we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
IDataParameter[] iDataParameterValues = GetDataParameters(parameterValues.Length);
// if we've been passed IDataParameters, don't do parameter discovery
if (AreParameterValuesIDataParameters(parameterValues, iDataParameterValues))
{
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, iDataParameterValues);
}
else
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
bool includeReturnValue = CheckForReturnValueParameter(parameterValues);
IDataParameter[] commandParameters = GetSpParameterSet(transaction.Connection, spName, includeReturnValue);
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of IDataParameters
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion ExecuteDataset
#region ExecuteNonQuery
/// <summary>
/// Execute an IDbCommand (that returns no resultset) against the database
/// </summary>
/// <param name="command">The IDbCommand to execute</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
public virtual int ExecuteNonQuery(IDbCommand command)
{
bool mustCloseConnection = false;
// Clean Up Parameter Syntax
CleanParameterSyntax(command);
if (command.Connection.State != ConnectionState.Open)
{
command.Connection.Open();
mustCloseConnection = true;
}
if (command == null) throw new ArgumentNullException("command");
int returnVal;
returnVal = command.ExecuteNonQuery();
if (mustCloseConnection)
{
command.Connection.Close();
}
return returnVal;
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset and takes no parameters) against the database specified in
/// the connection string
/// </summary>
/// <param name="connectionString">A valid connection string for an IDbConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
public virtual int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteNonQuery(connectionString, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters
/// </summary>
/// <param name="connectionString">A valid connection string for an IDbConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <param name="commandParameters">An array of IDataParameters used to execute the command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
/// <exception cref="System.InvalidOperationException">Thrown if any of the IDataParameters.ParameterNames are null, or if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the parameter count does not match the number of values supplied</exception>
public virtual int ExecuteNonQuery(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );
// Create & open an IDbConnection, and dispose of it after we are done
using (IDbConnection connection = GetConnection(connectionString))
{
connection.Open();
// Call the overload that takes a connection in place of the connection string
return ExecuteNonQuery(connection, commandType, commandText, commandParameters);
}
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns no resultset) against the database specified in
/// the connection string using the provided parameter values. This method will query the database to discover the parameters for the
/// stored procedure (the first time each stored procedure is called), and assign the values based on parameter order.
/// </summary>
/// <remarks>
/// This method provides no access to output parameters or the stored procedure's return value parameter.
///
/// </remarks>
/// <param name="connectionString">A valid connection string for an IDbConnection</param>
/// <param name="spName">The name of the stored prcedure</param>
/// <param name="parameterValues">An array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if spName is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the parameter count does not match the number of values supplied</exception>
public virtual int ExecuteNonQuery(string connectionString, string spName, params object[] parameterValues)
{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );
// If we receive parameter values, we need to figure out where they go
if ((parameterValues != null) && (parameterValues.Length > 0))
{
IDataParameter[] iDataParameterValues = GetDataParameters(parameterValues.Length);
// if we've been passed IDataParameters, don't do parameter discoveryu
if (AreParameterValuesIDataParameters(parameterValues, iDataParameterValues))
{
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, iDataParameterValues);
}
else
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
bool includeReturnValue = CheckForReturnValueParameter(parameterValues);
IDataParameter[] commandParameters = GetSpParameterSet(connectionString, spName, includeReturnValue);
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of IDataParameters
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset and takes no parameters) against the provided IDbConnection.
/// </summary>
/// <param name="connection">A valid IDbConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if connection is null</exception>
public virtual int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteNonQuery(connection, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset) against the specified IDbConnection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// </remarks>
/// <param name="connection">A valid IDbConnection</param>
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">The stored procedure name or SQL command</param>
/// <param name="commandParameters">An array of IDbParamters used to execute the command</param>
/// <returns>An int representing the number of rows affected by the command</returns>
/// <exception cref="System.InvalidOperationException">Thrown if any of the IDataParameters.ParameterNames are null, or if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <exception cref="System.ArgumentException">Thrown if the parameter count does not match the number of values supplied</exception>
/// <exception cref="System.ArgumentNullException">Thrown if connection is null</exception>
public virtual int ExecuteNonQuery(IDbConnection connection, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
// Create a command and prepare it for execution
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -