📄 sqlhelper.cs
字号:
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(connectionString, 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 ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, commandParameters);
// Otherwise we can just call the SP without params
}
else
{
return ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName);
}
} // ExecuteNonQuery
// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlConnection.
// e.g.:
// Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders")
// Parameters:
// -connection - a valid SqlConnection
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: An int representing the number of rows affected by the command
public static int ExecuteNonQuery(SqlConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(connection, commandType, commandText,((SqlParameter[]) null));
} // ExecuteNonQuery
// Execute a SqlCommand (that returns no resultset) against the specified SqlConnection
// using the provided parameters.
// e.g.:
// Dim result As Integer = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", 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: An int representing the number of rows affected by the command
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();
int retval;
bool mustCloseConnection = false;
PrepareCommand(cmd, connection,((SqlTransaction) null), commandType, commandText, commandParameters, ref mustCloseConnection);
// Finally, execute the command
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;
} // ExecuteNonQuery
// Execute a stored procedure via a SqlCommand (that returns no 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 result As integer = ExecuteNonQuery(conn, "PublishOrders", 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: An int representing the number of rows affected by the command
public static int ExecuteNonQuery(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 ExecuteNonQuery(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteNonQuery(connection, CommandType.StoredProcedure, spName);
}
} // ExecuteNonQuery
// Execute a SqlCommand (that returns no resultset and takes no parameters) against the provided SqlTransaction.
// e.g.:
// Dim result As Integer = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders")
// Parameters:
// -transaction - a valid SqlTransaction associated with the connection
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: An int representing the number of rows affected by the command
public static int ExecuteNonQuery(SqlTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteNonQuery(transaction, commandType, commandText,((SqlParameter[]) null));
} // ExecuteNonQuery
// Execute a SqlCommand (that returns no resultset) against the specified SqlTransaction
// using the provided parameters.
// e.g.:
// Dim result As Integer = ExecuteNonQuery(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: An int representing the number of rows affected by the command
public static int ExecuteNonQuery(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();
int retval;
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, ref mustCloseConnection);
// Finally, execute the command
retval = cmd.ExecuteNonQuery();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
} // ExecuteNonQuery
// Execute a stored procedure via a SqlCommand (that returns no 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 result As Integer = SqlHelper.ExecuteNonQuery(trans, "PublishOrders", 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: An int representing the number of rows affected by the command
public static int ExecuteNonQuery(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 ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteNonQuery(transaction, CommandType.StoredProcedure, spName);
}
} // ExecuteNonQuery
#endregion
#region "ExecuteDataset"
// Execute a SqlCommand (that returns a resultset and takes no parameters) against the database specified in
// the connection string.
// e.g.:
// Dim ds As DataSet = SqlHelper.ExecuteDataset("", commandType.StoredProcedure, "GetOrders")
// Parameters:
// -connectionString - a valid connection string for a 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(string connectionString, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteDataset(connectionString, commandType, commandText,((SqlParameter[]) null));
} // ExecuteDataset
// Execute a SqlCommand (that returns a resultset) against the database specified in the connection string
// using the provided parameters.
// e.g.:
// Dim ds As Dataset = ExecuteDataset(connString, CommandType.StoredProcedure, "GetOrders", new SqlParameter("@prodid", 24))
// Parameters:
// -connectionString - a valid connection string for a 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(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
SqlConnection connection = null;
try
{
connection = new SqlConnection(connectionString);
connection.Open();
// Call the overload that takes a connection in place of the connection string
return ExecuteDataset(connection, commandType, commandText, commandParameters);
}
finally
{
if (connection != null)
{
connection.Dispose();
}
}
} // ExecuteDataset
// Execute a stored procedure via a SqlCommand (that returns a resultset) against the database specified in
// the connection string 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(connString, "GetOrders", 24, 36)
// Parameters:
// -connectionString - a valid connection string for a 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(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"));
}
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(connectionString, spName);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -