# The C# SqlAggregator Class

### For an overview see page C# Classes

## Class Definition

        public class SqlAggregator : ISqlConnection, IDisposable
        {
            // ===== CONSTRUCTORS =====
            public SqlAggregator(Database db, SqlLocalConnection[] engines);
            public SqlAggregator(Database db, SqlLocalConnection[] engines, int bufferSize);

            // ===== PROPERTIES =====
            public IDatabaseWrapper Wrapper { get; }

            // ===== QUERY EXECUTION METHODS =====
            public SqlResultSet ExecuteQuery(String query, params Object[] args);
            public int ExecuteStatement(String stmt, params Object[] args);

            // ===== LIFECYCLE METHODS =====
            public void Disconnect();
            public void Dispose();

            // ===== INTERNAL MANAGEMENT =====
            public void DetachResultSet(SqlResultSet result);
        }

## Member Descriptions

SqlAggregator(Database, SqlLocalConnection[])
Creates an SQL aggregator connection that distributes queries across multiple database engines. Uses default buffer size (64 KB) for internal communication.
SqlAggregator(Database, SqlLocalConnection[], int)
Creates an SQL aggregator connection with custom buffer size. The `bufferSize` parameter controls the internal transmit buffer used for serializing query data across engines.
Wrapper
Gets the internal `IDatabaseWrapper` instance for low-level database operations. Part of the `ISqlConnection` interface contract.
ExecuteQuery(String, params Object[])
Executes a parameterized SQL SELECT query across all aggregated engines and merges the results. Returns a `SqlResultSet` cursor for iterating through combined tuples. Throws `SqlException` if the aggregator is closed.
ExecuteStatement(String, params Object[])
Executes a parameterized SQL DML statement (INSERT, UPDATE, DELETE) across all aggregated engines. Returns the total number of affected records. Throws `SqlException` if the aggregator is closed.
Disconnect()
Closes the aggregator connection: disposes any active result set, releases the native aggregator handle, and sets internal state to closed. Safe to call multiple times.
Dispose()
Releases resources by calling `Disconnect()`. Implements the `IDisposable` pattern for use with `using` statements.
DetachResultSet(SqlResultSet)
Internal method called by `SqlResultSet` to detach itself from the aggregator. Sets the internal result reference to `null`. Not intended for direct application use.
Aggregation Model
The `SqlAggregator` executes queries in parallel across all provided `SqlLocalConnection` engines and merges results. Suitable for read-heavy workloads requiring horizontal scaling.
Thread Safety
Each `SqlAggregator` instance is not thread-safe. Use separate aggregator instances per thread. The underlying engines must remain valid for the aggregator's lifetime.
Parameter Binding
Both `ExecuteQuery` and `ExecuteStatement` support parameterized queries using '?' placeholders. Parameters are passed as `params Object[]` and bound by position.
Resource Management
Always call `Dispose()` or use a `using` block to ensure timely release of native aggregator resources. The aggregator does not own the underlying engine connections—they must be managed separately.
Error Handling
Methods throw `SqlException` if the aggregator is already closed. Query execution errors from individual engines are propagated to the caller.
String Encoding
Uses the database's `defaultStringEncoding` for converting .NET strings to the wire format expected by the eXtremeDB engine.