DacBase.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 FPER
  9. {
  10. public class DacBase
  11. {
  12. public static int iOpenCount = 0;
  13. public static int iAccessCount = 0;
  14. //String strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
  15. // "127.0.0.1", // IP ex)192.168.63.22
  16. // "sysdba", // 사용자
  17. // "masterkey", // 암호
  18. // "D:\\타기작업실\\아이콘트롤스\\DB\\FPER.FDB", // 절대위치.. ex) C:\TEST\TEST.FDB
  19. // "KSC_5601", // KSC_5601 같은 문자셋을 지정
  20. // "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
  21. public FbConnection connection = null;
  22. public int Receiver_ID = 0; // cyim 2015.7.30 데이타베이스 접속 루틴 변경
  23. // cyim 2015.7.29 수신반 루틴을 위해 외부 파라미터로 동작하도록 수정 : 생성자 추가
  24. public DacBase(int ID)
  25. {
  26. Receiver_ID = ID;
  27. }
  28. ~DacBase()
  29. {
  30. }
  31. // cyim 2015.7.29 수신반 루틴을 위해 외부 파라미터로 동작하도록 수정
  32. // 아이디와 패스워드는 고정, 모드에 따라 아이피, 경로가 틀리다
  33. public bool Connect()
  34. {
  35. bool ret = false;
  36. try
  37. {
  38. string DB_IP = null;
  39. string DB_PATH = null;
  40. // 수신반이 아니라면 레지스트리값 이용
  41. if (_Data.FireDeskMode == false)
  42. {
  43. //레지스트리에서 DB접속정보 읽어오기
  44. // RegistryKey rk = Registry.LocalMachine.OpenSubKey("SOFTWARE\\I_FPER_COMM_DAEMON", false);
  45. // String SERVER_NAME = "";
  46. // String DATABASE_NAME = "";
  47. // String DATABASE_USER_ID = "";
  48. // String DATABASE_PASSWORD = "";
  49. // if (rk != null)
  50. // {
  51. // DATABASE_NAME = rk.GetValue("DATABASE_NAME").ToString();
  52. // SERVER_NAME = DATABASE_NAME.Substring(0, DATABASE_NAME.IndexOf(":"));
  53. // DATABASE_NAME = DATABASE_NAME.Substring(DATABASE_NAME.IndexOf(":") + 1);
  54. // DATABASE_USER_ID = rk.GetValue("DATABASE_USER_ID").ToString();
  55. // DATABASE_PASSWORD = rk.GetValue("DATABASE_PASSWORD").ToString();
  56. DB_IP = _Data.Registry_DATABASE_NAME_IP;
  57. DB_PATH = _Data.Registry_DATABASE_NAME_PATH;
  58. }
  59. // 수신반인 경우에는 ini 파일을 이용
  60. else
  61. {
  62. // 단, 수신기 아이디가 0이라면 수신반 DB 를 이용하는 것이고, ini 파일을 이용해서 경로를 파악한다
  63. if (Receiver_ID == 0)
  64. {
  65. DB_IP = _Data.Registry_DATABASE_NAME_IP;
  66. DB_PATH = _Data.Registry_DATABASE_NAME_PATH;
  67. }
  68. else
  69. {
  70. _FireDesk_Receiver FireDesk_Receiver = (_FireDesk_Receiver)_Data.Hash_Receiver[Receiver_ID.ToString()];
  71. DB_IP = FireDesk_Receiver.DATABASE_NAME_IP;
  72. DB_PATH = FireDesk_Receiver.DATABASE_NAME_PATH;
  73. }
  74. }
  75. // 사용자와 암호는 고정시킨다
  76. String strConnection = string.Format("Server={0};User={1};Password={2};Database={3};Charset={4};ServerType={5}",
  77. DB_IP, // IP ex)192.168.63.22
  78. "SYSDBA", // 사용자
  79. "masterkey", // 암호
  80. DB_PATH, // 절대위치.. ex) C:\TEST\TEST.FDB
  81. "KSC_5601", // KSC_5601 같은 문자셋을 지정
  82. "0"); // 0이면 Classic/Super Server, 1이면 Embedded Server를 지정하신다음...
  83. connection = new FbConnection(strConnection);
  84. //connection.ConnectionTimeout = 2147483647;
  85. //connection.Database.
  86. connection.Open();
  87. iOpenCount++;
  88. iAccessCount++;
  89. if (iAccessCount > 100000)
  90. {
  91. iAccessCount = 0;
  92. }
  93. ret = (this.connection.State == ConnectionState.Open);
  94. }
  95. catch (Exception ex)
  96. {
  97. Util.UErrorMessage(ex, 0, 0);
  98. }
  99. return ret;
  100. }
  101. public void DisConnect()
  102. {
  103. try
  104. {
  105. if (connection != null)
  106. connection.Close();
  107. iOpenCount--;
  108. }
  109. catch (Exception ex)
  110. {
  111. Util.UErrorMessage(ex, 0, 0);
  112. }
  113. ////Util.UDebugMessage("-DB DisConnect ", 0, 0);
  114. // cyim_debug
  115. //Debug.WriteLine(">>>>>> Close >>>>> " + Convert.ToString(iOpenCount));
  116. }
  117. public DataSet Select(String sqlSyntax)
  118. {
  119. DataSet data = null;
  120. if (this.Connect())
  121. {
  122. try
  123. {
  124. //Util.UDebugMessage(string.Format("+DB Select {0}", sqlSyntax), 0, 0);
  125. //FbTransaction transaction = connection.BeginTransaction();
  126. //FbCommand command = new FbCommand(sqlSyntax, connection, transaction);
  127. FbCommand command = new FbCommand(sqlSyntax, connection);
  128. command.CommandType = CommandType.Text;
  129. command.CommandTimeout = 2147483647;
  130. data = new DataSet();
  131. FbDataAdapter adapter = new FbDataAdapter(command);
  132. adapter.Fill(data);
  133. //transaction.Rollback();
  134. adapter.Dispose();
  135. command.Dispose();
  136. }
  137. catch (Exception ex)
  138. {
  139. Util.UErrorMessage(ex, 0, 0);
  140. throw ex;
  141. }
  142. finally
  143. {
  144. //Util.UDebugMessage(string.Format("-DB Select"), 0, 0);
  145. this.DisConnect();
  146. }
  147. }
  148. return data;
  149. }
  150. public void ExecuteNonQuery(String sqlSyntax)
  151. {
  152. if (this.Connect())
  153. {
  154. FbTransaction transaction = connection.BeginTransaction();
  155. try
  156. {
  157. //Util.UDebugMessage(string.Format("+DB Excute {0}", sqlSyntax), 0, 0);
  158. FbCommand command = new FbCommand(sqlSyntax, connection, transaction);
  159. command.CommandType = CommandType.Text;
  160. command.CommandTimeout = 2147483647;
  161. //command.CommandType = CommandType.StoredProcedure;
  162. //command.Parameters.Add(New SqlParameter("@Cmd", CMD))
  163. command.ExecuteNonQuery();
  164. transaction.Commit();
  165. command.Dispose();
  166. }
  167. catch (Exception ex)
  168. {
  169. Util.UErrorMessage(ex, 0, 0);
  170. transaction.Rollback();
  171. throw ex;
  172. }
  173. finally
  174. {
  175. this.DisConnect();
  176. //Util.UDebugMessage(string.Format("-DB Excute "), 0, 0);
  177. }
  178. }
  179. }
  180. }
  181. }