Skip to content

Instantly share code, notes, and snippets.

@MoimHossain

MoimHossain/SQLHelper.cs Secret

Created Jul 15, 2018
Embed
What would you like to do?
SQLHelper.cs
public static class TokenHelper
{
public static Task<String> GetTokenAsync()
{
var provider = new AzureServiceTokenProvider();
return provider.GetAccessTokenAsync("https://database.windows.net/");
}
}
public static class SqlConnectionExtensions
{
public async static Task<TPayload> ExecuteScalar<TPayload>(string commandText)
where TPayload: class
{
var connectionString = "connection string without credentails";
var token = await ADAuthentication.GetSqlTokenAsync();
using (var conn = new SqlConnection(connectionString))
{
conn.AccessToken = token;
await conn.OpenAsync();
using (var cmd = new SqlCommand(commandText, conn))
{
var result = await cmd.ExecuteScalarAsync();
return result as TPayload;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.