//#define LOG_TRACE //@@ 2017.11.01;로그 확인시 사용;girak.kim using System; using System.Data; using System.Data.SqlClient; using Cosmos.UserFrame; using Cosmos.BaseFrame; /*-----------------------------------------------------------------------------------------------*/ // 설 명 : MSSQL DB 관련 처리 // 작 성 자 : // 변경 이력 : /*-----------------------------------------------------------------------------------------------*/ namespace Cosmos.Common { /// /// SqlDB에 대한 요약 설명입니다. /// public class SqlDB : IDisposable { /// /// SQL Connection Object /// public SqlConnection sqlConnection; /// /// SQL Command Object /// public SqlCommand sqlCommand; /// /// SQL Transaction Object /// public SqlTransaction sqlTransaction; // SQL Transaction 성공 Flag private bool bBeginTransactionFlag; // SQL Connection 문장 private string strSqlConnectionString; // SQL Message private string strMessage; /// /// SQL Connection 문장 /// public string SqlConnectionString { get { return this.strSqlConnectionString; } set { this.strSqlConnectionString = value; } } /// /// Connection을 얻음 /// public SqlConnection Connection { get { return this.sqlConnection; } } /// /// 커맨드를 얻음 /// public SqlCommand Command { get { return this.sqlCommand; } set { this.sqlCommand = value; } } #region 오버로드 생성자 함수 주석 /* /// /// 생성자 /// public MSSqlDB() { try { // Sql Connection Object Create sqlConnection = new SqlConnection(); // SQL Command Object sqlCommand = null; // SQL Transaction Object sqlTransaction = null; // SQL Transaction 성공 Flag bBeginTransactionFlag = false; // Sql Connection String this.strSqlConnectionString = ""; } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Process Exception !!! " + e.ToString()); } } /// /// 생성자 /// /// public MSSqlDB(string strSqlConnectionString) { try { // Sql Connection Object Create sqlConnection = new SqlConnection(); // SQL Command Object sqlCommand = null; // SQL Transaction Object sqlTransaction = null; // SQL Transaction 성공 Flag bBeginTransactionFlag = false; // DB Connect String this.strSqlConnectionString = strSqlConnectionString; // DB Open DBOpen(); } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Process Exception !!! " + e.ToString()); } } */ #endregion /// /// 생성자 /// /// /// /// /// public SqlDB(string pDataSource, string pInitialCatalog, string pUID, string pPassword) { try { if (IsDBOpen() != true) { // Sql Connection Object Create sqlConnection = new SqlConnection(); // SQL Command Object sqlCommand = null; // SQL Transaction Object sqlTransaction = null; // SQL Transaction 성공 Flag bBeginTransactionFlag = false; string strSqlConnectionString = "Data Source=" + pDataSource + "; Initial Catalog = " + pInitialCatalog + " ; uid = " + pUID + "; Password = " + pPassword; // DB Connect String this.strSqlConnectionString = strSqlConnectionString; // DB Open DBOpen(); } } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.SqlDB()", "Process Exception !!! " + e.ToString()); } } /// /// Database Open되어 있는지 확인 /// /// Database Open 여부 #region IS_SQL_OPEN public bool IsDBOpen() { try { // SQL Database가 Open 되어 있으면 SQL Database를 Close한다 //if (sqlConnection.State.HasFlag(ConnectionState.Open) == true) //if(sqlConnection.State == ConnectionState.Open) if(sqlConnection != null) { if (sqlConnection.State == ConnectionState.Open) { return true; } } } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.IsDBOpen()", "Process Exception !!! " + e.ToString()); } return false; } #endregion #region SQL_OPEN /// /// Sql Database Open /// /// Sql Database Open 성공여부를 리턴 public bool DBOpen() { // SQL Database Open try { // SQL Transaction 객체 소멸 DBTransactionDispose(); // SQL Database가 Open 되어 있으면 SQL Database를 Close한다 if (sqlConnection.State == ConnectionState.Open) { DBClose(); } // Connection String 설정 sqlConnection.ConnectionString = this.strSqlConnectionString; // SQL Open sqlConnection.Open(); return true; } catch (SqlException e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBOpen()", this.strSqlConnectionString + " : " + e.Message); return false; } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBOpen()", this.strSqlConnectionString + " : " + e.Message); return false; } } /// /// Sql Database Open /// /// Sql Database Connection string /// public bool DBOpen(string strConnectionString) { try { // Sql Connection Object Is Null if (sqlConnection == null) { sqlConnection = new SqlConnection(); } // Set Sql Connection String this.strSqlConnectionString = strConnectionString; // Sql Connection Open return DBOpen(); } catch (SqlException e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBOpen()", e.Message); return false; } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBOpen()", e.Message); return false; } } #endregion /// /// SQL Database Close /// #region SQL_CLOSE public void DBClose() { try { // SQL Transaction 객체 소멸 DBTransactionDispose(); // SQL Connection이 열려있으면 SQL Connection Close if (sqlConnection.State != ConnectionState.Closed) { sqlConnection.Close(); } } catch (SqlException e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBClose()", e.Message); } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBClose()", e.Message); } } #endregion /// /// SQL Transaction 객체 소멸 /// #region SQL_TRANSACTION_DISPOSE // SQL 객체 소멸 public void DBTransactionDispose() { // Transaction 객체 소멸 if (sqlTransaction != null) { sqlTransaction.Dispose(); sqlTransaction = null; } // Command 객체 소멸 if (sqlCommand != null) { sqlCommand.Dispose(); sqlCommand = null; } // SQL Transaction 성공 Flag bBeginTransactionFlag = false; } #endregion /// /// SQL Transction 시작 /// /// Tran Saction 성공여부 #region SQL_TRANSACTION_START public bool DBBeginTransaction() { try { // SQL Database가 Close 되어 있으면 SQL Database Open if (sqlConnection.State == ConnectionState.Closed) { // SQL Database Open if (DBOpen() == false) { return false; } } // SQL Transaction 객체 소멸 DBTransactionDispose(); // SQL Command 객체 생성 sqlCommand = sqlConnection.CreateCommand(); // SQL Transaction 생성 sqlTransaction = sqlConnection.BeginTransaction(); // SQL Command에 SQL Connection 설정 sqlCommand.Connection = sqlConnection; // SQL Command에 SQL Transaction 설정 sqlCommand.Transaction = sqlTransaction; // SQL Transaction 성공 Flag bBeginTransactionFlag = true; return true; } catch (SqlException e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBBeginTransaction()", e.Message); return false; } catch (Exception e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBBeginTransaction()", e.Message); return false; } } #endregion /// /// SQL Transaction 실행 /// /// SQL Transaction 실행 성공 여부 #region SQL_TRANSACTION_COMMIT public bool DBTransactionCommit() { try { // SQL Transaction 성공 Flag if (bBeginTransactionFlag == false) { // SQL Transaction 객체 소멸 DBTransactionDispose(); strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.SqlDB", "DBTransactionCommit", "SQL Transaction Not Start"); UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionCommit()", strMessage); return false; } // Transaction 객체 소멸 if (sqlTransaction == null) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionCommit()", "SQL Transaction Not Create"); return false; } // SQL Transaction Commit sqlTransaction.Commit(); // SQL Transaction 객체 소멸 DBTransactionDispose(); return true; } catch (SqlException e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionCommit()", e.Message); return false; } catch (Exception e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionCommit()", e.Message); return false; } } #endregion /// /// SQL Transaction 취소 /// #region SQL_TRANSACTION_ROLLBACK public bool DBTransactionRollback() { try { // SQL Transaction 성공 Flag if (bBeginTransactionFlag == false) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionRollback()", "SQL Transaction Not Start"); return false; } // Transaction 객체 소멸 if (sqlTransaction == null) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_DEBUG, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionRollback()", "SQL Transaction Not Create"); return false; } // SQL Transaction Rollback sqlTransaction.Rollback(); // SQL Transaction 객체 소멸 DBTransactionDispose(); return true; } catch (SqlException e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionRollback()", e.Message); return false; } catch (Exception e) { // SQL Transaction 객체 소멸 DBTransactionDispose(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBTransactionRollback()", e.Message); return false; } } #endregion #region SQL_DATA_NONQUERY public int DBExecuteNonQuery(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { return DBExecuteNonQuery(strCommand, sqlCommandType, sqlParameters, 120); } public int DBExecuteNonQuery(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, int nCommandTimeOut) { int iRet = 0; try { // SQL Database가 Close 되어 있으면 SQL Database Open if (sqlConnection.State == ConnectionState.Closed) { // SQL Transaction 성공 Flag if (bBeginTransactionFlag == true) { UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SQL Database Opened Error!!"); return UserCom.NG1; //Connection 에러 } else { // SQL Database Open if (DBOpen() == false) { return UserCom.NG1; //Connection 에러 } } } // SQL Transaction 성공 Flag if (bBeginTransactionFlag == false) { // SQL Transaction 객체 소멸 DBTransactionDispose(); sqlCommand = new SqlCommand(strCommand, sqlConnection); } else { sqlCommand.CommandText = strCommand; } if (strCommand == null) return UserCom.OK; if (strCommand.Trim() == "") return UserCom.OK; // SetSQL Command Type sqlCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlCommand.Parameters.Clear(); if (sqlParameters != null) { // SQL Paramater foreach (SqlParameter sqlParameter in sqlParameters) { sqlCommand.Parameters.Add(sqlParameter); } } sqlCommand.CommandTimeout = nCommandTimeOut; // SQL Data NonQuery 생성 iRet = sqlCommand.ExecuteNonQuery(); #if(LOG_TRACE) //@@ 로그 확인 용 UserLog.WriteLogFile(UserCom.LOG_OP, "DB Query : ", System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name + "," + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strCommand); #endif // Update문에서 적용된 행이 없으면 에러로 반환되어야 하므로 //if(iRet < 0) // 적용된 행이 없을 경우 에러를 반환하면 안되므로 원복(CHOCY - 20161215) //if( iRet <= 0) if(iRet < 0) { return UserCom.NG4; } return UserCom.OK; } catch (SqlException e) { if (e.Number == 2601 || e.Number == 2627) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG; // Duplicate key error } else { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG2; //SQL 에러 } } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "Exception!!! Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message); return UserCom.NG3; //기타 에러; } } /// /// return -> 1:OK, 0:Duplicate key 에러, -1:Connection 에러, -2:SQL 에러, -3:기타 에러 /// /// SQL 실행 명령 문장 /// public int DBExecuteNonQuery(string[] strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { return DBExecuteNonQuery(strCommand, sqlCommandType, sqlParameters, 120); } public int DBExecuteNonQuery(string[] strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, int nCommandTimeOut) { //return DBExecuteNonQuery(strCommand, sqlCommandType, sqlParameters, false); int iRet = 0; string eachCommand = string.Empty; try { // SQL Database가 Close 되어 있으면 SQL Database Open if (sqlConnection.State == ConnectionState.Closed) { // SQL Transaction 성공 Flag if (bBeginTransactionFlag == true) { UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SQL Database Opened Error!!"); return UserCom.NG1; //Connection 에러 } else { // SQL Database Open if (DBOpen() == false) { return UserCom.NG1; //Connection 에러 } } } // SQL Command 객체 생성 foreach (string sCommand in strCommand) { if (sCommand == null) continue; if (sCommand.Trim() == "") continue; eachCommand = sCommand; // SQL Transaction 성공 Flag if (bBeginTransactionFlag == false) { sqlCommand = new SqlCommand(eachCommand, sqlConnection); } else { sqlCommand.CommandText = eachCommand; } // SetSQL Command Type sqlCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlCommand.Parameters.Clear(); if (sqlParameters != null) { // SQL Paramater foreach (SqlParameter sqlParameter in sqlParameters) { sqlCommand.Parameters.Add(sqlParameter); } } sqlCommand.CommandTimeout = nCommandTimeOut; // SQL Data NonQuery 생성 iRet = sqlCommand.ExecuteNonQuery(); #if(LOG_TRACE) //@@ 로그 확인 용 UserLog.WriteLogFile(UserCom.LOG_OP, "DB Query : ", System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name + "," + System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", eachCommand); #endif // Update문에서 적용된 행이 없으면 에러로 반환되어야 하므로 //if(iRet < 0) // 적용된 행이 없을 경우 에러를 반환하면 안되므로 원복(CHOCY - 20161215) //if( iRet <= 0) if (iRet < 0) { return UserCom.NG4; //적용된 Row 없음 } } return UserCom.OK; } catch (SqlException e) { if (e.Number == 2601 || e.Number == 2627) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG; // Duplicate key error } else { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG2; //SQL 에러 } } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "Exception!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message); return UserCom.NG3; //기타 에러; } } /// /// return -> 1:OK, -1:Connection 에러, -2:SQL 에러, -3:기타 에러 /// /// SQL 실행 명령 문장 /// public int DBExecuteNonQuery(string[] strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, bool bStandalone) { return DBExecuteNonQuery(strCommand, sqlCommandType, sqlParameters, bStandalone, 120); } public int DBExecuteNonQuery(string[] strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, bool bStandalone, int nCommandTimeOut) { int iRet = 0; string eachCommand = string.Empty; try { if (bStandalone == true) DBBeginTransaction(); // SQL Database가 Close 되어 있으면 SQL Database Open if (sqlConnection.State == ConnectionState.Closed) { // SQL Transaction 성공 Flag if (bBeginTransactionFlag == true) { UserLog.WriteLogFile(UserCom.LOG_ERROR, UserCom.INFO_LEVEL, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SQL Database Opened Error!!"); return UserCom.NG1; //Connection 에러 } else { // SQL Database Open if (DBOpen() == false) { return UserCom.NG1; //Connection 에러 } } } // SQL Command 객체 생성 foreach (string sCommand in strCommand) { if (sCommand == null) continue; if (sCommand.Trim() == "") continue; eachCommand = sCommand; // SQL Transaction 성공 Flag if (bBeginTransactionFlag == false) { // SQL Transaction 객체 소멸 if (bStandalone == true) DBTransactionDispose(); sqlCommand = new SqlCommand(eachCommand, sqlConnection); } else { sqlCommand.CommandText = eachCommand; } // SetSQL Command Type sqlCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlCommand.Parameters.Clear(); if (sqlParameters != null) { // SQL Paramater foreach (SqlParameter sqlParameter in sqlParameters) { sqlCommand.Parameters.Add(sqlParameter); } } sqlCommand.CommandTimeout = nCommandTimeOut; // SQL Data NonQuery 생성 iRet = sqlCommand.ExecuteNonQuery(); // Update문에서 적용된 행이 없으면 에러로 반환되어야 하므로 //if(iRet < 0) // 적용된 행이 없을 경우 에러를 반환하면 안되므로 원복(CHOCY - 20161215) //if( iRet <= 0) if (iRet < 0) { if (bStandalone == true) DBTransactionRollback(); return UserCom.NG4; //적용된 Row 없음 } } if (bStandalone == true) DBTransactionCommit(); return UserCom.OK; } catch (SqlException e) { if (bStandalone == true) DBTransactionRollback(); if (e.Number == 2601 || e.Number == 2627) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG; // Duplicate key error } else { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "SqlException!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message + " " + e.Number.ToString()); return UserCom.NG2; //SQL 에러 } } catch (Exception e) { if (bStandalone == true) DBTransactionRollback(); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", "Exception!!! Query=" + eachCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBExecyteNonQuery()", e.Message); return UserCom.NG3; //기타 에러; } } #region 오버로드함수 주석 /* /// /// SQL DataNonQuery를 실행 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// 삽입/삭제/갱신 건수 public int DBExecuteNonQuery(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { try { // SQL Database가 Close 되어 있으면 SQL Database Open if(sqlConnection.State == ConnectionState.Closed) { // SQL Transaction 성공 Flag if(bBeginTransactionFlag == true) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteNonQuery", "SQL Database Opened Error!!"); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return -1; } else { // SQL Database Open if(DBOpen() == false) { return -1; } } } // SQL Transaction 성공 Flag if(bBeginTransactionFlag == false) { // SQL Transaction 객체 소멸 DBTransactionDispose(); // SQL Command 객체 생성 // SqlCommand sqlCommand = new SqlCommand(strCommand, sqlConnection); sqlCommand = new SqlCommand(strCommand, sqlConnection); } else { sqlCommand.CommandText = strCommand; } // SetSQL Command Type sqlCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlCommand.Parameters.Clear(); if(sqlParameters != null) { // SQL Paramater foreach(SqlParameter sqlParameter in sqlParameters) { sqlCommand.Parameters.Add(sqlParameter); } } // SQL Data NonQuery 생성 return sqlCommand.ExecuteNonQuery(); } catch(SqlException e) { strMessage = string.Format("[{0}][{1}]{2}{3}", "Cosmos.Common.MSSqlDB", "DBExecuteNonQuery", e.Message, e.Number); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return -1; } catch(Exception e) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteNonQuery", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return -1; } } /// /// /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// 삽입/삭제/갱신 건수 public int DBExecuteNonQuery(string strCommand, CommandType sqlCommandType) { return DBExecuteNonQuery(strCommand, sqlCommandType, (SqlParameter[])null); } /// /// /// /// SQL 실행 명령 문장 /// 삽입/삭제/갱신 건수 public int DBExecuteNonQuery(string strCommand) { return DBExecuteNonQuery(strCommand, CommandType.Text, (SqlParameter[])null); } */ #endregion #endregion #region SQL_DATA_TABLE /// /// SQL DataTable를 이용한 Select /// return -> 1:OK, -1:Connection 정보 에러, -2:SQL 에러, -3:기타 에러, 0:Select 결과 없음 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// Select 결과 DataTable /// Select 결과 public int DBDataTableSelect(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, out DataTable outDtReturn) { return DBDataTableSelect(strCommand, sqlCommandType, sqlParameters, out outDtReturn, 120); } public int DBDataTableSelect(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters, out DataTable outDtReturn, int nCommandTimeout) { outDtReturn = null; try { // SQL Connection String을 체크한다. this.strSqlConnectionString.Trim(); if (this.strSqlConnectionString.Length == 0) { return UserCom.NG1; //Connection 정보 에러 } // SQL Connection 객체 생성 SqlConnection sqlCnnt = new SqlConnection(); // SQL Connection 문자 설정 sqlCnnt.ConnectionString = this.strSqlConnectionString; // SQL DataAdapter 생성 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); // SQL DataAdapter Select Command 생성 sqlDataAdapter.SelectCommand = new SqlCommand(strCommand); // Set SQL Command Type sqlDataAdapter.SelectCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlDataAdapter.SelectCommand.Parameters.Clear(); if (sqlParameters != null) { // SQL Paramater foreach (SqlParameter sqlParameter in sqlParameters) { sqlDataAdapter.SelectCommand.Parameters.Add(sqlParameter); } } // SQL Connection 설정 sqlDataAdapter.SelectCommand.Connection = sqlCnnt; sqlDataAdapter.SelectCommand.CommandTimeout = nCommandTimeout; // SQL DataSet 객체 생성 DataSet sqlDataSet = new DataSet(); // SQL DataSet 객체에 실행된 Data를 체움 int iSelCnt = sqlDataAdapter.Fill(sqlDataSet); if (iSelCnt <= 0) { return UserCom.NG; //Select 결과 없음 } outDtReturn = sqlDataSet.Tables[0]; return UserCom.OK; } catch (SqlException e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBDataTableSelect()", "SqlException Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBDataTableSelect()", e.Message); return UserCom.NG2; //SQL 에러 } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBDataTableSelect()", "Exception Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, "SqlDB.DBDataTableSelect()", e.Message); return UserCom.NG3; //기타 에러 } } #region 오버로드함수 주석 /* /// /// SQL DataTable를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// Select DataTable public DataTable DBDataTableSelect(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { try { // SQL Connection String을 체크한다. this.strSqlConnectionString.Trim(); if (this.strSqlConnectionString.Length == 0) { return (DataTable)null; } // SQL Connection 객체 생성 SqlConnection sqlCnnt = new SqlConnection(); // SQL Connection 문자 설정 sqlCnnt.ConnectionString = this.strSqlConnectionString; // SQL DataAdapter 생성 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); // SQL DataAdapter Select Command 생성 sqlDataAdapter.SelectCommand = new SqlCommand(strCommand); UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Select Query=" + strCommand); // Set SQL Command Type sqlDataAdapter.SelectCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlDataAdapter.SelectCommand.Parameters.Clear(); if (sqlParameters != null) { // SQL Paramater foreach (SqlParameter sqlParameter in sqlParameters) { sqlDataAdapter.SelectCommand.Parameters.Add(sqlParameter); } } // SQL Connection 설정 sqlDataAdapter.SelectCommand.Connection = sqlCnnt; // SQL DataSet 객체 생성 DataSet sqlDataSet = new DataSet(); // SQL DataSet 객체에 실행된 Data를 체움 sqlDataAdapter.Fill(sqlDataSet); if (sqlDataSet.Tables.Count != 1) { return (DataTable)null; } // SQL Data Reader 생성 return sqlDataSet.Tables[0]; } catch (SqlException e) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "SqlException Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", e.Message); return (DataTable)null; } catch (Exception e) { UserLog.WriteLogFile(UserCom.LOG_IOS, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", "Exception Query=" + strCommand); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", e.Message); return (DataTable)null; } } /// /// SQL DataTable를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// Select DataTable public DataTable DBDataTableSelect(string strCommand, CommandType sqlCommandType) { return DBDataTableSelect(strCommand, sqlCommandType, (SqlParameter[])null); } /// /// SQL DataTable를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// Select DataTable public DataTable DBDataTableSelect(string strCommand) { return DBDataTableSelect(strCommand, CommandType.Text, (SqlParameter[])null); } */ #endregion #endregion #region IDisposable 멤버 /// /// /// public void Dispose() { if (sqlConnection != null) { // SQL Database Close DBClose(); // SQL Connection Object Dispose sqlConnection.Dispose(); sqlConnection = null; } } #endregion #region SQL_DATA_SET - 주석 /* /// /// SQL DataSet를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// Select DataSet public DataSet DBDataSetSelect(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { try { // SQL Connection String을 체크한다. this.strSqlConnectionString.Trim(); if(this.strSqlConnectionString.Length == 0) { return (DataSet)null; } // SQL Connection 객체 생성 SqlConnection sqlCnnt = new SqlConnection(); // SQL Connection 문자 설정 sqlCnnt.ConnectionString = this.strSqlConnectionString; // SQL DataAdapter 생성 SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); // SQL DataAdapter Select Command 생성 sqlDataAdapter.SelectCommand = new SqlCommand(strCommand); // Set SQL Command Type sqlDataAdapter.SelectCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlDataAdapter.SelectCommand.Parameters.Clear(); if(sqlParameters != null) { // SQL Paramater foreach(SqlParameter sqlParameter in sqlParameters) { sqlDataAdapter.SelectCommand.Parameters.Add(sqlParameter); } } // SQL Connection 설정 sqlDataAdapter.SelectCommand.Connection = sqlCnnt; // SQL DataSet 객체 생성 DataSet sqlDataSet = new DataSet(); // SQL DataSet 객체에 실행된 Data를 체움 sqlDataAdapter.Fill(sqlDataSet); // SQL Data Reader 생성 return sqlDataSet; } catch(SqlException e) { //strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBDataSetSelect", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", e.Message); return (DataSet)null; } catch(Exception e) { //strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBDataSetSelect", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", e.Message); return (DataSet)null; } } /// /// SQL DataSet를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// Select DataSet public DataSet DBDataSetSelect(string strCommand, CommandType sqlCommandType) { return DBDataSetSelect(strCommand, sqlCommandType, (SqlParameter[])null); } /// /// SQL DataSet를 이용한 Select문장 /// /// SQL 실행 명령 문장 /// Select DataSet public DataSet DBDataSetSelect(string strCommand) { return DBDataSetSelect(strCommand, CommandType.Text, (SqlParameter[])null); } */ #endregion #region SQL_DATA_READER - 주석 /* /// /// SQL DataReader를 실행 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// public SqlDataReader DBExecuteReader(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { try { // SQL Database가 Close 되어 있으면 SQL Database Open if(sqlConnection.State == ConnectionState.Closed) { // SQL Database Open if(DBOpen() == false) { return (SqlDataReader)null; } } // SQL Command 객체 생성 SqlCommand sqlLocalCommand = new SqlCommand(strCommand, sqlConnection); // SetSQL Command Type sqlLocalCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlLocalCommand.Parameters.Clear(); if(sqlParameters != null) { // SQL Paramater foreach(SqlParameter sqlParameter in sqlParameters) { sqlLocalCommand.Parameters.Add(sqlParameter); } } // SQL Data Reader 생성 return sqlLocalCommand.ExecuteReader(); } catch(SqlException e) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteReader", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return (SqlDataReader)null; } catch(Exception e) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteReader", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return (SqlDataReader)null; } } /// /// /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// public SqlDataReader DBExecuteReader(string strCommand, CommandType sqlCommandType) { return DBExecuteReader(strCommand, sqlCommandType, (SqlParameter[])null); } /// /// /// /// SQL 실행 명령 문장 /// public SqlDataReader DBExecuteReader(string strCommand) { return DBExecuteReader(strCommand, CommandType.Text, (SqlParameter[])null); } */ #endregion #region SQL_DTAT_SCALAR - 주석 /* /// /// SQL DataScalar를 실행 /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 실행 명령 파라메타 /// SQL 단일 실행 결과 객체 public object DBExecuteScalar(string strCommand, CommandType sqlCommandType, SqlParameter[] sqlParameters) { try { // SQL Database가 Close 되어 있으면 SQL Database Open if(sqlConnection.State == ConnectionState.Closed) { // SQL Database Open if(DBOpen() == false) { return (object)null; } } // SQL Command 객체 생성 SqlCommand sqlLocalCommand = new SqlCommand(strCommand, sqlConnection); // SetSQL Command Type sqlLocalCommand.CommandType = sqlCommandType; // Set SQL Parameter sqlLocalCommand.Parameters.Clear(); if(sqlParameters != null) { // SQL Paramater foreach(SqlParameter sqlParameter in sqlParameters) { sqlLocalCommand.Parameters.Add(sqlParameter); } } // SQL Data Reader 생성 return (object)sqlLocalCommand.ExecuteScalar(); } catch(SqlException e) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteScalar", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return (object)null; } catch(Exception e) { strMessage = string.Format("[{0}][{1}]{2}", "Cosmos.Common.MSSqlDB", "DBExecuteScalar", e.Message); UserLog.WriteLogFile(UserCom.LOG_ERROR, System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", strMessage); return (object)null; } } /// /// /// /// SQL 실행 명령 문장 /// SQL 실행 명령 유형 /// SQL 단일 실행 결과 객체 public object DBExecuteScalar(string strCommand, CommandType sqlCommandType) { return DBExecuteScalar(strCommand, sqlCommandType, (SqlParameter[])null); } /// /// /// /// SQL 실행 명령 문장 /// SQL 단일 실행 결과 객체 public object DBExecuteScalar(string strCommand) { return DBExecuteScalar(strCommand, CommandType.Text, (SqlParameter[])null); } */ #endregion } }