Quickstart
Start by creating an ISqlRunner
for your chosen provider:
// MS SQL Server, requires CastIron.SqlServer package
var runner = CastIron.SqlServer.RunnerFactory.Create(connectionString);
// SQLite, requires CastIron.Sqlite package
var runner = CastIron.Sqlite.RunnerFactory.Create(connectionString);
// PostgreSQL, requires CastIron.Postgres package
var runner = CastIron.Postgres.RunnerFactory.Create(connectionString);
I want to…
Execute a query object and get a mapped result
var result = runner.Query(new MyQueryObject());
var result = runner.Query(new MyQueryObject(), new MyResultMaterializer());
var result = runner.Query(new MyQueryObject(), r => r.AsEnumerable<MyResultType>().ToList());
// All the above methods also have Async variants
var result = await runner.QueryAsync(new MyQueryObject());
Execute a command object
runner.Execute(new MyCommandObject());
await runner.ExecuteAsync(new MyCommandObject());
Execute a query and stream results
using (var stream = runner.QueryStream(new MyQueryObject())) {
foreach (var result in stream.AsEnumerable<MyResultType>()) {
...
}
}
using (var stream = runner.QueryStream("SELECT * FROM MyTable")) {
foreach (var result in stream.AsEnumerable<MyResultType>()) {
...
}
}
using (var stream = await runner.QueryStreamAsync(new MyQueryObject())) {
foreach (var result in stream.AsEnumerable<MyResultType>()) {
...
}
}
Execute a query and get a raw IDataReader
using (var stream = runner.QueryStream(new MyQueryObject())))
using (var reader = stream.AsRawReader())
{
...
}
Execute a string of SQL and get a mapped result
var result = runner.Query<MyResultType>("SELECT * FROM MyTable");
var result = runner.Query("SELECT * FROM MyTable", new MyResultMaterializer());
var result = runner.Query("SELECT * FROM MyTable", r => r.AsEnumerable<MyResultType>().FirstOrDefault());
Execute a string of SQL which does not return a result
runner.Execute("UPDATE MyTable SET ...");
Map an existing IDataReader
to an enumerable of objects
var result = runner.WrapAsResultStream(dataReader).AsEnumerable<MyType>();
Map an existing DataTable
to an enumerable of objects
var result = runner.WrapAsResultStream(dataTable).AsEnumerable<MyType>();
Batch multiple queries and commands onto a single connection and execute at once
var batch = runner.CreateBatch();
var promise1 = batch.Add(new MyQueryObject1());
var promise2 = batch.Add(new MyQueryObject2(), new MyResultMaterializer());
var promise3 = batch.Add<MyResultType>("SELECT * FROM MyTable");
batch.Add(new MyCommandObject());
runner.Execute(batch);
var result1 = promise1.GetValue();
var result2 = promise2.GetValue();
var result3 = promise3.GetValue();