Adf.Data.DatabaseQueryHandler.RunQuery C# (CSharp) Method

RunQuery() private method

Runs the specified IAdfQuery and get the executed result set into a System.Data.DataSet.
The current state of the connection is closed.
private RunQuery ( IAdfQuery query ) : DataSet
query IAdfQuery The that defines the datasource name and query statement.
return System.Data.DataSet
        private DataSet RunQuery(IAdfQuery query)
        {
            var result = new DataSet { EnforceConstraints = false };
            if (query == null) return result;

            IDbConnection connection = Provider.GetConnection(DataSource);
            IDbDataAdapter da = Provider.GetAdapter();

            try
            {
                if (connection.State == ConnectionState.Closed) connection.Open();

                da.TableMappings.Add("Table", query.LeadTable());

                IDbCommand command = Provider.GetCommand(DataSource, connection, query);
                da.SelectCommand = command;

                IDbTransaction transaction = Provider.GetTransaction(DataSource);
                if (transaction != null) da.SelectCommand.Transaction = transaction;

                result.Load(command.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly), LoadOption.OverwriteChanges, query.LeadTable());

                da.Fill(result);
            }
            catch(Exception exception)
            {
                Provider.HandleException(exception, DataSource, query);
            }
            finally
            {
                if (da.SelectCommand == null || da.SelectCommand.Transaction == null)
                    if (connection.State == ConnectionState.Open) connection.Close();
            }

            return result;
        }