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

RunScalar() public method

Executes the IAdfQuery, and returns the first column of the first row in the resultset returned by the IAdfQuery. Extra columns or rows are ignored.
The current state of the connection is closed.
public RunScalar ( IAdfQuery query ) : object
query IAdfQuery The that defines the datasource name and query statement.
return object
        public object RunScalar(IAdfQuery query)
        {
            var result = new object();
            if (query == null) return result;

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

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

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

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

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

                result = da.SelectCommand.ExecuteScalar();
            }
            catch (Exception exception)
            {
                Provider.HandleException(exception, DataSource, query);
            }

            finally
            {
                if (da.SelectCommand.Transaction == null) connection.Close();
            }

            return result;
        }