Exemplo n.º 1
0
        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());
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
 protected DataTable ExecuteReader(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "")
 {
     return(ExecuteReader(databaseCommand, null, callingMethodName));
 }
Exemplo n.º 6
0
 protected int ExecuteNonQuery(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "")
 {
     return(ExecuteNonQuery(databaseCommand, null, callingMethodName));
 }
Exemplo n.º 7
0
 protected T ExecuteScalar <T>(DatabaseCommand databaseCommand, [CallerMemberName] string callingMethodName = "")
 {
     return(ExecuteScalar <T>(databaseCommand, null, callingMethodName));
 }