using System; using System.Data; using System.Data.SqlClient; namespace Netmash.DataAccess.SqlServer { public abstract class BaseDataAccess { #region Fields private readonly string _connectionString; #endregion protected BaseDataAccess(string connectionString) { _connectionString = connectionString; } #region Properties public SqlConnection Connection { get; private set; } = null; public SqlTransaction Transaction { get; private set; } = null; public SqlCommand Command { get; private set; } = null; #endregion #region Methods #region OpenConnection public void OpenConnection(string connStr = null) { Connection = new SqlConnection(connStr ?? _connectionString); Connection.Open(); } #endregion #region OpenConnection public void CloseConnection() { Connection.Close(); } #endregion #region OpenTransaction public void OpenTransaction() { Transaction = Connection.BeginTransaction(); } #endregion #region OpenConnectionWithTransaction public void OpenConnectionWithTransaction() { OpenConnection(); OpenTransaction(); } #endregion #region Rollback public void Rollback() { CheckTransaction(); if (Transaction.Connection == null) return; Transaction.Rollback(); } private void CheckTransaction() { if (Transaction == null) throw new Exception("No open transaction found!"); } #endregion #region Commit public void Commit() { CheckTransaction(); if (Transaction.Connection == null) return; Transaction.Commit(); } #endregion #region CreateCommand public void CreateCommand() { Command = new SqlCommand { Connection = Connection }; if (Transaction != null) Command.Transaction = Transaction; } public void CreateCommand(CommandType commandType) { CreateCommand(); Command.CommandType = commandType; } public void CreateCommand(CommandType commandType, string commandText) { CreateCommand(commandType); Command.CommandText = commandText; } #endregion #region GetDbValue protected T GetDbValue(object obj) { if (obj != DBNull.Value && obj != null) return (T)obj; return default(T); } protected T GetDbValue(IDataReader dr, string columnName) { if (dr[columnName] != DBNull.Value && dr[columnName] != null) return (T)dr[columnName]; return default(T); } #endregion #region GetParamValue protected T GetParamValue(string parameterName) { object obj = (Command.Parameters[parameterName]).Value; if (obj != DBNull.Value && obj != null) return (T)obj; return default(T); } #endregion #region AddParameterToCommand protected void AddParameterToCommand(string parameterName, ParameterDirection direction, DbType dbType, object value, int size = int.MinValue) { var param = Command.CreateParameter(); param.ParameterName = parameterName; param.Direction = direction; param.DbType = dbType; if (value == null || value == DBNull.Value) param.Value = DBNull.Value; else param.Value = value; if (size != int.MinValue) param.Size = size; Command.Parameters.Add(param); } protected void AddParameterToCommand(string parameterName, ParameterDirection direction, DbType dbType) { var param = Command.CreateParameter(); param.ParameterName = parameterName; param.Direction = direction; param.DbType = dbType; Command.Parameters.Add(param); } #endregion #endregion } }