Last active
August 15, 2018 16:44
-
-
Save jamesmh/9e1382a567e0891670c2d55b47ec3ba7 to your computer and use it in GitHub Desktop.
ADO Query Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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