123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using FirebirdSql.Data.FirebirdClient;
- using Microsoft.Win32;
- using System.Data;
- using System.Diagnostics;
- namespace IControls_FireManager
- {
- //DATABASE 작업 모드
- public enum QUERYMode
- {
- select, insert, update, delete, InsertAndUpdate
- }
- //TB_COMM의 COMM_LOOP 필드 상수
- public enum COMMLOOP
- {
- FrontLoop = 1, BackLoop = 2, IO = 3, KEYPAD = 4, EmergencyBroadcast = 5
- }
-
- public class DacBase
- {
- //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를 지정하신다음...
- // FireBird 연결자
- public FbConnection connection = null;
- public _DB_Connect DBSetting = null;
- public bool DBSet(_DB_Connect cDB)
- {
- DBSetting = new _DB_Connect();
- DBSetting.sDATABASE_NAME = cDB.sDATABASE_NAME;
- DBSetting.sPASSWORD = cDB.sPASSWORD;
- DBSetting.sUSER_ID = cDB.sUSER_ID;
- DBSetting.sSERVER_NAME = cDB.sSERVER_NAME;
- return true;
- }
- public bool Connect()
- {
- bool ret = false;
- string strConnection;
- try
- {
- if (DBSetting != null) // DBSetting 설정이 된 경우..
- {
- strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
- DBSetting.sSERVER_NAME, // IP ex)192.168.63.22
- DBSetting.sUSER_ID, // 사용자
- DBSetting.sPASSWORD, // 암호
- DBSetting.sDATABASE_NAME, // 절대위치.. ex) C:\TEST\TEST.FDB
- "KSC_5601", // KSC_5601 같은 문자셋을 지정
- "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
- }
- else
- {
- //레지스트리에서 DB접속정보 읽어오기
- RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FireManager", 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();
- }
- strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
- SERVER_NAME, // IP ex)192.168.63.22
- DATABASE_USER_ID, // 사용자
- DATABASE_PASSWORD, // 암호
- DATABASE_NAME, // 절대위치.. ex) C:\TEST\TEST.FDB
- "KSC_5601", // KSC_5601 같은 문자셋을 지정
- "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
- }
- connection = new FbConnection(strConnection);
-
- //connection.Database.
- connection.Open();
- ret = (this.connection.State == ConnectionState.Open);
- }
- catch (Exception ex)
- {
- Util.UErrorMessage(ex, 0, 0);
- throw new Exception(string.Format("[{0}]\r\n{1}", ex.Message, ex.StackTrace));
- }
- return ret;
- }
- public void DisConnect() {
- try {
- if (connection != null)
- connection.Close();
- }
- catch (Exception ex) {
- Util.UErrorMessage(ex, 0, 0);
- }
- //Util.UDebugMessage("-DB DisConnect ", 0, 0);
- //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);
- FbCommand command = new FbCommand(sqlSyntax, connection);
- command.CommandType = CommandType.Text;
- command.CommandTimeout = 2147483647;
- data = new DataSet();
- FbDataAdapter adapter = new FbDataAdapter(command);
- adapter.Fill(data);
- 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.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);
- }
- }
- }
- }
- }
|