Created
October 29, 2021 08:54
-
-
Save amarodeabreu/d2ce68dfb83267b1035fea1a37036774 to your computer and use it in GitHub Desktop.
Generic SQL with T
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
/// <summary> | |
/// Generic type SQL Query Fetch | |
/// </summary> | |
/// <typeparam name="T">Generic</typeparam> | |
/// <param name="connectionString">SQL Connection string</param> | |
/// <param name="sql">SQL select query</param> | |
/// <returns>List Object of type inferred</returns> | |
public async Task<IEnumerable<T>> QueryTask<T>(string connectionString, string sql) | |
{ | |
using IDbConnection db = new SqlConnection(connectionString); | |
var result = await db.QueryAsync<T>(sql); | |
return result; | |
} | |
//Usage: | |
var resultList = await QueryTask<string>(connString, queryString); | |
/// <summary> | |
/// Generic type SQL Query Fetch | |
/// </summary> | |
/// <typeparam name="T">Generic</typeparam> | |
/// <param name="connectionString">SQL Connection string</param> | |
/// <param name="sql">SQL select query</param> | |
/// <returns>Object of type inferred</returns> | |
public async Task<T> QueryTask<T>(string connectionString, string sql) | |
{ | |
using IDbConnection db = new SqlConnection(connectionString); | |
var result = await db.QueryFirstOrDefaultAsync<T>(sql); | |
return result; | |
} | |
//Usage: | |
var result = await QueryTask<string>(connString, queryString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment