Beispiel #1
0
        public async Task <int> ExecuteNonQueryAsync(DbCommand command)
        {
            int  result;
            bool connectionOpened = false;

            fixUpParameterizedStatementForProvider(command);
            // need to create connection
            if (command.Connection == null)
            {
                using (DbConnection connection = OpenConnection(false))
                {
                    command.Connection = connection;
                    try
                    {
                        result = await command.ExecuteNonQueryAsync();
                    }
                    catch (Exception ex)
                    {
                        DataAccessException dax = new DataAccessException("Could not execute non-query command.", ex);
                        dax.Data.Add("Provider", _connectionString.ProviderName);
                        dax.Data.Add("Connection String", _connectionString.ConnectionString);
                        dax.Data.Add("Command Text", command.CommandText);
                        throw dax;
                    }
                    finally
                    {
                        connection.Close();
                        command.Connection = null;
                    }
                }
                return(result);
            }
            // use existing connection, but make sure it is open
            if (command.Connection.State == ConnectionState.Closed)
            {
                command.Connection.Open();
                connectionOpened = true;
            }
            try
            {
                result = await command.ExecuteNonQueryAsync();
            }
            catch (Exception ex)
            {
                DataAccessException dax = new DataAccessException("Could not execute non-query command.", ex);
                dax.Data.Add("Provider", _connectionString.ProviderName);
                dax.Data.Add("Connection String", _connectionString.ConnectionString);
                dax.Data.Add("Command Text", command.CommandText);
                throw dax;
            }
            finally
            {
                if (connectionOpened)
                {
                    command.Connection.Close();
                }
            }

            return(result);
        }
Beispiel #2
0
        public DbDataReader ExecuteReader(DbCommand command, CommandBehavior behavior)
        {
            bool openedConnection = false;

            fixUpParameterizedStatementForProvider(command);
            if (command.Connection == null)
            {
                DbConnection connection = OpenConnection(false);
                command.Connection = connection;
                openedConnection   = true;
            }
            else if (command.Connection.State == ConnectionState.Closed)
            {
                command.Connection.Open();
                openedConnection = true;
            }
            try
            {
                return(openedConnection ? command.ExecuteReader(CommandBehavior.CloseConnection | behavior) : command.ExecuteReader(behavior));
            }
            catch (Exception ex)
            {
                DataAccessException dax = new DataAccessException("Could not execute the ExecuteReader command.", ex);
                dax.Data.Add("Provider", _connectionString.ProviderName);
                dax.Data.Add("Connection String", _connectionString.ConnectionString);
                dax.Data.Add("Command Text", command.CommandText);
                throw dax;
            }
        }
Beispiel #3
0
        /// <summary>
        /// Opens a connection to the database. If using the default connection on the class instance
        /// and the connection has not yet been created, the default connection will be created before
        /// attempting to open the connection. If not using the default connection, a new connection
        /// object will be created and opened.
        /// </summary>
        /// <exception cref="OgreDA.DataAccess.DataAccessException">
        /// Thrown if the database connection cannot be opened.
        /// </exception>
        /// <param name="useDefaultConnection">Indicates whether the default connection object associated with the
        /// class instance should be used or if a new connection should be created.</param>
        /// <returns>A reference to the connection that was opened.</returns>
        public DbConnection OpenConnection(bool useDefaultConnection = false)
        {
            DbConnection connection = CreateConnection(useDefaultConnection);

            try
            {
                connection.Open();
            }
            catch (Exception ex)
            {
                DataAccessException dax = new DataAccessException("The database connection could not be opened.", ex);
                dax.Data.Add("Provider", _connectionString.ProviderName);
                dax.Data.Add("Connection String", _connectionString.ConnectionString);
                throw dax;
            }
            return(connection);
        }
Beispiel #4
0
 /// <summary>
 /// Executes an ExecuteScalar command using the passed DbCommand object on the database.
 ///
 /// An attempt will be made to adjust an parameterized commands to match the syntax required
 /// by the provider. Parameterized commands should be defined using named parameters as supported
 /// by SQL server.
 ///
 /// A connection must already be associated with the command (either manually or by using the default
 /// connection when the command was created) and the connection must already be open.
 /// </summary>
 /// <exception cref="OgreDA.DataAccess.DataAccessException">
 /// Thrown if the query fails.
 /// </exception>
 /// <param name="command">The DbCommand object with the command to execute.</param>
 /// <param name="transaction">The DbTransaction object for the transaction.</param>
 /// <returns>The value of the first column of the first result of the command.</returns>
 public object?ExecuteScalar(DbCommand command, DbTransaction?transaction)
 {
     if (transaction != null)
     {
         command.Transaction = transaction;
     }
     fixUpParameterizedStatementForProvider(command);
     try
     {
         return(command.ExecuteScalar());
     }
     catch (Exception ex)
     {
         DataAccessException dax = new DataAccessException("Could not execute the ExecuteReader command.", ex);
         dax.Data.Add("Provider", _connectionString.ProviderName);
         dax.Data.Add("Connection String", _connectionString.ConnectionString);
         dax.Data.Add("Command Text", command.CommandText);
         throw dax;
     }
 }
Beispiel #5
0
        public async Task <DbDataReader> ExecuteReaderAsync(DbCommand command, CommandBehavior behavior = CommandBehavior.Default, DbTransaction?transaction = null)
        {
            bool openedConnection = false;

            fixUpParameterizedStatementForProvider(command);
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
            else
            {
                if (command.Connection == null)
                {
                    DbConnection connection = OpenConnection(false);
                    command.Connection = connection;
                    openedConnection   = true;
                }
                else if (command.Connection.State == ConnectionState.Closed)
                {
                    command.Connection.Open();
                    openedConnection = true;
                }
            }
            try
            {
                CommandBehavior b = openedConnection ? CommandBehavior.CloseConnection | behavior : behavior;
                return(await command.ExecuteReaderAsync(b));
            }
            catch (Exception ex)
            {
                DataAccessException dax = new DataAccessException("Could not execute the ExecuteReader command.", ex);
                dax.Data.Add("Provider", _connectionString.ProviderName);
                dax.Data.Add("Connection String", _connectionString.ConnectionString);
                dax.Data.Add("Command Text", command.CommandText);
                throw dax;
            }
        }