98ef76daa71e39d764be85110464f193bf1e9c7d.svn-base 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using FirebirdSql.Data.FirebirdClient;
  5. using Microsoft.Win32;
  6. using System.Data;
  7. using System.Diagnostics;
  8. namespace IControls_FireManager
  9. {
  10. //DATABASE 작업 모드
  11. public enum QUERYMode
  12. {
  13. select, insert, update, delete, InsertAndUpdate
  14. }
  15. //TB_COMM의 COMM_LOOP 필드 상수
  16. public enum COMMLOOP
  17. {
  18. FrontLoop = 1, BackLoop = 2, IO = 3, KEYPAD = 4, EmergencyBroadcast = 5
  19. }
  20. public class DacBase
  21. {
  22. //String strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
  23. // "127.0.0.1", // IP ex)192.168.63.22
  24. // "sysdba", // 사용자
  25. // "masterkey", // 암호
  26. // "D:\\타기작업실\\아이콘트롤스\\DB\\FPER.FDB", // 절대위치.. ex) C:\TEST\TEST.FDB
  27. // "KSC_5601", // KSC_5601 같은 문자셋을 지정
  28. // "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
  29. // FireBird 연결자
  30. public FbConnection connection = null;
  31. public _DB_Connect DBSetting = null;
  32. public bool DBSet(_DB_Connect cDB)
  33. {
  34. DBSetting = new _DB_Connect();
  35. DBSetting.sDATABASE_NAME = cDB.sDATABASE_NAME;
  36. DBSetting.sPASSWORD = cDB.sPASSWORD;
  37. DBSetting.sUSER_ID = cDB.sUSER_ID;
  38. DBSetting.sSERVER_NAME = cDB.sSERVER_NAME;
  39. return true;
  40. }
  41. public bool Connect()
  42. {
  43. bool ret = false;
  44. string strConnection;
  45. try
  46. {
  47. if (DBSetting != null) // DBSetting 설정이 된 경우..
  48. {
  49. strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
  50. DBSetting.sSERVER_NAME, // IP ex)192.168.63.22
  51. DBSetting.sUSER_ID, // 사용자
  52. DBSetting.sPASSWORD, // 암호
  53. DBSetting.sDATABASE_NAME, // 절대위치.. ex) C:\TEST\TEST.FDB
  54. "KSC_5601", // KSC_5601 같은 문자셋을 지정
  55. "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
  56. }
  57. else
  58. {
  59. //레지스트리에서 DB접속정보 읽어오기
  60. RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FireManager", false);
  61. String SERVER_NAME = "";
  62. String DATABASE_NAME = "";
  63. String DATABASE_USER_ID = "";
  64. String DATABASE_PASSWORD = "";
  65. if (rk != null)
  66. {
  67. DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString();
  68. SERVER_NAME = DATABASE_NAME.Substring(0, DATABASE_NAME.IndexOf(":"));
  69. DATABASE_NAME = DATABASE_NAME.Substring(DATABASE_NAME.IndexOf(":") + 1);
  70. DATABASE_USER_ID = rk.GetValue("DATABASE_USER_ID").ToString();
  71. DATABASE_PASSWORD = rk.GetValue("DATABASE_PASSWORD").ToString();
  72. }
  73. strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
  74. SERVER_NAME, // IP ex)192.168.63.22
  75. DATABASE_USER_ID, // 사용자
  76. DATABASE_PASSWORD, // 암호
  77. DATABASE_NAME, // 절대위치.. ex) C:\TEST\TEST.FDB
  78. "KSC_5601", // KSC_5601 같은 문자셋을 지정
  79. "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
  80. }
  81. connection = new FbConnection(strConnection);
  82. //connection.Database.
  83. connection.Open();
  84. ret = (this.connection.State == ConnectionState.Open);
  85. }
  86. catch (Exception ex)
  87. {
  88. Util.UErrorMessage(ex, 0, 0);
  89. throw new Exception(string.Format("[{0}]\r\n{1}", ex.Message, ex.StackTrace));
  90. }
  91. return ret;
  92. }
  93. public void DisConnect() {
  94. try {
  95. if (connection != null)
  96. connection.Close();
  97. }
  98. catch (Exception ex) {
  99. Util.UErrorMessage(ex, 0, 0);
  100. }
  101. //Util.UDebugMessage("-DB DisConnect ", 0, 0);
  102. //Debug.WriteLine(">>>>>> Close >>>>> " + Convert.ToString(iOpenCount));
  103. }
  104. public DataSet Select(String sqlSyntax) {
  105. DataSet data = null;
  106. if (this.Connect()) {
  107. try {
  108. Util.UDebugMessage(string.Format("+DB Select {0}", sqlSyntax), 0, 0);
  109. FbCommand command = new FbCommand(sqlSyntax, connection);
  110. command.CommandType = CommandType.Text;
  111. command.CommandTimeout = 2147483647;
  112. data = new DataSet();
  113. FbDataAdapter adapter = new FbDataAdapter(command);
  114. adapter.Fill(data);
  115. adapter.Dispose();
  116. command.Dispose();
  117. }
  118. catch (Exception ex) {
  119. Util.UErrorMessage(ex, 0, 0);
  120. throw ex;
  121. }
  122. finally {
  123. Util.UDebugMessage(string.Format("-DB Select"), 0, 0);
  124. this.DisConnect();
  125. }
  126. }
  127. return data;
  128. }
  129. public void ExecuteNonQuery(String sqlSyntax) {
  130. if (this.Connect()) {
  131. FbTransaction transaction = connection.BeginTransaction();
  132. try {
  133. Util.UDebugMessage(string.Format("+DB Excute {0}", sqlSyntax), 0, 0);
  134. FbCommand command = new FbCommand(sqlSyntax, connection, transaction);
  135. command.CommandType = CommandType.Text;
  136. command.CommandTimeout = 2147483647;
  137. command.ExecuteNonQuery();
  138. transaction.Commit();
  139. command.Dispose();
  140. }
  141. catch (Exception ex) {
  142. Util.UErrorMessage(ex, 0, 0);
  143. transaction.Rollback();
  144. throw ex;
  145. }
  146. finally {
  147. this.DisConnect();
  148. Util.UDebugMessage(string.Format("-DB Excute "), 0, 0);
  149. }
  150. }
  151. }
  152. }
  153. }