using System.Data; using MySqlConnector; namespace KART_API.Utils { public class MYDAL { static readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MSDAL)); /// /// Config 파일 로드 /// /// public IConfiguration GetConfiguration() { var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); return builder.Build(); } /// /// 새싹 DB 연결 /// /// DB 연결 객체 public MySqlConnection ConnectNewssak() { MySqlConnection connection = new MySqlConnection(); string connectionString = string.Empty; try { var configuration = GetConfiguration(); connection.ConnectionString = configuration.GetSection("ConnectionStrings").GetSection("NewssakString").Value; connection.Open(); } catch (Exception ex) { logger.Info("MYDAL class exception in ConnectKonKiwi : " + connectionString + "///" + ex.ToString() + Environment.NewLine); } return connection; } /// /// DB 연결 /// /// Connection String /// DB 연결 객체 public MySqlConnection Connect(string connectionString) { MySqlConnection connection = new MySqlConnection(); try { connection.ConnectionString = connectionString; connection.Open(); } catch (Exception ex) { logger.Info("MYDAL class exception in Connect : " + ex.ToString() + Environment.NewLine); } return connection; } /// /// DB 연결 해제 /// /// DB 연결 객체 public void Disconnect(MySqlConnection connection) { try { connection.Close(); connection.Dispose(); } catch (Exception ex) { logger.Info("MYDAL class exception in Disconnect : " + ex.ToString() + Environment.NewLine); } } /// /// DB접속객체에서 Procedure 실행하여 DataReader로 반환 /// /// 프로시저명 /// DB 연결 객체 /// 인수배열 /// 결과 행이 저장되어 있는 DataReader 객체 public MySqlDataReader GetDataReader(string procedureName, MySqlConnection connection, List sqlParameter) { MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(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("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return reader; } /// /// 프로시저를 수행하여 DataReader로 반환 /// /// 프로시저명 /// DB 연결 객체 /// 결과 행이 저장되어 있는 DataReader 객체 public MySqlDataReader GetDataReaderWithProcedure(string procedureName, MySqlConnection connection) { MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(procedureName, connection); cmd.CommandTimeout = 300; try { cmd.CommandType = CommandType.StoredProcedure; reader = cmd.ExecuteReader(); } catch (Exception ex) { logger.Info("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return reader; } /// /// SQL문을 수행하여 DataReader로 반환 /// /// SQL문 /// DB 연결 객체 /// 결과 행이 저장되어 있는 DataReader 객체 public MySqlDataReader GetDataReader(string sql, MySqlConnection connection) { MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(sql, connection); cmd.CommandTimeout = 300; try { cmd.CommandType = CommandType.Text; reader = cmd.ExecuteReader(); } catch (Exception ex) { logger.Info("MYDAL class exception in GetDataReader : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return reader; } /// /// 프로시저를 수행하여 DataTable 객체 반환 /// /// 프로시저명 /// DB 연결 객체 /// 인수배열 /// 프로시저 수행 결과 DataTable public DataTable GetDataTable(string procedureName, MySqlConnection connection, MySqlParameter[] sqlParameter) { DataTable dataTable = new DataTable(); MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(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("MYDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return dataTable; } /// /// 파라미터 없이 프로시저를 수행하여 DataTable 객체 반환 /// /// 프로시저명 /// DB 연결 객체 /// public DataTable GetDataTableNonePara(string procedureName, MySqlConnection connection) { DataTable dataTable = new DataTable(); MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(procedureName, connection); cmd.CommandTimeout = 300; try { cmd.CommandType = CommandType.StoredProcedure; reader = cmd.ExecuteReader(); dataTable.Load(reader); reader.Close(); } catch (Exception ex) { logger.Info("MYDAL class exception in GetDataTable : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return dataTable; } /// /// 프로시저를 수행하여 DataTable 객체 반환 /// /// 프로시저명 /// DB 연결 객체 /// 인수 List /// public DataTable GetDataTable(string procedureName, MySqlConnection connection, List sqlParameter) { DataTable dataTable = new DataTable(); MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(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("MYDAL 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, MySqlConnection connection) { DataTable dataTable = new DataTable(); MySqlDataReader reader = null; MySqlCommand cmd = new MySqlCommand(sql, connection); cmd.CommandTimeout = 300; try { cmd.CommandType = CommandType.Text; reader = cmd.ExecuteReader(); dataTable.Load(reader); reader.Close(); } catch (Exception ex) { logger.Info("MYDAL 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, MySqlConnection connection, MySqlParameter[] sqlParameter) { MySqlDataAdapter adapter = null; DataSet ds = new DataSet(); MySqlCommand cmd = new MySqlCommand(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 MySqlDataAdapter(cmd); adapter.Fill(ds); } catch (Exception ex) { logger.Info("MYDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); adapter.Dispose(); } return ds; } /// /// SQL문을 수행하여 DataSet 객체 반환 /// /// 프로시저명 /// DB 연결 객제 /// 인수배열 /// 프로시저를 수행결과 DataSet public DataSet GetDataSet(string sql, MySqlConnection connection) { MySqlDataAdapter adapter = null; DataSet ds = new DataSet(); MySqlCommand cmd = new MySqlCommand(sql, connection); cmd.CommandTimeout = 300; try { cmd.CommandType = CommandType.Text; adapter = new MySqlDataAdapter(cmd); adapter.Fill(ds); } catch (Exception ex) { logger.Info("MYDAL class exception in GetDataSet : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); adapter.Dispose(); } return ds; } /// /// SQL문 실행 /// /// SQL문 /// DB 연결 객체 /// 실패 : -1 public int ExecuteSQL(string sql, MySqlConnection connection) { int affectedRow = 0; try { MySqlCommand cmd = new MySqlCommand(sql, connection); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandTimeout = 300; cmd.ExecuteNonQuery(); cmd.Dispose(); } catch (Exception ex) { affectedRow = -1; logger.Info("MYDAL class exception in ExecuteSQL : " + ex.ToString() + Environment.NewLine); } return affectedRow; } /// /// 프로시저 수행 /// /// 프로시저명 /// DB 연결 객체 /// 인수 배열 /// 실패 : -1 public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection, MySqlParameter[] sqlParameter) { int affectedRow = -1; MySqlCommand cmd = new MySqlCommand(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("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return affectedRow; } /// /// 프로시저 수행 /// /// 프로시저명 /// DB 연결 객체 /// 인수 List /// 실패 : -1 public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection, List sqlParameter) { int affectedRow = -1; MySqlCommand cmd = new MySqlCommand(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("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return affectedRow; } /// /// 프로시저 수행 /// /// 프로시저명 /// DB 연결 객체 /// 실패 : -1 public int ExecuteStoredProcedure(string procedureName, MySqlConnection connection) { int affectedRow = -1; MySqlCommand cmd = new MySqlCommand(procedureName, connection); try { cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.CommandTimeout = 300; affectedRow = cmd.ExecuteNonQuery(); } catch (Exception ex) { logger.Info("MYDAL class exception in ExecuteStoredProcedure : " + ex.ToString() + Environment.NewLine); } finally { cmd.Dispose(); } return affectedRow; } } }