@ -3896,7 +3896,105 @@ VALUES (33, '{0}') END ";
string c = year + "-" + month + "-" + day ;
return c ;
}
}
public static int ExecuteNonQuery ( string connectionString , CommandType commandType , string commandText )
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery ( connectionString , commandType , commandText , ( SqlParameter [ ] ) null ) ;
}
public static int ExecuteNonQuery ( string connectionString , CommandType commandType , string commandText , params SqlParameter [ ] commandParameters )
{
if ( connectionString = = null | | connectionString . Length = = 0 ) throw new ArgumentNullException ( "connectionString" ) ;
// Create & open a SqlConnection, and dispose of it after we are done
using ( SqlConnection connection = new SqlConnection ( connectionString ) )
{
connection . Open ( ) ;
// Call the overload that takes a connection in place of the connection string
return ExecuteNonQuery ( connection , commandType , commandText , commandParameters ) ;
}
}
public static int ExecuteNonQuery ( 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 ( ) ;
bool mustCloseConnection = false ;
PrepareCommand ( cmd , connection , ( SqlTransaction ) null , commandType , commandText , commandParameters , out mustCloseConnection ) ;
// Finally, execute the command
int retval = cmd . ExecuteNonQuery ( ) ;
// Detach the SqlParameters from the command object, so they can be used again
cmd . Parameters . Clear ( ) ;
if ( mustCloseConnection )
connection . Close ( ) ;
return retval ;
}
private static void PrepareCommand ( SqlCommand command , SqlConnection connection , SqlTransaction transaction , CommandType commandType , string commandText , SqlParameter [ ] commandParameters , out bool mustCloseConnection )
{
if ( command = = null ) throw new ArgumentNullException ( "command" ) ;
if ( commandText = = null | | commandText . Length = = 0 ) throw new ArgumentNullException ( "commandText" ) ;
// If the provided connection is not open, we will open it
if ( connection . State ! = ConnectionState . Open )
{
mustCloseConnection = true ;
connection . Open ( ) ;
}
else
{
mustCloseConnection = false ;
}
// Associate the connection with the command
command . Connection = connection ;
// Set the command text (stored procedure name or SQL statement)
command . CommandText = commandText ;
// If we were provided a transaction, assign it
if ( transaction ! = null )
{
if ( transaction . Connection = = null ) throw new ArgumentException ( "The transaction was rollbacked or commited, please provide an open transaction." , "transaction" ) ;
command . Transaction = transaction ;
}
// Set the command type
command . CommandType = commandType ;
// Attach the command parameters if they are provided
if ( commandParameters ! = null )
{
AttachParameters ( command , commandParameters ) ;
}
return ;
}
private static void AttachParameters ( SqlCommand command , SqlParameter [ ] commandParameters )
{
if ( command = = null ) throw new ArgumentNullException ( "command" ) ;
if ( commandParameters ! = null )
{
foreach ( SqlParameter p in commandParameters )
{
if ( p ! = null )
{
// Check for derived output value with no value assigned
if ( ( p . Direction = = ParameterDirection . InputOutput | |
p . Direction = = ParameterDirection . Input ) & &
( p . Value = = null ) )
{
p . Value = DBNull . Value ;
}
command . Parameters . Add ( p ) ;
}
}
}
}
}
}