NDB.DataAccess.SqlServer
parent
f2791e0631
commit
7b3776f27f
|
@ -0,0 +1,167 @@
|
||||||
|
using System;
|
||||||
|
using System.Data;
|
||||||
|
using System.Data.SqlClient;
|
||||||
|
|
||||||
|
namespace NDB.DataAccess.SqlServer
|
||||||
|
{
|
||||||
|
public abstract class BaseDataAccess
|
||||||
|
{
|
||||||
|
#region Fields
|
||||||
|
private string ConnectionString { get; set; }
|
||||||
|
#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<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
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
8
NDB.sln
8
NDB.sln
|
@ -17,7 +17,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Infrastructure.PublicIP
|
||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Testing.App", "NDB.Testing.App\NDB.Testing.App.csproj", "{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Testing.App", "NDB.Testing.App\NDB.Testing.App.csproj", "{656CCE38-90C3-4D19-9D07-EF1F0410CFDC}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.Logging.Api", "NDB.Logging.Api\NDB.Logging.Api.csproj", "{74E221BE-C097-468B-93E4-AAE1B2173C49}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NDB.Logging.Api", "NDB.Logging.Api\NDB.Logging.Api.csproj", "{74E221BE-C097-468B-93E4-AAE1B2173C49}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NDB.DataAccess.SqlServer", "NDB.DataAccess.SqlServer\NDB.DataAccess.SqlServer.csproj", "{95AF4B47-2C84-44C1-BD25-297F09A48BED}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
@ -41,6 +43,10 @@ Global
|
||||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.Build.0 = Release|Any CPU
|
{74E221BE-C097-468B-93E4-AAE1B2173C49}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{95AF4B47-2C84-44C1-BD25-297F09A48BED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{95AF4B47-2C84-44C1-BD25-297F09A48BED}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{95AF4B47-2C84-44C1-BD25-297F09A48BED}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{95AF4B47-2C84-44C1-BD25-297F09A48BED}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|
Loading…
Reference in New Issue