135 lines
3.9 KiB
C#
135 lines
3.9 KiB
C#
// Copyright (c) 2020-present schick Informatik
|
|
// Description: The new and mighty DB manager that uses async methods and
|
|
// connection pooling.. will try it out in Maersk/PO numbers and expand over the whole
|
|
// app later
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data.SqlClient;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
using log4net;
|
|
|
|
namespace bsmd.database
|
|
{
|
|
public static class DBManagerAsync
|
|
{
|
|
#region Fields
|
|
|
|
private static ILog _log = LogManager.GetLogger(typeof(DBManagerAsync));
|
|
private static readonly SemaphoreSlim _asyncSemaphore = new SemaphoreSlim(1);
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public static string ConnectionString { get; set; }
|
|
|
|
#endregion
|
|
|
|
#region public methods
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
#region async DB access methods
|
|
|
|
|
|
|
|
internal static async Task<SqlDataReader> PerformCommandAsync(SqlCommand cmd)
|
|
{
|
|
SqlDataReader reader = null;
|
|
|
|
// await _asyncSemaphore.WaitAsync();
|
|
try
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(ConnectionString))
|
|
{
|
|
await connection.OpenAsync();
|
|
reader = await cmd.ExecuteReaderAsync();
|
|
}
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
Trace.WriteLine("SQL Exception:" + ex.Message);
|
|
_log.Error("Error performing command", ex);
|
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
|
_log.Debug("Parameters:");
|
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
|
{
|
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
// _asyncSemaphore.Release();
|
|
}
|
|
|
|
return reader;
|
|
}
|
|
|
|
internal static async Task<int> PerformNonQuery(SqlCommand cmd)
|
|
{
|
|
int result = -1;
|
|
// await _asyncSemaphore.WaitAsync();
|
|
try
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(ConnectionString))
|
|
{
|
|
await connection.OpenAsync();
|
|
result = await cmd.ExecuteNonQueryAsync();
|
|
}
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
Trace.WriteLine("SQL Exception:" + ex.Message);
|
|
_log.Error("Error performing command", ex);
|
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
|
_log.Debug("Parameters:");
|
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
|
{
|
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
internal static async Task<int?> PerformReadIntQuery(SqlCommand cmd)
|
|
{
|
|
int? result = null;
|
|
|
|
try
|
|
{
|
|
using (SqlConnection connection = new SqlConnection(ConnectionString))
|
|
{
|
|
object r = await cmd.ExecuteScalarAsync();
|
|
if (r == null) { result = null; }
|
|
else if (r == DBNull.Value) { result = null; }
|
|
else { result = (int)r; }
|
|
}
|
|
}
|
|
catch (SqlException ex)
|
|
{
|
|
_log.Error("Error performing command", ex);
|
|
_log.DebugFormat("Query: {0}", cmd.CommandText);
|
|
_log.Debug("Parameters:");
|
|
for (int i = 0; i < cmd.Parameters.Count; i++)
|
|
{
|
|
_log.DebugFormat("{0}:{1}", cmd.Parameters[i].ParameterName, cmd.Parameters[i].Value);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|