protected int ExecuteNonQuery(DatabaseCommand databaseCommand, IDictionary <string, object> parameterValues, [CallerMemberName] string callingMethodName = "") { ThrowExceptionIfDatabaseCommandIsNull(databaseCommand, callingMethodName); using (var connection = DbContext.CreateDbConnection()) using (var command = CreateIDbCommand(connection, databaseCommand, parameterValues)) return(command.ExecuteNonQuery()); }
/// <summary> /// Inspects an instance of <see cref="DatabaseCommand"/> to see whether it is <c>null</c>, and /// if it is, throws a <see cref="NullReferenceException"/> with a descriptive error message. /// </summary> /// <param name="databaseCommand"> /// The instance of <see cref="DatabaseCommand"/> to check for a <c>null</c> value. /// </param> /// <param name="callingMethodName"> /// No need to specify a value for this parameter; it will automatically be set to the calling method's name. /// </param> static DatabaseCommand ThrowExceptionIfDatabaseCommandIsNull(DatabaseCommand databaseCommand, string callingMethodName) { if (databaseCommand == null) { var message = String.Format(CultureInfo.InvariantCulture, "Unable to execute {0} because a SQL command has not been specified.", callingMethodName); throw new ArgumentException(message); } return(databaseCommand); }
static IDbCommand CreateIDbCommand(IDbConnection connection, DatabaseCommand databaseCommand, IDictionary <string, object> parameterValues) { if (connection == null) { throw new ArgumentNullException(nameof(connection)); } if (databaseCommand == null) { throw new ArgumentNullException(nameof(databaseCommand)); } if (connection.State == ConnectionState.Closed) { connection.Open(); } var command = connection.CreateCommand(); // If we have an Oracle connection, make sure the "BindByName" property is set to "true". SetBindByNamePropertyToTrueIfPresent(command); command.CommandText = databaseCommand.CommandText; if (databaseCommand.CommandType.HasValue) { command.CommandType = databaseCommand.CommandType.Value; } if (databaseCommand.Parameters != null) { // Creating a new list here so we work on a copy of the parameters instead of the original values. var listOfParams = new List <IDataParameter>(databaseCommand.Parameters); foreach (var paramValue in parameterValues) { var currentParamName = paramValue.Key; var paramWithValue = listOfParams.FirstOrDefault(paramDef => paramDef.ParameterName == currentParamName); // Just ignore parameters that haven't been defined. if (paramWithValue != null) { paramWithValue.Value = paramValue.Value; } command.Parameters.Add(paramWithValue); } } return(command); }
protected DataTable ExecuteReader(DatabaseCommand databaseCommand, IDictionary <string, object> parameterValues, [CallerMemberName] string callingMethodName = "") { ThrowExceptionIfDatabaseCommandIsNull(databaseCommand, callingMethodName); var dataTable = new DataTable(); dataTable.Locale = CultureInfo.InvariantCulture; using (var connection = DbContext.CreateDbConnection()) using (var reader = CreateIDbCommand(connection, databaseCommand, parameterValues).ExecuteReader()) dataTable.Load(reader); return(dataTable); }
protected DataTable ExecuteReader(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "") { return(ExecuteReader(databaseCommand, null, callingMethodName)); }
protected int ExecuteNonQuery(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "") { return(ExecuteNonQuery(databaseCommand, null, callingMethodName)); }
protected T ExecuteScalar <T>(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "") { return(ExecuteScalar <T>(databaseCommand, null, callingMethodName)); }