Database connections can be injected into a QueryBuilder in two ways:

Inject an IDbConnection object
Just grab your connection from whatever connection manager you're using and give it to your QueryBuilder.
public static QueryBuilder CreateQueryBuilder() {
  IDbConnection c = MyConnectionManager.GetConnection();
  return new QueryBuilder(c);
}

Inject query execution functions
The alternate QueryBuilder constructor takes four proxy functions as parameters.
  • createCommand : Returns an IDbCommand.
  • executeReader(IDbCommand) : Executes an IDbCommand and returns an IDataReader.
  • executeNonQuery(IDbCommand) : Executes an IDbCommand and returns an Int32 (usually rows changed)
  • executeScalar(IDbCommand) : Executes an IDbComments and returns an Object (usually the first column of the first row in the result set)

For example, if you're stuck using something like the Enterprise library, you could create a working QueryBuilder like so:
public static QueryBuilder CreateQueryBuilder() {
  Database db = DatabaseFactory.CreateDatabase("main");

  Func<IDbCommand> createCommand = () =>
    { return db.GetSqlStringCommand("select 1"); };

  Func<IDbCommand, IDataReader> executeReader = (q) =>
    { return db.ExecuteReader((DbCommand)q); };

  Func<IDbCommand, Int32> executeNonQuery = (q) =>
    { return db.ExecuteNonQuery((DbCommand)q); };

  Func<IDbCommand, Object> executeScalar = (q) =>
    { return db.ExecuteScalar((DbCommand)q); };

  return new QueryBuilder(
    createCommand,
    executeReader,
    executeNonQuery,
    executeScalar
  );
}

Last edited Aug 8, 2014 at 11:00 PM by svidgen, version 4