netmash/NDB.DataAccess.SqlServer/BaseDataAccess.cs

168 lines
4.4 KiB
C#
Raw Normal View History

2020-04-12 05:53:03 +03:00
using System;
using System.Data;
using System.Data.SqlClient;
namespace NDB.DataAccess.SqlServer
{
public abstract class BaseDataAccess
{
#region Fields
private readonly string _connectionString;
2020-04-12 05:53:03 +03:00
#endregion
protected BaseDataAccess(string connectionString)
{
_connectionString = connectionString;
2020-04-12 05:53:03 +03:00
}
#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);
2020-04-12 05:53:03 +03:00
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<T>(object obj)
{
if (obj != DBNull.Value && obj != null)
return (T)obj;
return default(T);
}
protected T GetDbValue<T>(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<T>(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
}
}