Skip to content

Instantly share code, notes, and snippets.

@jamesmh
Last active August 15, 2018 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesmh/9e1382a567e0891670c2d55b47ec3ba7 to your computer and use it in GitHub Desktop.
Save jamesmh/9e1382a567e0891670c2d55b47ec3ba7 to your computer and use it in GitHub Desktop.
ADO Query Class
public class Query
{
private readonly string _connectionString;
public Query(string connectionString)
{
this._connectionString = connectionString;
}
public async Task<T> UsingConnectionAsync<T>(Func<SqlConnection, Task<T>> func)
{
using (SqlConnection con = new SqlConnection(this._connectionString))
{
await con.OpenAsync();
return await func(con);
}
}
public async Task<T> UsingTransactionAsync<T>(Func<SqlConnection, SqlTransaction, Task<T>> func)
{
return await UsingConnectionAsync(
async con =>
{
using(SqlTransaction trans = con.BeginTransaction())
{
var result = await func(con, trans);
trans.Commit();
return result;
}
}
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment