common/Logic/Common.Logic.Extensions.SQL/SQLExtensions.cs
2026-04-23 17:50:07 +02:00

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();
}
}
}