123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using FirebirdSql.Data.FirebirdClient;
- using Microsoft.Win32;
- using System.Data;
- using System.Diagnostics;
- namespace FPER
- {
- public class DacBase
- {
- public static int iOpenCount = 0;
- public static int iAccessCount = 0;
- //String strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
- // "127.0.0.1", // IP ex)192.168.63.22
- // "sysdba", // 사용자
- // "masterkey", // 암호
- // "D:\\타기작업실\\아이콘트롤스\\DB\\FPER.FDB", // 절대위치.. ex) C:\TEST\TEST.FDB
- // "KSC_5601", // KSC_5601 같은 문자셋을 지정
- // "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
- public FbConnection connection = null;
- public int Receiver_ID = 0; // cyim 2015.7.30 데이타베이스 접속 루틴 변경
- // cyim 2015.7.29 수신반 루틴을 위해 외부 파라미터로 동작하도록 수정 : 생성자 추가
- public DacBase(int ID)
- {
- Receiver_ID = ID;
- }
- ~DacBase()
- {
-
- }
- // cyim 2015.7.29 수신반 루틴을 위해 외부 파라미터로 동작하도록 수정
- // 아이디와 패스워드는 고정, 모드에 따라 아이피, 경로가 틀리다
- public bool Connect()
- {
- bool ret = false;
- try
- {
- string DB_IP = null;
- string DB_PATH = null;
- // 수신반이 아니라면 레지스트리값 이용
- if (_Data.FireDeskMode == false)
- {
- //레지스트리에서 DB접속정보 읽어오기
- // RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FPER_COMM_DAEMON", false);
- // String SERVER_NAME = "";
- // String DATABASE_NAME = "";
- // String DATABASE_USER_ID = "";
- // String DATABASE_PASSWORD = "";
- // if (rk != null)
- // {
- // DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString();
- // SERVER_NAME = DATABASE_NAME.Substring(0, DATABASE_NAME.IndexOf(":"));
- // DATABASE_NAME = DATABASE_NAME.Substring(DATABASE_NAME.IndexOf(":") + 1);
- // DATABASE_USER_ID = rk.GetValue("DATABASE_USER_ID").ToString();
- // DATABASE_PASSWORD = rk.GetValue("DATABASE_PASSWORD").ToString();
- DB_IP = _Data.Registry_DATABASE_NAME_IP;
- DB_PATH = _Data.Registry_DATABASE_NAME_PATH;
- }
- // 수신반인 경우에는 ini 파일을 이용
- else
- {
- // 단, 수신기 아이디가 0이라면 수신반 DB 를 이용하는 것이고, ini 파일을 이용해서 경로를 파악한다
- if (Receiver_ID == 0)
- {
- DB_IP = _Data.Registry_DATABASE_NAME_IP;
- DB_PATH = _Data.Registry_DATABASE_NAME_PATH;
- }
- else
- {
- _FireDesk_Receiver FireDesk_Receiver = (_FireDesk_Receiver)_Data.Hash_Receiver[Receiver_ID.ToString()];
- DB_IP = FireDesk_Receiver.DATABASE_NAME_IP;
- DB_PATH = FireDesk_Receiver.DATABASE_NAME_PATH;
- }
- }
- // 사용자와 암호는 고정시킨다
- String strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
- DB_IP, // IP ex)192.168.63.22
- "SYSDBA", // 사용자
- "masterkey", // 암호
- DB_PATH, // 절대위치.. ex) C:\TEST\TEST.FDB
- "KSC_5601", // KSC_5601 같은 문자셋을 지정
- "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
- connection = new FbConnection(strConnection);
- //connection.ConnectionTimeout = 2147483647;
-
- //connection.Database.
- connection.Open();
- iOpenCount++;
- iAccessCount++;
- if (iAccessCount > 100000)
- {
- iAccessCount = 0;
- }
- ret = (this.connection.State == ConnectionState.Open);
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- }
- return ret;
- }
- public void DisConnect()
- {
- try
- {
- if (connection != null)
- connection.Close();
- iOpenCount--;
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- }
- ////Util.UDebugMessage("-DB DisConnect ", 0, 0);
- // cyim_debug
- //Debug.WriteLine(">>>>>> Close >>>>> " + Convert.ToString(iOpenCount));
- }
- public DataSet Select(String sqlSyntax)
- {
- DataSet data = null;
- if (this.Connect())
- {
- try
- {
- //Util.UDebugMessage(string.Format("+DB Select {0}", sqlSyntax), 0, 0);
- //FbTransaction transaction = connection.BeginTransaction();
- //FbCommand command = new FbCommand(sqlSyntax, connection, transaction);
- FbCommand command = new FbCommand(sqlSyntax, connection);
- command.CommandType = CommandType.Text;
- command.CommandTimeout = 2147483647;
- data = new DataSet();
- FbDataAdapter adapter = new FbDataAdapter(command);
- adapter.Fill(data);
- //transaction.Rollback();
- adapter.Dispose();
- command.Dispose();
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- throw ex;
- }
- finally
- {
- //Util.UDebugMessage(string.Format("-DB Select"), 0, 0);
- this.DisConnect();
- }
- }
- return data;
- }
- public void ExecuteNonQuery(String sqlSyntax)
- {
- if (this.Connect())
- {
- FbTransaction transaction = connection.BeginTransaction();
- try
- {
- //Util.UDebugMessage(string.Format("+DB Excute {0}", sqlSyntax), 0, 0);
- FbCommand command = new FbCommand(sqlSyntax, connection, transaction);
- command.CommandType = CommandType.Text;
- command.CommandTimeout = 2147483647;
- //command.CommandType = CommandType.StoredProcedure;
- //command.Parameters.Add(New SqlParameter("@Cmd", CMD))
- command.ExecuteNonQuery();
- transaction.Commit();
- command.Dispose();
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- transaction.Rollback();
- throw ex;
- }
- finally
- {
- this.DisConnect();
- //Util.UDebugMessage(string.Format("-DB Excute "), 0, 0);
- }
- }
- }
- }
- }
|