private static void MapParameters(SqlCommand command, StoredProcedureParameterList parameters) { int returnValueOffset = 1; AssertParameterCount(command.Parameters.Count, parameters.Count, returnValueOffset, command.CommandText); for (int i = 0 + returnValueOffset, j = parameters.Count; i <= j; i++) { StoredProcedureParameter spp = parameters[i - 1]; SqlParameter sqlParameter; if (spp.Key != null) { sqlParameter = command.Parameters[spp.Key]; } else { sqlParameter = command.Parameters[i]; } sqlParameter.Value = spp.Value; if (sqlParameter.Value == null) { sqlParameter.Value = DBNull.Value; } switch (spp.ParameterDirection) { case ParameterDirectionWrap.Input: sqlParameter.Direction = ParameterDirection.Input; break; case ParameterDirectionWrap.Output: sqlParameter.Direction = ParameterDirection.Output; break; case ParameterDirectionWrap.InputOutput: sqlParameter.Direction = ParameterDirection.InputOutput; break; case ParameterDirectionWrap.ReturnValue: sqlParameter.Direction = ParameterDirection.ReturnValue; break; default: throw new ArgumentException("Unknow parameter direction specified: " + spp.ParameterDirection.ToString()); } if (spp.Size.HasValue) { sqlParameter.Size = spp.Size.Value; } } }
/// <summary> /// Executes a procedure and allows the caller to inject a resultset mapper and an output parameter mapper. /// </summary> /// <param name="database"></param> /// <param name="procedureName"></param> /// <param name="parameterMapper"></param> /// <param name="result"></param> public static void ExecuteAndMapResults(Database database, string procedureName, StoredProcedureParameterList parameterList, ResultMapper result, OutputParameterMapper outputMapper) { try { using (SqlConnection connection = database.GetConnection()) { SqlCommand command = CommandFactory.CreateCommand(connection, database.InstanceName, procedureName, parameterList); connection.Open(); IRecordSet reader = new DataRecord(command.ExecuteReader(CommandBehavior.CloseConnection)); result(reader); connection.Close(); if (outputMapper != null) { outputMapper(new ParameterSet(command.Parameters)); } } } catch (Exception e) { throw new SafeProcedureException(database, procedureName, e); } }
/// <summary> /// Creates and prepares an SqlCommand object and sets parameters from the parameter list either by their index value or name. /// </summary> /// <returns></returns> internal static SqlCommand CreateCommand(SqlConnection connection, string databaseInstanceName, string commandName, StoredProcedureParameterList parameterList) { SqlCommand command = CreateParameterizedCommand(connection, databaseInstanceName, commandName); MapParameters(command, parameterList); return(command); }
/// <summary> /// Creates and prepares an MySqlCommand object and sets parameters from the parameter list either by their index value or name. /// </summary> /// <returns></returns> internal static MySqlCommand CreateCommand(MySqlConnection connection, string databaseInstanceName, string commandName, StoredProcedureParameterList parameterList) { MySqlCommand command = CreateParameterizedCommand(connection, databaseInstanceName, commandName); MapParameters(command, parameterList); ApplySecurity(command, commandCache.GetCommandCopy(connection, databaseInstanceName, commandName).Parameters); if (log.IsMoreDebugEnabled) { log.MoreDebug(new StackFrame(1, true), command); } return(command); }