using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data.SqlClient;    // SqlConnection
using Oracle.ManagedDataAccess.Client;
using System.Data;              // ConnectionState, DataTable

namespace Post
{
    public class ProcessDatabase
    {
        public ExecuteOracleDatabase executeOracle;
        public ExecuteSqlserverDatabase executeSqlserver;
        public SqlConnection sqlConn;
        public OracleConnection orclConn;

        public string DB_TYPE { get; set; }

        public ProcessDatabase(string type)
        {
            DB_TYPE = type;
        }

        #region ConnectionDB() : db connection
        //public bool ConnectionDB(string ip, int port, string dbname, string username, string password)
        public Object ConnectionDB(string ip, int port, string dbname, string username, string password)
        {
            ConstValues constValues = new ConstValues();
            ConstValuesConfig constValuesConfig = new ConstValuesConfig();
            Object conn = new Object();
            //bool result = true;

            Console.WriteLine("DB_TYPE : " + DB_TYPE);

            try
            {
                // conn validation check
                if (ip.Length == 0)
                {
                    ip = Environment.GetEnvironmentVariable("localhost");
                    Console.WriteLine("localhost : " + ip);
                } else if (port == 0)
                {
                    if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                    {
                        port = constValuesConfig.CONST_DEFAULT_CONN_PORT_ORACLE;
                    } else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                    {
                        port = constValuesConfig.CONST_DEFAULT_CONN_PORT_SQLSERVER;
                    }
                }

                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    conn = ConnectOracle(ip, port, dbname, username, password);
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    conn = ConnectSqlserver(ip, port, dbname, username, password);
                }


            } catch(Exception e)
            {
                Console.WriteLine("connectiondb : " + e.ToString());
                return null;
            }

            return conn;
        }
        #endregion

        #region ConnectSqlserver() : check sqlserver connection
        public SqlConnection ConnectSqlserver(string ip, int port, string dbname, string username, string password)
        {
            Console.WriteLine("ConnectSqlserver");
            sqlConn = new SqlConnection();

            string dataSource = String.Format(@"{0},{1}", ip, port);
            sqlConn.ConnectionString = String.Format("DATA SOURCE={0};Initial Catalog={1};User id={2};Password={3};Pooling={4};Connection TImeout={5};Min Pool Size={6};Max pool Size={7}", dataSource, dbname, username, password, "true", 120, 1, 10);
            sqlConn.Open();
            Console.WriteLine("sqlserver state : " + sqlConn.State);

            if (sqlConn.State == ConnectionState.Broken || sqlConn.State == ConnectionState.Closed)
            {
                return null;
            }

            executeSqlserver = new ExecuteSqlserverDatabase(sqlConn);
            return sqlConn;
        }
        #endregion

        #region ConnectOracle() : check oracle connection
        public OracleConnection ConnectOracle(string ip, int port, string dbname, string username, string password)
        {
            Console.WriteLine("ConnectOracle");
            orclConn = new OracleConnection();
            string dataSource = String.Format("{0}:{1}/{2}", ip, port, dbname);
            orclConn.ConnectionString = String.Format("DATA SOURCE={0};USER ID={1};Password={2};Pooling={3};Connection Timeout={4};Min Pool Size={5};Max pool Size={6}", dataSource, username, password, "true", 120, 1, 10);
            orclConn.Open();

            Console.WriteLine("oracle state : " + orclConn.State);

            executeOracle = new ExecuteOracleDatabase(orclConn);
            return orclConn;
        }
        #endregion

        #region ExecuteQuery() : execute query(create, insert)
        public bool ExecuteQuery(string type, List<Struct.TraceDml> tracingRecordDmlList, List<Struct.TracingRecordDataDefinition> tracingDefinitionDataList)
        {
            bool isEecute = true;
            ConstValuesTracing constValuesTracing = new ConstValuesTracing();
            Console.WriteLine("partition : " + tracingRecordDmlList[0].partition);

            try
            {
                Console.WriteLine("tracingDefinitionDataList *** " + tracingDefinitionDataList.Count);
                Console.WriteLine("tracingRecordDmlList *** " + tracingRecordDmlList.Count);

            } catch(Exception e)
            {
                Console.WriteLine("execute : " + e.ToString());
            }

            return isEecute;
        }
        #endregion

