2022-07-14 19:41:50 +02:00
|
|
|
|
using Dapper;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Configuration;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.SqlClient;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RMDataManagerLibrary.Internal.DataAccess
|
|
|
|
|
{
|
2022-07-30 15:26:54 +02:00
|
|
|
|
internal class SqlDataAccess : IDisposable
|
2022-07-14 19:41:50 +02:00
|
|
|
|
{
|
|
|
|
|
public string GetConnectionString(string name)
|
|
|
|
|
{
|
|
|
|
|
return ConfigurationManager.ConnectionStrings[name].ConnectionString;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<T> LoadData<T, U>(string storedProcedure, U parameters, string connectionStringName)
|
|
|
|
|
{
|
|
|
|
|
string connectionString = GetConnectionString(connectionStringName);
|
|
|
|
|
|
|
|
|
|
using (IDbConnection connection = new SqlConnection(connectionString))
|
|
|
|
|
{
|
|
|
|
|
List<T> rows = connection.Query<T>(storedProcedure, parameters,
|
|
|
|
|
commandType: CommandType.StoredProcedure).ToList();
|
|
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveData<T>(string storedProcedure, T parameters, string connectionStringName)
|
|
|
|
|
{
|
|
|
|
|
string connectionString = GetConnectionString(connectionStringName);
|
|
|
|
|
|
|
|
|
|
using (IDbConnection connection = new SqlConnection(connectionString))
|
|
|
|
|
{
|
|
|
|
|
connection.Execute(storedProcedure, parameters,
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-30 15:26:54 +02:00
|
|
|
|
|
|
|
|
|
private IDbConnection _connection;
|
|
|
|
|
private IDbTransaction _transaction;
|
2022-08-02 17:50:09 +02:00
|
|
|
|
private bool isClosed = false;
|
2022-07-30 15:26:54 +02:00
|
|
|
|
public void StartTransaction(string connectionStringName)
|
|
|
|
|
{
|
2022-08-02 17:50:09 +02:00
|
|
|
|
isClosed = false;
|
2022-07-30 15:26:54 +02:00
|
|
|
|
string connectionString = GetConnectionString(connectionStringName);
|
|
|
|
|
|
|
|
|
|
_connection = new SqlConnection(connectionString);
|
|
|
|
|
_connection.Open();
|
|
|
|
|
|
|
|
|
|
_transaction = _connection.BeginTransaction();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SaveDataInTransaction<T>(string storedProcedure, T parameters)
|
|
|
|
|
{
|
|
|
|
|
_connection.Execute(storedProcedure, parameters,
|
|
|
|
|
commandType: CommandType.StoredProcedure, transaction: _transaction);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public List<T> LoadDataInTransaction<T, U>(string storedProcedure, U parameters)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
List<T> rows = _connection.Query<T>(storedProcedure, parameters,
|
|
|
|
|
commandType: CommandType.StoredProcedure, transaction: _transaction).ToList();
|
|
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void CommitTransaction()
|
|
|
|
|
{
|
|
|
|
|
_transaction?.Commit();
|
|
|
|
|
_connection?.Close();
|
2022-08-02 17:50:09 +02:00
|
|
|
|
isClosed = true;
|
2022-07-30 15:26:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RollBackTransaction()
|
|
|
|
|
{
|
|
|
|
|
_transaction?.Rollback();
|
|
|
|
|
_connection?.Close();
|
2022-08-02 17:50:09 +02:00
|
|
|
|
isClosed = true;
|
2022-07-30 15:26:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-08-02 17:50:09 +02:00
|
|
|
|
if (!isClosed)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
CommitTransaction();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
// TODO - log this issue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_transaction = null;
|
|
|
|
|
_connection = null;
|
2022-07-30 15:26:54 +02:00
|
|
|
|
}
|
2022-07-14 19:41:50 +02:00
|
|
|
|
}
|
|
|
|
|
}
|