📄 adohelper.cs
字号:
IDbCommand cmd = connection.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (IDbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
CleanParameterSyntax(cmd);
// Finally, execute the command
int retval = ExecuteNonQuery(cmd);
// Detach the IDataParameters from the command object, so they can be used again
// don't do this...screws up output parameters -- cjbreisch
// cmd.Parameters.Clear();
if( mustCloseConnection )
connection.Close();
return retval;
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns no resultset) against the specified IDbConnection
/// 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="connection">A valid IDbConnection</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>An int representing the number of rows affected 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 connection is null</exception>
public virtual int ExecuteNonQuery(IDbConnection connection, string spName, params object[] parameterValues)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
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(connection, 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(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 ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
}
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset and takes no parameters) against the provided IDbTransaction.
/// </summary>
/// <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>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 transaction is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction.Connection is null</exception>
public virtual int ExecuteNonQuery(IDbTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteNonQuery(transaction, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns no resultset) against the specified IDbTransaction
/// using the provided parameters.
/// </summary>
/// <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>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 transaction is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if transaction.Connection is null</exception>
public virtual int ExecuteNonQuery(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);
// Finally, execute the command
int retval = ExecuteNonQuery(cmd);
// Detach the IDataParameters from the command object, so they can be used again
// don't do this...screws up output parameters -- cjbreisch
// cmd.Parameters.Clear();
return retval;
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns no 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>
/// <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>An int representing the number of rows affected 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 int ExecuteNonQuery(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 discoveryu
if (AreParameterValuesIDataParameters(parameterValues, iDataParameterValues))
{
return ExecuteNonQuery(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 IDbParameters
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion ExecuteNonQuery
#region ExecuteReader
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the database specified in
/// the connection string.
/// </summary>
/// <param name="command">The IDbCommand object to use</param>
/// <returns>A IDataReader containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
public virtual IDataReader ExecuteReader(IDbCommand command)
{
return ExecuteReader(command, AdoConnectionOwnership.External);
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the database specified in
/// the connection string.
/// </summary>
/// <param name="command">The IDbCommand object to use</param>
/// <param name="connectionOwnership">Enum indicating whether the connection was created internally or externally.</param>
/// <returns>A IDataReader containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if command is null.</exception>
protected virtual IDataReader ExecuteReader(IDbCommand command, AdoConnectionOwnership connectionOwnership)
{
// Clean Up Parameter Syntax
CleanParameterSyntax(command);
if (command.Connection.State != ConnectionState.Open)
{
command.Connection.Open();
connectionOwnership = AdoConnectionOwnership.Internal;
}
// Create a reader
IDataReader dataReader;
// Call ExecuteReader with the appropriate CommandBehavior
if (connectionOwnership == AdoConnectionOwnership.External)
{
dataReader = command.ExecuteReader();
}
else
{
try
{
dataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
// Don't just throw ex. It changes the call stack. But we want the ex around for debugging, so...
Debug.WriteLine(ex);
throw;
}
}
ClearCommand( command );
return dataReader;
}
/// <summary>
/// Create and prepare an IDbCommand, and call ExecuteReader with the appropriate CommandBehavior.
/// </summary>
/// <remarks>
/// If we created and opened the connection, we want the connection to be closed when the DataReader is closed.
///
/// If the caller provided the connection, we want to leave it to them to manage.
/// </remarks>
/// <param name="connection">A valid IDbConnection, on which to execute this command</param>
/// <param name="transaction">A valid IDbTransaction, or 'null'</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 to be associated with the command or 'null' if no parameters are required</param>
/// <param name="connectionOwnership">Indicates whether the connection parameter was provided by the caller, or created by AdoHelper</param>
/// <returns>IDataReader containing the results of 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>
private IDataReader ExecuteReader(IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, IDataParameter[] commandParameters, AdoConnectionOwnership connectionOwnership)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
bool mustCloseConnection = false;
// Create a command and prepare it for execution
IDbCommand cmd = connection.CreateCommand();
try
{
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );
CleanParameterSyntax(cmd);
// override conenctionOwnership if we created the connection in PrepareCommand -- cjbreisch
if (mustCloseConnection)
{
connectionOwnership
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -