public static int Execute(RDBMSProvider rdbmsProvider, string connectionString, string sqlQuery, object paramsObj = null, bool useTransaction = false, int?commandTimeOut = null, CommandType?commandType = null) { IDbConnection connection = null; IDbTransaction transaction = null; try { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException("Connection string cannot be null, empty or white-space.", nameof(connectionString)); } connection = CreateConnection(rdbmsProvider, connectionString); connection.Open(); if (useTransaction) { transaction = connection.BeginTransaction(); } var ret = connection.Execute(sqlQuery, paramsObj, transaction, commandTimeOut, commandType); transaction?.Commit(); return(ret); } catch { throw; } finally { connection.Dispose(); transaction?.Dispose(); } }
public static IEnumerable <TReturn> Query <TFirst, TSecond, TThird, TReturn>(RDBMSProvider rdbmsProvider, string connectionString, string sqlQuery, Func <TFirst, TSecond, TThird, TReturn> map, object paramsObj = null, bool useTransaction = false, bool buffered = true, string splitOn = "Id", int?commandTimeOut = null, CommandType?commandType = null) { IDbConnection connection = null; IDbTransaction transaction = null; try { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException("Connection string cannot be null, empty or white-space.", nameof(connectionString)); } connection = CreateConnection(rdbmsProvider, connectionString); connection.Open(); if (useTransaction) { transaction = connection.BeginTransaction(); } var ret = connection.Query <TFirst, TSecond, TThird, TReturn>(sqlQuery, map, paramsObj, transaction, buffered, splitOn, commandTimeOut, commandType); transaction?.Commit(); return(ret); } catch { throw; } finally { connection.Dispose(); transaction?.Dispose(); } }
public static object ExecuteScalar(RDBMSProvider rdbmsProvider, string connectionString, CommandDefinition command) { IDbConnection connection = null; try { if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException("Connection string cannot be null, empty or white-space.", nameof(connectionString)); } connection = CreateConnection(rdbmsProvider, connectionString); connection.Open(); return(connection.ExecuteScalar(command)); } catch { throw; } finally { connection.Dispose(); } }
private static IDbConnection CreateConnection(RDBMSProvider rdbmsProvider, string connectionString) { IDbConnection connection = null; switch (rdbmsProvider) { case RDBMSProvider.MSSQLServer: connection = new SqlConnection(connectionString); break; case RDBMSProvider.MySQL: connection = new MySqlConnection(connectionString); break; case RDBMSProvider.PostgreSQL: connection = new NpgsqlConnection(connectionString); break; case RDBMSProvider.Oracle: connection = new OracleConnection(connectionString); break; case RDBMSProvider.Firebird: connection = new FbConnection(connectionString); break; case RDBMSProvider.SQLite: connection = new SQLiteConnection(connectionString); break; default: break; } return(connection); }
public UserRepository(IConfiguration configuration) { _configuration = configuration; _rdbmsProvider = _configuration.GetValue <RDBMSProvider>("AppSettings:MainRDBMSType"); _connectionString = _configuration.GetValue <string>("AppSettings:MainRDBMSConnectionString"); }
public DbSession(RDBMSProvider rdbmsProvider, string connectionString) { Connection = CreateConnection(rdbmsProvider, connectionString); Connection.Open(); }