using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Data.SqlClient;
using System.Data;
namespace REPORT_API_ver6.Services
{
public class DAL
{
static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(DAL));
///
/// Report DB 연결
///
/// DB 연결 객체
public SqlConnection ConnectReport()
{
SqlConnection connection = new SqlConnection();
try
{
var configuration = Common.GetConfiguration();
connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("ReportString").Value;
connection.Open();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ConnectReport : " + ex.ToString() + Environment.NewLine);
}
return connection;
}
///
/// KON / KIWI DB 연결
///
/// 0:kon 1:kiwi
/// DB 연결 객체
public SqlConnection ConnectKonKiwi(int isKiwi)
{
SqlConnection connection = new SqlConnection();
string connectionString = string.Empty;
try
{
var configuration = Common.GetConfiguration();
if (isKiwi == 0)
{
connectionString = configuration.GetSection("ConnectionStrings").GetSection("KonString").Value;
}
else
{
connectionString = configuration.GetSection("ConnectionStrings").GetSection("KiwiString").Value;
}
connection.ConnectionString = connectionString;
connection.Open();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ConnectKonKiwi : " + connectionString + "///" + ex.ToString() + Environment.NewLine);
}
return connection;
}
///
/// 인트라넷 DB 연결
///
/// DB 연결 객체
public SqlConnection ConnectIntra()
{
SqlConnection connection = new SqlConnection();
try
{
var configuration = Common.GetConfiguration();
connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("IntraString").Value;
connection.Open();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ConnectIntra : " + ex.ToString() + Environment.NewLine);
}
return connection;
}
///
/// DB 연결
///
/// Connection String
/// DB 연결 객체
public SqlConnection Connect(string connectionString)
{
SqlConnection connection = new SqlConnection();
try
{
connection.ConnectionString = connectionString;
connection.Open();
}
catch (Exception ex)
{
logger.Info("DAL class exception in Connect : " + ex.ToString() + Environment.NewLine);
}
return connection;
}
///
/// DB 연결 해제
///
/// DB 연결 객체
public void Disconnect(SqlConnection connection)
{
try
{
connection.Close();
connection.Dispose();
}
catch (Exception ex)
{
logger.Info("DAL class exception in Disconnect : " + ex.ToString() + Environment.NewLine);
}
}
///
/// DB접속객체에서 Procedure 실행하여 DataReader로 반환
///
/// 프로시저명
/// DB 연결 객체
/// 인수배열
/// 결과 행이 저장되어 있는 DataReader 객체
public SqlDataReader GetDataReader(string procedureName, SqlConnection connection, List sqlParameter)
{
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
for (int i = 0; i < sqlParameter.Count; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return reader;
}
///
/// 프로시저를 수행하여 DataReader로 반환
///
/// 프로시저명
/// DB 연결 객체
/// 결과 행이 저장되어 있는 DataReader 객체
public SqlDataReader GetDataReaderWithProcedure(string procedureName, SqlConnection connection)
{
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return reader;
}
///
/// SQL문을 수행하여 DataReader로 반환
///
/// SQL문
/// DB 연결 객체
/// 결과 행이 저장되어 있는 DataReader 객체
public SqlDataReader GetDataReader(string sql, SqlConnection connection)
{
SqlDataReader reader = null;
/*
string sessionID = hcAccessor.HttpContext.Session.Id;
//killProcessQuery(sessionID, connection);
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
*/
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandTimeout = 300;
try
{
cmd.CommandType = CommandType.Text;
reader = cmd.ExecuteReader();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return reader;
}
///
/// 프로시저를 수행하여 DataTable 객체 반환
///
/// 프로시저명
/// DB 연결 객체
/// 인수배열
/// 프로시저 수행 결과 DataTable
public DataTable GetDataTable(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
{
DataTable dataTable = new DataTable();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
for (int i = 0; i < sqlParameter.Length; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
dataTable.Load(reader);
reader.Close();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return dataTable;
}
///
/// 파라미터 없이 프로시저를 수행하여 DataTable 객체 반환
///
/// 프로시저명
/// DB 연결 객체
///
public DataTable GetDataTableNonePara(string procedureName, SqlConnection connection)
{
DataTable dataTable = new DataTable();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
dataTable.Load(reader);
reader.Close();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return dataTable;
}
///
/// 프로시저를 수행하여 DataTable 객체 반환
///
/// 프로시저명
/// DB 연결 객체
/// 인수 List
///
public DataTable GetDataTable(string procedureName, SqlConnection connection, List sqlParameter)
{
DataTable dataTable = new DataTable();
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
for (int i = 0; i < sqlParameter.Count; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
reader = cmd.ExecuteReader();
dataTable.Load(reader);
reader.Close();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataTable(" + procedureName + ") : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return dataTable;
}
///
/// SQL문을 수행하여 DataTable 객체 반환
///
/// SQL문
/// DB 연결 객체
/// SQL문 수행 결과 DataTable
public DataTable GetDataTable(string sql, SqlConnection connection)
{
DataTable dataTable = new DataTable();
SqlDataReader reader = null;
/*
string sessionID = hcAccessor.HttpContext.Session.Id;
//killProcessQuery(sessionID, connection);
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
*/
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandTimeout = 300;
try
{
cmd.CommandType = CommandType.Text;
reader = cmd.ExecuteReader();
dataTable.Load(reader);
reader.Close();
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataTable : " + Environment.NewLine + sql + Environment.NewLine + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return dataTable;
}
///
/// 프로시저를 수행하여 DataSet 객체 반환
///
/// 프로시저명
/// DB 연결 객제
/// 인수배열
/// 프로시저를 수행결과 DataSet
public DataSet GetDataSet(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
{
SqlDataAdapter adapter = null;
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(procedureName, connection);
cmd.CommandTimeout = 300;
try
{
for (int i = 0; i < sqlParameter.Length; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
cmd.CommandType = CommandType.StoredProcedure;
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
adapter.Dispose();
}
return ds;
}
///
/// SQL문을 수행하여 DataSet 객체 반환
///
/// 프로시저명
/// DB 연결 객제
/// 인수배열
/// 프로시저를 수행결과 DataSet
public DataSet GetDataSet(string sql, SqlConnection connection)
{
SqlDataAdapter adapter = null;
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandTimeout = 300;
try
{
cmd.CommandType = CommandType.Text;
adapter = new SqlDataAdapter(cmd);
adapter.Fill(ds);
}
catch (Exception ex)
{
logger.Info("DAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
adapter.Dispose();
}
return ds;
}
///
/// SQL문 실행
///
/// SQL문
/// DB 연결 객체
/// 실패 : -1
public int ExecuteSQL(string sql, SqlConnection connection)
{
int affectedRow = 0;
/*
string sessionID = hcAccessor.HttpContext.Session.Id;
//killProcessQuery(sessionID, connection);
sql = "--" + sessionID + "--" + Environment.NewLine + sql;
*/
try
{
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandTimeout = 300;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (Exception ex)
{
affectedRow = -1;
logger.Info("DAL class exception in ExecuteSQL : " + ex.ToString() + Environment.NewLine);
}
return affectedRow;
}
///
/// 프로시저 수행
///
/// 프로시저명
/// DB 연결 객체
/// 인수 배열
/// 실패 : -1
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection, SqlParameter[] sqlParameter)
{
int affectedRow = -1;
SqlCommand cmd = new SqlCommand(procedureName, connection);
try
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
for (int i = 0; i < sqlParameter.Length; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
affectedRow = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return affectedRow;
}
///
/// 프로시저 수행
///
/// 프로시저명
/// DB 연결 객체
/// 인수 List
/// 실패 : -1
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection, List sqlParameter)
{
int affectedRow = -1;
SqlCommand cmd = new SqlCommand(procedureName, connection);
try
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
for (int i = 0; i < sqlParameter.Count; i++)
{
cmd.Parameters.Add(sqlParameter[i]);
}
affectedRow = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return affectedRow;
}
///
/// 프로시저 수행
///
/// 프로시저명
/// DB 연결 객체
/// 실패 : -1
public int ExecuteStoredProcedure(string procedureName, SqlConnection connection)
{
int affectedRow = -1;
SqlCommand cmd = new SqlCommand(procedureName, connection);
try
{
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 300;
affectedRow = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
logger.Info("DAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine);
}
finally
{
cmd.Dispose();
}
return affectedRow;
}
///
/// 쿼리 강제로 KILL
///
/// Session ID
/// DB 연결 객체
public void killProcessQuery(string sessionID, SqlConnection connection)
{
string killQuery = "declare @spid int" + Environment.NewLine;
killQuery += " select @spid = session_id" + Environment.NewLine;
killQuery += " from sys.dm_exec_requests handle" + Environment.NewLine;
killQuery += " outer apply sys.fn_get_sql(handle.sql_handle) spname" + Environment.NewLine;
killQuery += " where spname.text is not null" + Environment.NewLine;
killQuery += " and spname.text like '%" + sessionID + "%'" + Environment.NewLine;
killQuery += " and spname.text not like '%from sys.dm_exec_requests handle%'" + Environment.NewLine;
killQuery += " if @spid <> ''" + Environment.NewLine;
killQuery += " begin" + Environment.NewLine;
killQuery += " exec('kill '+@spid)" + Environment.NewLine;
killQuery += " end" + Environment.NewLine;
try
{
SqlCommand cmd = new SqlCommand(killQuery, connection);
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandTimeout = 300;
cmd.ExecuteNonQuery();
cmd.Dispose();
}
catch (Exception ex)
{
logger.Info("DAL class exception in killProcessQuery : " + ex.ToString() + Environment.NewLine);
}
}
}
}