laatzen/LaaProductionWeb/LaaProductionWeb.Data/SqlClientExtensions.cs
Stoyan Zlatev cf9dfe1915 Production Approvals,
sql client refactoring
2023-04-14 16:45:41 +02:00

40 lines
1.1 KiB
C#

namespace LaaProductionWeb.Data
{
using LaaProductionWeb.Data.Interfaces;
using Microsoft.Extensions.DependencyInjection;
using System.Data.SqlClient;
using System.Diagnostics;
public static class SqlClientExtensions
{
public static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString)
{
services.AddTransient<ISqlClient>(_ => new SqlClient(connectionString));
return services;
}
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection, SqlInfoMessageEventHandler messageHandler = null)
{
sqlConnection.FireInfoMessageEventOnUserErrors = true;
sqlConnection.InfoMessage += SqlConnectionInfoMessage;
if (messageHandler != null)
{
sqlConnection.InfoMessage += messageHandler;
}
sqlConnection.Open();
}
internal static void SqlConnectionInfoMessage(object sender, SqlInfoMessageEventArgs e)
{
Debug.WriteLine(e);
// TODO: Handle errors
}
}
}