        #region ExecuteDdl() : execute ddl
        public bool ExecuteDdl(string ddlQuery)
        {
            bool isExecute = true;
            ConstValues constValues = new ConstValues();
            ConstValuesTracing constValuesTracing = new ConstValuesTracing();
            Console.WriteLine("ddlQuery *** " + ddlQuery);

            try
            {
                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    using(OracleCommand cmd = new OracleCommand(ddlQuery, orclConn))
                    {
                        int cnt = cmd.ExecuteNonQuery();
                        Console.WriteLine("orcl cnt : " + cnt);
                    }
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    using (SqlCommand cmd = new SqlCommand(ddlQuery, sqlConn))
                    {
                        int cnt = cmd.ExecuteNonQuery();
                        Console.WriteLine("cnt : " + cnt);
                    }
                }
            }
            catch(SqlException e)
            {
                //Console.WriteLine("중복 create : " + e.ToString());
                return false;
            }
            catch (Exception e)
            {
                Console.WriteLine("execute : " + e.ToString());
            }

            return isExecute;
        }
        #endregion





        #region SetMetaInfoListByTraceDmlList()
        public void SetMetaInfoListByTraceDmlList(List<Struct.TraceDml> tracingRecordDmlList, ref List<ConstStruct.MetaInfo> metaInfoList)
        {
            try
            {
                Console.WriteLine("\n[ SetMetaInfoListByTraceDmlList start]");
                for(int i = 0; i < tracingRecordDmlList.Count; i++)
                {
                    string schema = tracingRecordDmlList[i].schema;
                    string table = tracingRecordDmlList[i].object_n;

                    DataTable dt = ExcuteSelectTableMetaInfo("", schema, table);
                    ConstStruct.MetaInfo info = new ConstStruct.MetaInfo();
                    info.schema = schema;
                    info.tablename = table;
                    info.data = dt;
                    metaInfoList.Add(info);
                    Console.WriteLine("metainfolist info : " + metaInfoList[i].tablename);
                    Console.WriteLine("metainfolist info : " + metaInfoList[i].data.Rows.Count);

                    dt = null;
                    info = null;
                    Console.WriteLine("[ SetMetaInfoListByTraceDmlList end ]");
                }
            } catch(Exception e)
            {
                Console.WriteLine("setmeta : " + e.ToString());
            }
        }
        #endregion

        #region ExcuteSelectTableMetaInfo() : table 메타 정보 select control, virtual column 미포함
        public DataTable ExcuteSelectTableMetaInfo(string container, string schema, string table)
        {
            Console.WriteLine("\n( ExcuteSelectTableMetaInfo start )");
            ConstValues constValues = new ConstValues();
            ConstFunc constFunc = new ConstFunc();
            //constFunc.WriteTraceLog("Control ExcuteSelectTableMetaInfo()", constLoggertype, constAlias, constLogconfig, new string[] { "container:" + container, "schema:" + schema, "table:" + table }, isTrace);

            DataTable result = new DataTable();

            try
            {
                Console.WriteLine("db type : " + DB_TYPE);
                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("oracle process : "+ executeOracle.orclConn);
                    
                    result = executeOracle.SelectTableMetaInfo(schema, table);
                    Console.WriteLine("result >>>>>>> :: " + result.Rows.Count);
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                    result = executeSqlserver.SelectTableMetaInfo(container, schema, table);
            }
            catch (Exception e)
            {
                Console.WriteLine("ExcuteSelectTableMetaInfo :: " + e.ToString());
                return null;
            }

            return result;
        }
        #endregion

        #region GetTableMetaInfo() : 메타 정보 조회하고 없으면 새로 조회하여 meta list 에 추가
        public DataTable GetTableMetaInfo(string container, string schema, string tablename, ref List<ConstStruct.MetaInfo> metaInfoList)
        {
            Console.WriteLine("\n[ GetTableMetaInfo start ]");
            DataTable result = new DataTable();

            try
            {
                // 메타정보 리스트에 존재여부 체크, 있으면 리턴
                // 메타정보 리스트에 없으면 메타정보 조회후 리스트에 추가하고 리턴
                bool isExist = false;

                for (int i = 0; i < metaInfoList.Count; i++)
                {
                    if (metaInfoList[i].schema.Equals(schema, StringComparison.OrdinalIgnoreCase) && metaInfoList[i].tablename.Equals(tablename, StringComparison.OrdinalIgnoreCase))
                    {
                        isExist = true;
                        result = metaInfoList[i].data;
                        break;
                    }
                }
                if (!isExist)
                {
                    Console.WriteLine("GetTableMetaInfo ] not exist");
                    DataTable dt = ExcuteSelectTableMetaInfo(container, schema, tablename);
                    ConstStruct.MetaInfo item = new ConstStruct.MetaInfo();
                    item.schema = schema;
                    item.tablename = tablename;
                    item.data = dt;
                    result = dt;

                    dt = null;
                    item = null;
                }
                Console.WriteLine("[ GetTableMetaInfo end ]");
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] PostProgram GetTableMetaInfo() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
                result = null;
            }

            return result;
        }
        #endregion

        #region GetMetaInfo() : meta list 에서 해당 meta info 구함
        public DataTable GetMetaInfo(List<ConstStruct.MetaInfo> metaInfoList, string schema, string table)
        {
            DataTable metaInfo = new DataTable();
            try
            {
                for (int i = 0; i < metaInfoList.Count; i++)
                {
                    if (metaInfoList[i].schema.Equals(schema, StringComparison.OrdinalIgnoreCase) && metaInfoList[i].tablename.Equals(table, StringComparison.OrdinalIgnoreCase))
                    {
                        metaInfo = metaInfoList[i].data;
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] QueryFunc GetMetaInfo() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
                return null;
            }

            return metaInfo;
        }
        #endregion

        #region GetMetaDefList() : meta, definition 정보
        public List<ConstStruct.MetaDefInfo> GetMetaDefList(string schema, string table, DataTable metaInfo, List<Struct.TracingRecordDataDefinition> definitionDataList)
        {
            ConstValues constValues = new ConstValues();
            List<ConstStruct.MetaDefInfo> metaDefList = new List<ConstStruct.MetaDefInfo>();

            try
            {
                // record 에 해당하는 definition 찾음
                Struct.TracingRecordDataDefinition definitionColumn = new Struct.TracingRecordDataDefinition();
                if (definitionDataList.Count > 0)
                {
                    for (int j = 0; j < definitionDataList.Count; j++)
                    {
                        string[] arrTableName = definitionDataList[j].table_def.table_name.Split('.');
                        string strSchema = arrTableName[0];
                        string strTable = arrTableName[1];

                        if (strSchema.Equals(schema, StringComparison.OrdinalIgnoreCase) && strTable.Equals(table, StringComparison.OrdinalIgnoreCase))
                        {
                            definitionColumn = definitionDataList[j];
                            break;
                        }
                    }
                }

                if (definitionColumn.column_def != null && definitionColumn.column_def.Count > 0)
                {
                    // meta check list 초기set                    
                    for (int i = 0; i < definitionColumn.column_def.Count; i++)
                    {
                        for (int j = 0; j < metaInfo.Rows.Count; j++)
                        {
                            if (definitionColumn.column_def[i].name.Equals(metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_COLUMN_NAME].ToString(), StringComparison.OrdinalIgnoreCase))
                            {
                                ConstStruct.MetaDefInfo item = new ConstStruct.MetaDefInfo();
                                item.colno = Int32.Parse(metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_COLUMN_ID].ToString()) - 1;
                                item.colname = metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_COLUMN_NAME].ToString();
                                item.dataType = metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_DATA_TYPE].ToString();
                                item.definitionType = (short)(definitionColumn.column_def[i].datatype + definitionColumn.column_def[i].sub_datatype);
                                item.dataLength = Int32.Parse(metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_DATA_LENGTH].ToString());
                                item.value = null;
                                if (metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_IS_KEY].ToString().Equals("Y", StringComparison.OrdinalIgnoreCase))
                                    item.isPk = true;
                                else
                                    item.isPk = false;
                                if (metaInfo.Rows[j][constValues.CONST_TABLE_METAINFO_IS_IDENTITY].ToString().Equals("Y", StringComparison.OrdinalIgnoreCase))
                                    item.isIdentity = true;
                                else
                                    item.isIdentity = false;
                                metaDefList.Add(item);
                                break;
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] QueryFunc GetMetaDefInfoList() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
            }

            return metaDefList;

        }
        #endregion

        #region GetInsertColNameFromRecord() : insert column 이름 가져옴
        public void GetInsertColNameFromRecord(Struct.TracingRecordDml record, List<ConstStruct.MetaDefInfo> metaDefList, ref string colNames, ref string colValues, ref List<ConstStruct.ParameterOrderInfo> parameterOrderInfoList)
        {
            ConstValues constValues = new ConstValues();
            ConstValuesTracing constValuesTracing = new ConstValuesTracing();

            try
            {
                for(int i = 0; i < metaDefList.Count; i++)
                {
                    bool isExist = true;
                    int metaDefNum = 0;

                    if(metaDefList.Count > 0)
                    {
                        for (int j = 0; j < record.dataList.Count; j++)
                        {
                            if (record.dataList[j].colno == metaDefList[i].colno)
                            {
                                if (metaDefList[i].isIdentity) continue;

                                short type = record.dataList[i].type;
                                if (!(type == constValuesTracing.CONST_COL_TYPE_BLOB || type == constValuesTracing.CONST_COL_TYPE_CLOB || type == constValuesTracing.CONST_COL_TYPE_NCLOB ||
                                      type == constValuesTracing.CONST_COL_TYPE_BFILE || type == constValuesTracing.CONST_COL_TYPE_RAW || type == constValuesTracing.CONST_COL_TYPE_LONGRAW ||
                                      type == constValuesTracing.CONST_COL_TYPE_LONG_FETCH || type == constValuesTracing.CONST_COL_TYPE_BLOB_FETCH || type == constValuesTracing.CONST_COL_TYPE_CLOB_FETCH ||
                                      type == constValuesTracing.CONST_COL_TYPE_NCLOB_FETCH || type == constValuesTracing.CONST_COL_TYPE_LONGRAW_FETCH))
                                {
                                    // lob 아닌 데이터는 value 값 미존재하면 null 값
                                    if (record.dataList[i].str != null && record.dataList[i].str.Length > 0)
                                    {
                                        if (colNames.Length == 0)
                                            colNames += metaDefList[j].colname;
                                        else
                                            colNames += "," + metaDefList[j].colname;

                                        metaDefNum = j;
                                        break;
                                    } else
                                    {
                                        isExist = false;
                                        break;
                                    }
                                }
                            }

                            if(j == (metaDefList.Count-1))
                            {
                                isExist = false;
                            }
                        }
                    } else
                    {
                        isExist = false;
                    }

                    if(isExist)
                    {
                        if (constValues.CONST_DBTYPE_USING.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                            metaDefList[metaDefNum].value = "@" + metaDefList[metaDefNum].colname + "_" + record.dataList[i].flag + "{0}";
                        else if (constValues.CONST_DBTYPE_USING.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                            metaDefList[metaDefNum].value = ":" + metaDefList[metaDefNum].colname + "_" + record.dataList[i].flag;

                        ConstStruct.ParameterOrderInfo item = new ConstStruct.ParameterOrderInfo();
                        item.schema = record.header.schema;
                        item.object_n = record.header.object_n;
                        item.colno = metaDefList[metaDefNum].colno;
                        item.colname = metaDefList[metaDefNum].colname;
                        item.parameterValue = metaDefList[metaDefNum].value;
                        item.type = metaDefList[metaDefNum].definitionType;     // mapper 적용된 값
                        item.flag = record.dataList[i].flag;
                        item.metaColtype = metaDefList[metaDefNum].dataType;
                        parameterOrderInfoList.Add(item);
                    }
                }

                Console.WriteLine("metaDefList.Count :: " + metaDefList.Count);
                for(int i = 0; i < metaDefList.Count; i++)
                {
                    if (metaDefList[i].value != null)
                    {
                        if(colValues.Length == 0)
                        {
                            colValues += metaDefList[i].value;
                        }
                        else
                        {
                            colValues += "," + metaDefList[i].value;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] QueryFunc GetInsertColumnNameFromTracingRecord() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
            }
        }
        #endregion

        #region GetInsertValuesFromRecord() : insert column 값 가져옴
        public string GetInsertValuesFromRecord(Struct.TracingRecordDml record, List<ConstStruct.MetaDefInfo> metaDefList)
        {
            ConstValuesTracing constValuesTracing = new ConstValuesTracing();

            string colValues = "";
            try
            {
                for(int i = 0; i < record.dataList.Count; i++)
                {
                    bool isExist = true;
                    int metaDefNum = 0;

                    if (metaDefList.Count > 0)
                    {
                        for(int j = 0; j < metaDefList.Count; j++)
                        {
                            if(record.dataList[i].colno == metaDefList[j].colno)
                            {
                                
                            }
                        }
                    } else
                    {
                        isExist = false;
                    }
                }
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] QueryFunc GetInsertBulkValueFromTracingRecord() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
                return null;
            }

            return colValues;
        }
        #endregion

        #region GetParameterDataTable() : parameter 매핑에 사용할 data table 생성
        public DataTable GetParameterDataTable(List<ConstStruct.ParameterOrderInfo> parameterOrderInfoList, int count)
        {
            DataTable result = new DataTable();
            ConstValuesTracing constValuesTracing = new ConstValuesTracing();

            try
            {
                for (int i = 0; i < parameterOrderInfoList.Count; i++)
                {
                    if (parameterOrderInfoList[i].type == constValuesTracing.CONST_COL_TYPE_RAW || parameterOrderInfoList[i].type == constValuesTracing.CONST_COL_TYPE_LONGRAW ||
                        parameterOrderInfoList[i].type == constValuesTracing.CONST_COL_TYPE_LONGRAW_FETCH || parameterOrderInfoList[i].type == constValuesTracing.CONST_COL_TYPE_BLOB ||
                        parameterOrderInfoList[i].type == constValuesTracing.CONST_COL_TYPE_BLOB_FETCH)
                    {
                        string colname = parameterOrderInfoList[i].colname + "_" + parameterOrderInfoList[i].flag;
                        DataColumn column = new DataColumn(colname, typeof(byte[]));
                        result.Columns.Add(column);
                    }
                    else
                    {
                        string colname = parameterOrderInfoList[i].colname + "_" + parameterOrderInfoList[i].flag;
                        DataColumn column = new DataColumn(colname, System.Type.GetType("System.String"));
                        result.Columns.Add(column);
                    }
                }
                for (int i = 0; i < count; i++)
                {
                    DataRow row = result.NewRow();
                    result.Rows.Add(row.ItemArray);
                }
            }
            catch (Exception e)
            {
                //logMessage = "[System Exception] QueryFunc GetParameterDataTable() exception : " + ex.Message.ToString();
                //constFunc.WriteLog(constValues.CONST_LOGGER_TYPE_FATAL, logMessage, constValues.CONST_LOGGER_POST, alias, config);
            }

            return result;
        }
        #endregion

        








        #region CheckConnState() : check db connection
        public bool CheckConnState()
        {
            bool result = true;
            ConstValues constValues = new ConstValues();

            try
            {
                ConnectionState state = new ConnectionState();
                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    state = executeOracle.orclConn.State;
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("== : " + executeSqlserver.sqlConn.State);
                    state = executeSqlserver.sqlConn.State;
                }

                if (state == ConnectionState.Broken || state == ConnectionState.Closed)
                {
                    result = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("checkConn : " + e.ToString());
                result = false;
            }

            return result;
        }
        #endregion

        #region IsExistTable() : check table exist
        public int IsExistTable(String schemaName, String tableName, Object conn)
        {
            int isExist = 1;
            ConstValues constValues = new ConstValues();

            if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
            {
                OracleConnection orclConn = (OracleConnection)conn;
                using (OracleCommand cmd = orclConn.CreateCommand())
                {
                    cmd.CommandText = String.Format(@"SELECT count(*) FROM all_tables where table_name = '{0}'", tableName);
                    OracleDataReader rd = cmd.ExecuteReader();
                    rd.Read();
                    isExist = int.Parse(rd[0].ToString());
                }
            }
            else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
            {
                SqlConnection sqlConn = (SqlConnection)conn;
                using (SqlCommand cmd = sqlConn.CreateCommand())
                {
                    cmd.CommandText = String.Format("SELECT count(*) FROM INFORMATION_SCHEMA.tables WITH(NOLOCK) WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME = '{1}'", schemaName, tableName);
                    SqlDataReader rd = cmd.ExecuteReader();
                    rd.Read();
                    isExist = int.Parse(rd[0].ToString());
                    rd.Close();
                }
            }

            return isExist;
        }
        #endregion

        #region DisconnectDatabase() : db disconnection
        public void DisconnectDatabase(Object conn)
        {
            Console.WriteLine("database 종료");
            ConstValues constValues = new ConstValues();
            try
            {
                if (DB_TYPE.Equals(constValues.CONST_DBTYPE_ORACLE, StringComparison.OrdinalIgnoreCase))
                {
                    OracleConnection oracleconn = (OracleConnection)conn;
                    oracleconn.Close();
                }
                else if (DB_TYPE.Equals(constValues.CONST_DBTYPE_MSSQL, StringComparison.OrdinalIgnoreCase))
                {
                    SqlConnection sqlconn = (SqlConnection)conn;
                    sqlconn.Close();
                }
            } catch(Exception e)
            {
                Console.WriteLine("disconn : " + e.ToString());
            }
        }
        #endregion
    }
}
