/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified
/// SqlTransaction using the dataRow column values as the stored procedure's parameters 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 row values.
///
/// A valid SqlTransaction object
/// The name of the stored procedure
/// The dataRow used to hold the stored procedure's parameter values.
/// An int representing the number of rows affected by the command
public static int ExecuteNonQueryTypedParams(SqlTransaction transaction, String spName, DataRow dataRow)
{
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" );
// Sf the row has values, the store procedure parameters must be initialized
if (dataRow != null && dataRow.ItemArray.Length > 0)
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(transaction.Connection, spName);
// Set the parameters values
AssignParameterValues(commandParameters, dataRow);
return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return SqlHelper.ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
}
#endregion
#region ExecuteDatasetTypedParams
///
/// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in
/// the connection string using the dataRow column values as the stored procedure's parameters 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 row values.
///
/// A valid connection string for a SqlConnection
/// The name of the stored procedure
/// The dataRow used to hold the stored procedure's parameter values.
/// A dataset containing the resultset generated by the command
public static DataSet ExecuteDatasetTypedParams(string connectionString, String spName, DataRow dataRow)
{
if( connectionString == null || connectionString.Length == 0 ) throw new ArgumentNullException( "connectionString" );
if( spName == null || spName.Length == 0 ) throw new ArgumentNullException( "spName" );
//If the row has values, the store procedure parameters must be initialized
if ( dataRow != null && dataRow.ItemArray.Length > 0)
{
// Pull the parameters for this stored procedure from the parameter cache (or discover them & populate the cache)
SqlParameter[] commandParameters = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName);
// Set the parameters values
AssignParameterValues(commandParameters, dataRow);
return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
else
{
return SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8781179/viewspace-924812/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/8781179/viewspace-924812/
本文介绍了一种通过SqlCommand执行SQL存储过程的方法,该方法适用于返回或不返回结果集的情况,并利用DataRow作为存储过程参数值的载体。此外,还提供了一个实用的ExecuteNonQueryTypedParams方法用于执行不返回结果集的存储过程,以及一个ExecuteDatasetTypedParams方法来执行返回结果集的存储过程。
90

被折叠的 条评论
为什么被折叠?



