📄 adohelper.cs
字号:
if (command.Connection.State != ConnectionState.Open)
{
command.Connection.Open();
mustCloseConnection = true;
}
// Create the DataAdapter & DataSet
IDbDataAdapter da = null;
try
{
da = GetDataAdapter();
da.SelectCommand = command;
DataSet ds = new DataSet();
try
{
// Fill the DataSet using default values for DataTable names, etc
da.Fill(ds);
}
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;
}
// Detach the IDataParameters from the command object, so they can be used again
// Don't do this...screws up output params -- cjb
//command.Parameters.Clear();
// Return the DataSet
return ds;
}
finally
{
if (mustCloseConnection)
{
command.Connection.Close();
}
if( da != null )
{
IDisposable id = da as IDisposable;
if( id != null )
id.Dispose();
}
}
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset and takes no parameters) against the database specified in
/// the connection string.
/// </summary>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders");
/// </code></example>
/// <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>A DataSet containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if commandText is null</exception>
/// <returns>A DataSet containing the resultset generated by the command</returns>
public virtual DataSet ExecuteDataset(string connectionString, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteDataset(connectionString, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new IDbParameter("@prodid", 24));
/// </code></example>
/// <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 IDbParamters used to execute the command</param>
/// <returns>A DataSet containing the resultset generated 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 DataSet ExecuteDataset(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 ExecuteDataset(connection, commandType, commandText, commandParameters);
}
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns a 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>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(connString, "GetOrders", 24, 36);
/// </code></example>
/// <param name="connectionString">A valid connection string for an 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>A DataSet containing the resultset generated by the command</returns>
/// <exception cref="System.ArgumentNullException">Thrown if connectionString is null</exception>
/// <exception cref="System.ArgumentNullException">Thrown if spName is null</exception>
public virtual DataSet ExecuteDataset(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 discovery
if (AreParameterValuesIDataParameters(parameterValues, iDataParameterValues))
{
return ExecuteDataset(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 ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
}
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset and takes no parameters) against the provided IDbConnection.
/// </summary>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders");
/// </code></example>
/// <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>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 connection is null</exception>
public virtual DataSet ExecuteDataset(IDbConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of IDataParameters
return ExecuteDataset(connection, commandType, commandText, (IDataParameter[])null);
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset) against the specified IDbConnection
/// using the provided parameters.
/// </summary>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new IDataParameter("@prodid", 24));
/// </code></example>
/// <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 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 connection is null</exception>
public virtual DataSet ExecuteDataset(IDbConnection connection, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
if( connection == null ) throw new ArgumentNullException( "connection" );
// Create a command and prepare it for execution
IDbCommand cmd = connection.CreateCommand();
bool mustCloseConnection = false;
PrepareCommand(cmd, connection, (IDbTransaction)null, commandType, commandText, commandParameters, out mustCloseConnection );
CleanParameterSyntax(cmd);
DataSet ds = ExecuteDataset(cmd);
if( mustCloseConnection )
connection.Close();
// Return the DataSet
return ds;
}
/// <summary>
/// Execute a stored procedure via an IDbCommand (that returns a 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>
/// <example>
/// <code>
/// DataSet ds = helper.ExecuteDataset(conn, "GetOrders", 24, 36);
/// </code></example>
/// <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>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 connection is null</exception>
public virtual DataSet ExecuteDataset(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 discovery
if (AreParameterValuesIDataParameters(parameterValues, iDataParameterValues))
{
return ExecuteDataset(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 ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
}
}
else
{
// Otherwise we can just call the SP without params
return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
}
}
/// <summary>
/// Execute an IDbCommand (that returns a resultset and takes no parameters) against the provided IDbTransaction.
/// </summary>
/// <example><code>
/// DataSet ds = helper.ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -