📄 sqlhelper.cs
字号:
/// <returns>asynchronous result object</returns>
public static IAsyncResult BeginExecuteNonQuery
(IDbConnection connection, CommandType commandType, string commandText, AsyncCallback ac, Object state, params IDataParameter[] commandParameters)
{
ExecuteNonQueryDelegate5 d = new ExecuteNonQueryDelegate5(ExecuteNonQuery);
IAsyncResult result = d.BeginInvoke(connection, commandType, commandText, commandParameters, ac, state);
return result;
}
/// <summary>
/// Execute a stored procedure via a command (that returns no resultset) against the specified IDbConnection
/// using the provided parameter 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 parameter order.
/// </summary>
/// <param name="connection">a valid IDbConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="ac">callback object containing a delegate</param>
/// <param name="state">state object supplied by the client</param>
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>asynchronous result object</returns>
public static IAsyncResult BeginExecuteNonQuery
(IDbConnection connection, string spName, AsyncCallback ac, Object state, params object[] parameterValues)
{
ExecuteNonQueryDelegate6 d = new ExecuteNonQueryDelegate6(ExecuteNonQuery);
IAsyncResult result = d.BeginInvoke(connection, spName, parameterValues, ac, state);
return result;
}
/// <summary>
/// Execute a command that returns no resultset
/// </summary>
/// <param name="transaction">a valid IDbTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="ac">callback object containing a delegate</param>
/// <param name="state">state object supplied by the client</param>
/// <returns>asynchronous result object</returns>
public static IAsyncResult BeginExecuteNonQuery
(IDbTransaction transaction, CommandType commandType, string commandText, AsyncCallback ac, Object state)
{
ExecuteNonQueryDelegate7 d = new ExecuteNonQueryDelegate7(ExecuteNonQuery);
IAsyncResult result = d.BeginInvoke(transaction, commandType, commandText, ac, state);
return result;
}
/// <summary>
/// Execute a command that returns no resultset
/// </summary>
/// <param name="transaction">a valid IDbTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="ac">callback object containing a delegate</param>
/// <param name="state">state object supplied by the client</param>
/// <param name="commandParameters">an array of IDataParameters used to execute the command</param>
/// <returns>asynchronous result object</returns>
public static IAsyncResult BeginExecuteNonQuery
(IDbTransaction transaction, CommandType commandType, string commandText, AsyncCallback ac, Object state, params IDataParameter[] commandParameters)
{
ExecuteNonQueryDelegate8 d = new ExecuteNonQueryDelegate8(ExecuteNonQuery);
IAsyncResult result = d.BeginInvoke(transaction, commandType, commandText, commandParameters, ac, state);
return result;
}
/// <summary>
/// Execute a stored procedure via a SqlCommand (that returns no resultset) against the specified
/// IDbTransaction using the provided parameter 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 parameter order.
/// </summary>
/// <param name="transaction">a valid IDbTransaction</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="ac">callback object containing a delegate</param>
/// <param name="state">state object supplied by the client</param>
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>asynchronous result object</returns>
public static IAsyncResult BeginExecuteNonQuery
(IDbTransaction transaction, string spName, AsyncCallback ac, Object state, params object[] parameterValues)
{
ExecuteNonQueryDelegate9 d = new ExecuteNonQueryDelegate9(ExecuteNonQuery);
IAsyncResult result = d.BeginInvoke(transaction, spName, parameterValues, ac, state);
return result;
}
/// <summary>
/// Receive the result at the end of an asynchronous call.
/// </summary>
/// <param name="result">asynchronous result object</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int EndExecuteNonQuery(IAsyncResult result)
{
if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate1))
{
ExecuteNonQueryDelegate1 d = (ExecuteNonQueryDelegate1) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate2))
{
ExecuteNonQueryDelegate2 d = (ExecuteNonQueryDelegate2) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate3))
{
ExecuteNonQueryDelegate3 d = (ExecuteNonQueryDelegate3) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate4))
{
ExecuteNonQueryDelegate4 d = (ExecuteNonQueryDelegate4) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate5))
{
ExecuteNonQueryDelegate5 d = (ExecuteNonQueryDelegate5) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate6))
{
ExecuteNonQueryDelegate6 d = (ExecuteNonQueryDelegate6) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate7))
{
ExecuteNonQueryDelegate7 d = (ExecuteNonQueryDelegate7) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate8))
{
ExecuteNonQueryDelegate8 d = (ExecuteNonQueryDelegate8) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
else //if ((((AsyncResult)result).AsyncDelegate).GetType() == typeof(ExecuteNonQueryDelegate9))
{
ExecuteNonQueryDelegate9 d = (ExecuteNonQueryDelegate9) ((AsyncResult)result).AsyncDelegate;
return d.EndInvoke(result);
}
}
#endregion ExecuteNonQuery
#region ExecuteScalar
/// <summary>
/// Execute a command (that returns a 1x1 resultset) using the provided parameters.
/// </summary>
/// <param name="connection">a valid IDbConnection</param>
/// <param name="transaction">a valid IDbTransaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of IDataParameters used to execute the command</param>
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
private static object ExecuteScalar(IDbConnection connection, IDbTransaction transaction, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
//create a command and prepare it for execution
SqlCommand cmd = new SqlCommand();
ConnectionState connState = connection.State;
PrepareCommand(cmd, connection, transaction, commandType, commandText, commandParameters);
//execute the command & return the results
object retval;
try
{
retval = cmd.ExecuteScalar();
}
catch (SqlException e)
{
//Handle data access exception condition
LogException(e);
//Wrap the current exception and re-throw the new exception
throw e;
}
finally
{
// detach the IDataParameters from the command object, so they can be used again.
cmd.Parameters.Clear();
if (connState == ConnectionState.Closed)
{
connection.Close();
}
}
return retval;
}
/// <summary>
/// Execute a command (that returns a 1x1 resultset) using the provided parameters.
/// </summary>
/// <param name="connectionString">a valid connection string for a IDbConnection yet to be opened</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText)
{
//pass through the call providing null for the set of IDataParameters
return ExecuteScalar(connectionString, commandType, commandText, (SqlParameter[])null);
}
/// <summary>
/// Execute a command (that returns a 1x1 resultset) using the provided parameters.
/// </summary>
/// <param name="connectionString">a valid connection string for a IDbConnection yet to be opened</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of IDataParameters used to execute the command</param>
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(string connectionString, CommandType commandType, string commandText, params IDataParameter[] commandParameters)
{
//create & open a IDbConnection, and dispose of it after we are done.
using (SqlConnection cn = new SqlConnection(connectionString))
{
//call the overload that takes a connection in place of the connection string
return ExecuteScalar(cn, commandType, commandText, commandParameters);
}
}
/// <summary>
/// Execute a stored procedure via a command (that returns a 1x1 resultset) against the database specified in
/// the connection string using the provided parameter 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 parameter order.
/// </summary>
/// <param name="connectionString">a valid connection string for a IDbConnection</param>
/// <param name="spName">the name of the stored procedure</param>
/// <param name="parameterValues">an array of objects to be assigned as the input values of the stored procedure</param>
/// <returns>an object containing the value in the 1x1 resultset generated by the command</returns>
public static object ExecuteScalar(string connectionString, string spName, params object[] parameterValues)
{
//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)
SqlParameter[] commandParameters = (SqlParameter[])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 IDataParameters
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName, commandParameters);
}
//otherwise we can just call the SP without params
else
{
return ExecuteScalar(connectionString, CommandType.StoredProcedure, spName);
}
}
/// <summary>
/// Execute a command (that returns a 1x1 resultset) using the provided parameters.
/// </summary>
/// <param name="connection">a valid IDbConnection</param>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -