Last active
October 21, 2021 14:50
-
-
Save JustinMorgan/be54e6f89c58f22a9fe8694becb8c9a1 to your computer and use it in GitHub Desktop.
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
//used as an example DTO type. | |
class Crosslink | |
{ | |
public string Code { get; set; } | |
public string FullDescription { get; set; } | |
} | |
public async Task<Crosslink[]> QueryAsync(CommandType commandType, string query, params DbParameter[] parameters) | |
{ | |
var list = new List<Crosslink>(); | |
var dbFactory = DbProviderFactories.GetFactory("System.Data.SqlClient"); | |
using (var conn = dbFactory.CreateConnection()) | |
using (var cmd = conn.CreateCommand()) | |
{ | |
conn.ConnectionString = db.ConnectionString; | |
if (parameters != null) { | |
cmd.Parameters.AddRange(parameters); | |
} | |
cmd.CommandType = commandType; | |
cmd.CommandText = query; | |
await conn.OpenAsync(); | |
using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection | CommandBehavior.SingleResult)) | |
{ | |
int ciCode = reader.GetOrdinal("Code"); | |
int ciDesc = reader.GetOrdinal("FullDescription"); | |
while (await reader.ReadAsync()) | |
{ | |
list.Add(new Crosslink(reader.GetString(ciCode), reader.GetString(ciDesc))); | |
} | |
} | |
} | |
return list.ToArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment