📄 sqlhelper.cs
字号:
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of SqlParameters
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
}
} // ExecuteDataset
// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders")
// Parameters:
// -connection - a valid SqlConnection
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteDataset(connection, commandType, commandText,((SqlParameter[]) null));
} // ExecuteDataset
// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection
// using the provided parameters.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(conn, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
// Parameters:
// -connection - a valid SqlConnection
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// -commandParameters - an array of SqlParamters used to execute the command
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlConnection connection, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (connection == null)
{
throw (new ArgumentNullException("connection"));
}
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter dataAdatpter = null;
bool mustCloseConnection = false;
PrepareCommand(cmd, connection,((SqlTransaction) null), commandType, commandText, commandParameters, ref mustCloseConnection);
try
{
// Create the DataAdapter & DataSet
dataAdatpter = new SqlDataAdapter(cmd);
// Fill the DataSet using default values for DataTable names, etc
dataAdatpter.Fill(ds);
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
}
finally
{
if (dataAdatpter != null)
{
dataAdatpter.Dispose();
}
}
if (mustCloseConnection)
{
connection.Close();
}
// Return the dataset
return ds;
} // ExecuteDataset
// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified SqlConnection
// using the provided parameter values. This method will discover the parameters for the
// stored procedure, and assign the values based on parameter order.
// This method provides no access to output parameters or the stored procedure' s return value parameter.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(conn, "GetOrders", 24, 36)
// Parameters:
// -connection - a valid SqlConnection
// -spName - the name of the stored procedure
// -parameterValues - an array of objects to be assigned as the input values of the stored procedure
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlConnection connection, string spName, params object[] parameterValues)
{
if (connection == null)
{
throw (new ArgumentNullException("connection"));
}
if (spName == null || spName.Length == 0)
{
throw (new ArgumentNullException("spName"));
}
SqlParameter[] commandParameters;
// If we receive parameter values, we need to figure out where they go
if (parameterValues != null && parameterValues.Length > 0)
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
commandParameters = SqlHelperParameterCache.GetSpParameterSet(connection, spName);
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of SqlParameters
return ExecuteDataset(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteDataset(connection, CommandType.StoredProcedure, spName);
}
} // ExecuteDataset
// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlTransaction.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders")
// Parameters
// -transaction - a valid SqlTransaction
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteDataset(transaction, commandType, commandText,((SqlParameter[]) null));
} // ExecuteDataset
// Execute a SqlCommand (that returns a resultset) against the specified SqlTransaction
// using the provided parameters.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(trans, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
// Parameters
// -transaction - a valid SqlTransaction
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// -commandParameters - an array of SqlParamters used to execute the command
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlTransaction transaction, CommandType commandType, string commandText, params SqlParameter[] commandParameters)
{
if (transaction == null)
{
throw (new ArgumentNullException("transaction"));
}
if (transaction != null && transaction.Connection == null)
{
throw (new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"));
}
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
SqlDataAdapter dataAdatpter = null;
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, ref mustCloseConnection);
try
{
// Create the DataAdapter & DataSet
dataAdatpter = new SqlDataAdapter(cmd);
// Fill the DataSet using default values for DataTable names, etc
dataAdatpter.Fill(ds);
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
}
finally
{
if (dataAdatpter != null)
{
dataAdatpter.Dispose();
}
}
// Return the dataset
return ds;
} // ExecuteDataset
// Execute a stored procedure via a SqlCommand (that returns a resultset) against the specified
// SqlTransaction using the provided parameter values. This method will discover the parameters for the
// stored procedure, and assign the values based on parameter order.
// This method provides no access to output parameters or the stored procedure' s return value parameter.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(trans, "GetOrders", 24, 36)
// Parameters:
// -transaction - a valid SqlTransaction
// -spName - the name of the stored procedure
// -parameterValues - an array of objects to be assigned as the input values of the stored procedure
// Returns: A dataset containing the resultset generated by the command
public static DataSet ExecuteDataset(SqlTransaction 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 rollbacked or commited, please provide an open transaction.", "transaction"));
}
if (spName == null || spName.Length == 0)
{
throw (new ArgumentNullException("spName"));
}
SqlParameter[] commandParameters;
// If we receive parameter values, we need to figure out where they go
if (parameterValues != null && parameterValues.Length > 0)
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of SqlParameters
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteDataset(transaction, CommandType.StoredProcedure, spName);
}
} // ExecuteDataset
#endregion
#region "ExecuteReader"
// this enum is used to indicate whether the connection was provided by the caller, or created by SqlHelper, so that
// we can set the appropriate CommandBehavior when calling ExecuteReader()
private enum SqlConnectionOwnership
{
// Connection is owned and managed by SqlHelper
Internal,
// Connection is owned and managed by the caller
@External
} // SqlConnectionOwnership
// Create and prepare a SqlCommand, and call ExecuteReader with the appropriate CommandBehavior.
// 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.
// Parameters:
// -connection - a valid SqlConnection, on which to execute this command
// -transaction - a valid SqlTransaction, or ' null'
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// -commandParameters - an array of SqlParameters to be associated with the command or ' null' if no parameters are required
// -connectionOwnership - indicates whether the connection parameter was provided by the caller, or created by SqlHelper
// Returns: SqlDataReader containing the results of the command
private static SqlDataReader ExecuteReader(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, SqlParameter[] commandParameters, SqlConnectionOwnership connectionOwnership)
{
if (connection == null)
{
throw (new ArgumentNullException("connection"));
}
bool mustCloseConnection = false;
// Create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
try
{
// Create a reader
SqlDataReader dataReader;
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters, ref mustCloseConnection);
// Call ExecuteReader with the appropriate CommandBehavior
if (connectionOwnership == SqlConnectionOwnership.External)
{
dataReader = cmd.ExecuteReader();
}
else
{
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
// Detach the SqlParameters from the command object, so they can be used again
bool canClear = true;
foreach (SqlParameter commandParameter in cmd.Parameters)
{
if (commandParameter.Direction != ParameterDirection.Input)
{
canClear = false;
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -