40 lines
1.1 KiB
C#
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
|
|
}
|
|
}
|
|
}
|