SQL DB Helper Class
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.IO;
namespace SampleProject.Infrastructure.DataLayer
{
public class SQLDBGateway
{
public static string ConnectionString()
{
return "Data Source=localhost;DataBase=SampleDB;user=admin;password=admin;enlist=false;";
}
public static SqlConnection CreateConnection()
{
try
{
return new SqlConnection(ConnectionString());
}
catch (SqlException ex)
{
throw;
}
}
public static SqlCommand CreateCommand()
{
try
{
SqlConnection currentConnection = CreateConnection();
if (currentConnection.State == ConnectionState.Closed)
currentConnection.Open();
return currentConnection.CreateCommand();
}
catch (SqlException ex)
{
throw;
}
}
public static SqlCommand CreateCommand(SqlConnection con)
{
try
{
if (con.State == ConnectionState.Closed)
con.Open();
return con.CreateCommand();
}
catch (SqlException ex)
{
throw;
}
}
public static SqlTransaction Transaction()
{
try
{
SqlConnection connection = CreateCommand().Connection;
return connection.State != ConnectionState.Open ? (SqlTransaction)null : connection.BeginTransaction();
}
catch (SqlException ex)
{
throw;
}
}
public static void Rollback(SqlCommand? sqlCommand)
{
if (sqlCommand?.Connection.State == ConnectionState.Open)
{
sqlCommand.Transaction?.Rollback();
}
}
public static void CloseConnection(SqlCommand cmd)
{
try
{
if (cmd == null || cmd.Connection == null || cmd.Connection.State != ConnectionState.Open)
return;
cmd.Connection.Close();
}
catch (SqlException ex)
{
throw;
}
}
private static SqlConnection GetConnection(string connectionString)
{
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
return connection;
}
catch
{
throw;
}
}
public static SqlConnection GetConnection()
{
try
{
return GetConnection(ConnectionString());
}
catch (SqlException ex)
{
throw;
}
}
public static int ExecuteNonQuery(string query, SqlParameter[] paramArr, CommandType cmdType)
{
SqlCommand sqlCommand = (SqlCommand)null;
try
{
sqlCommand = CreateCommand();
sqlCommand.CommandText = query;
sqlCommand.CommandType = cmdType;
sqlCommand.CommandTimeout = 120;
if (paramArr != null)
{
for (int index = 0; index < paramArr.Length; ++index)
{
if (paramArr[index] != null)
sqlCommand.Parameters.Add(paramArr[index]);
}
}
return sqlCommand.ExecuteNonQuery();
}
catch (Exception ex)
{
Rollback(sqlCommand);
sqlCommand?.Connection.Close();
throw;
}
finally
{
sqlCommand?.Connection.Close();
}
}
public static object ExecuteScalar(string commandText)
{
SqlCommand sqlCommand = (SqlCommand)null;
try
{
sqlCommand = CreateCommand();
sqlCommand.CommandText = commandText;
sqlCommand.CommandType = CommandType.Text;
return sqlCommand.ExecuteScalar();
}
catch (Exception ex)
{
Rollback(sqlCommand);
throw;
}
finally
{
sqlCommand?.Connection.Close();
}
}
public static SqlDataReader ExecuteReader(string query, SqlParameter[] paramArr, CommandType cmdType, SqlConnection connection)
{
SqlCommand sqlCommand = (SqlCommand)null;
try
{
sqlCommand = new SqlCommand(query, connection);
sqlCommand.CommandType = cmdType;
if (paramArr != null)
{
for (int index = 0; index < paramArr.Length; ++index)
{
if (paramArr[index] != null)
sqlCommand.Parameters.Add(paramArr[index]);
}
}
return sqlCommand.ExecuteReader();
}
catch (SqlException ex)
{
sqlCommand?.Connection.Close();
}
catch (Exception ex)
{
throw;
}
}
public static DataTable Fill(string query, SqlParameter[] paramArr, CommandType cmdType)
{
SqlCommand selectCommand = (SqlCommand)null;
try
{
selectCommand = CreateCommand();
selectCommand.CommandText = query;
selectCommand.CommandType = cmdType;
selectCommand.CommandTimeout = 120;
if (paramArr != null)
{
for (int index = 0; index < paramArr.Length; ++index)
{
if (paramArr[index] != null)
selectCommand.Parameters.Add(paramArr[index]);
}
}
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(selectCommand);
DataTable dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
selectCommand.Connection.Close();
return dataTable;
}
catch (Exception ex)
{
Rollback(selectCommand);
throw;
}
finally
{
selectCommand?.Connection.Close();
}
}
}
}