53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
namespace Common.Logic.Extensions.SQL
|
|
{
|
|
using System;
|
|
using System.Data.SqlClient;
|
|
|
|
public static class SQLExtensions
|
|
{
|
|
internal static T EnsureNotNull<T>(this T value, String member)
|
|
{
|
|
value.ThrowIfNull(member);
|
|
|
|
return value;
|
|
}
|
|
|
|
internal static String EnsureNotNullOrWhiteSpace(this String value, String member)
|
|
{
|
|
value.ThrowIfNullOrWhiteSpace(member);
|
|
|
|
return value;
|
|
}
|
|
|
|
internal static void ThrowIfNull<T>(this T value, String member)
|
|
{
|
|
if (value == null)
|
|
{
|
|
throw new ArgumentException($"{member} should not be null.", member);
|
|
}
|
|
}
|
|
|
|
internal static void ThrowIfNullOrWhiteSpace(this String value, String member)
|
|
{
|
|
if (String.IsNullOrWhiteSpace(value))
|
|
{
|
|
throw new ArgumentException($"{member} should not be null or white space.", member);
|
|
}
|
|
}
|
|
|
|
internal static void OpenWithErrorHandling(this SqlConnection sqlConnection, Action<String> errorCallback)
|
|
{
|
|
sqlConnection.ThrowIfNull(nameof(sqlConnection));
|
|
|
|
sqlConnection.FireInfoMessageEventOnUserErrors = true;
|
|
|
|
if (errorCallback != null)
|
|
{
|
|
sqlConnection.InfoMessage += (_, args) => errorCallback(args.Message);
|
|
}
|
|
|
|
sqlConnection.Open();
|
|
}
|
|
}
|
|
}
|