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 { internal class SqlDataAccess : IDisposable { public string GetConnectionString(string name) { return ConfigurationManager.ConnectionStrings[name].ConnectionString; } public List LoadData(string storedProcedure, U parameters, string connectionStringName) { string connectionString = GetConnectionString(connectionStringName); using (IDbConnection connection = new SqlConnection(connectionString)) { List rows = connection.Query(storedProcedure, parameters, commandType: CommandType.StoredProcedure).ToList(); return rows; } } public void SaveData(string storedProcedure, T parameters, string connectionStringName) { string connectionString = GetConnectionString(connectionStringName); using (IDbConnection connection = new SqlConnection(connectionString)) { connection.Execute(storedProcedure, parameters, commandType: CommandType.StoredProcedure); } } private IDbConnection _connection; private IDbTransaction _transaction; public void StartTransaction(string connectionStringName) { string connectionString = GetConnectionString(connectionStringName); _connection = new SqlConnection(connectionString); _connection.Open(); _transaction = _connection.BeginTransaction(); } public void SaveDataInTransaction(string storedProcedure, T parameters) { _connection.Execute(storedProcedure, parameters, commandType: CommandType.StoredProcedure, transaction: _transaction); } public List LoadDataInTransaction(string storedProcedure, U parameters) { List rows = _connection.Query(storedProcedure, parameters, commandType: CommandType.StoredProcedure, transaction: _transaction).ToList(); return rows; } public void CommitTransaction() { _transaction?.Commit(); _connection?.Close(); } public void RollBackTransaction() { _transaction?.Rollback(); _connection?.Close(); } public void Dispose() { CommitTransaction(); } } }