📄 sqlhelper.cs
字号:
// -commandParameters - an array of SqlParamters used to execute the command
// Returns: An object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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 ExecuteScalar(connection, commandType, commandText, commandParameters);
}
finally
{
if (connection != null)
{
connection.Dispose();
}
}
} // ExecuteScalar
// Execute a stored procedure via a SqlCommand (that returns a 1x1 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 orderCount As Integer = CInt(ExecuteScalar(connString, "GetOrderCount", 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: An object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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);
// Assign the provided values to these parameters based on parameter order
AssignParameterValues(commandParameters, parameterValues);
// Call the overload that takes an array of SqlParameters
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
// Otherwise we can just call the SP without params
}
else
{
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
}
} // ExecuteScalar
// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlConnection.
// e.g.:
// Dim orderCount As Integer = CInt(ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount"))
// Parameters:
// -connection - a valid SqlConnection
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: An object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(SqlConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteScalar(connection, commandType, commandText,((SqlParameter[]) null));
} // ExecuteScalar
// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlConnection
// using the provided parameters.
// e.g.:
// Dim orderCount As Integer = CInt(ExecuteScalar(conn, CommandType.StoredProcedure, "GetOrderCount", 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 object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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();
object retval;
bool mustCloseConnection = false;
PrepareCommand(cmd, connection,((SqlTransaction) null), commandType, commandText, commandParameters, ref mustCloseConnection);
// Execute the command & return the results
retval = cmd.ExecuteScalar();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
if (mustCloseConnection)
{
connection.Close();
}
return retval;
} // ExecuteScalar
// Execute a stored procedure via a SqlCommand (that returns a 1x1 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 orderCount As Integer = CInt(ExecuteScalar(conn, "GetOrderCount", 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 object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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 ExecuteScalar(connection, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteScalar(connection, CommandType.StoredProcedure, spName);
}
} // ExecuteScalar
// Execute a SqlCommand (that returns a 1x1 resultset and takes no parameters) against the provided SqlTransaction.
// e.g.:
// Dim orderCount As Integer = CInt(ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount"))
// Parameters:
// -transaction - a valid SqlTransaction
// -commandType - the CommandType (stored procedure, text, etc.)
// -commandText - the stored procedure name or T-SQL command
// Returns: An object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(SqlTransaction transaction, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteScalar(transaction, commandType, commandText,((SqlParameter[]) null));
} // ExecuteScalar
// Execute a SqlCommand (that returns a 1x1 resultset) against the specified SqlTransaction
// using the provided parameters.
// e.g.:
// Dim orderCount As Integer = CInt(ExecuteScalar(trans, CommandType.StoredProcedure, "GetOrderCount", 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 object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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();
object retval;
bool mustCloseConnection = false;
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters, ref mustCloseConnection);
// Execute the command & return the results
retval = cmd.ExecuteScalar();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
} // ExecuteScalar
// Execute a stored procedure via a SqlCommand (that returns a 1x1 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 orderCount As Integer = CInt(ExecuteScalar(trans, "GetOrderCount", 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 object containing the value in the 1x1 resultset generated by the command
public static object ExecuteScalar(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 ExecuteScalar(transaction, CommandType.StoredProcedure, spName, commandParameters);
}
else // Otherwise we can just call the SP without params
{
return ExecuteScalar(transaction, CommandType.StoredProcedure, spName);
}
} // ExecuteScalar
#endregion
#region "ExecuteXmlReader"
// Execute a SqlCommand (that returns a resultset and takes no parameters) against the provided SqlConnection.
// e.g.:
// Dim r As XmlReader = ExecuteXmlReader(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 using "FOR XML AUTO"
// Returns: An XmlReader containing the resultset generated by the command
public static XmlReader ExecuteXmlReader(SqlConnection connection, CommandType commandType, string commandText)
{
// Pass through the call providing null for the set of SqlParameters
return ExecuteXmlReader(connection, commandType, commandText,((SqlParameter[]) null));
} // ExecuteXmlReader
// Execute a SqlCommand (that returns a resultset) against the specified SqlConnection
// using the provided parameters.
// e.g.:
// Dim r As XmlReader = ExecuteXmlReader(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 using "FOR XML AUTO"
// -commandParameters - an array of SqlParamters used to execute the command
// Returns: An XmlReader containing the resultset generated by the command
pub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -