//********************************************************************* // // 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 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. // // This method provides no access to output parameters or the stored procedure's return value parameter. // // e.g.: // int orderCount = (int)ExecuteScalar(connString, "GetOrderCount", 24, 36); // // param name="connectionString" a valid connection string for a SqlConnection // param name="spName" the name of the stored procedure // param name="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 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 = 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)); } }
public static SqlDataReader ExecuteSqlDataReaderTransaccion(SqlTransaction transaccion, string spName, params object[] parameterValues) { SqlCommand cmd = new SqlCommand(); 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 = SqlHelperParameterCache.GetSpParameterSetTransaction(transaccion, spName, false); //assign the provided values to these parameters based on parameter order AssignParameterValues(commandParameters, parameterValues); //create a command and prepare it for execution PrepareCommand(cmd, transaccion.Connection, transaccion, CommandType.StoredProcedure, spName, commandParameters); } //finally, execute the command. SqlDataReader dr = cmd.ExecuteReader(); return(dr); }