You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.8 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Data;
  8. using System.Data.Sql;
  9. using System.Data.SqlClient;
  10. using System.Configuration;
  11. using Oracle.ManagedDataAccess.Client;
  12. namespace ManagementSystem.Utility
  13. {
  14. public static class OracleUtility
  15. {
  16. #region 取得指定Table資料
  17. public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList , string strWhere, string strOrderBy, OracleConnection sqlConn)
  18. {
  19. DataSet dsData = new DataSet();
  20. string strCommand = "";
  21. try
  22. {
  23. string strColumns = "";
  24. foreach (string strColumn in strColumnList) //取得欄位陣列
  25. {
  26. if (strColumns != "")
  27. {
  28. strColumns += "," + strColumn.Trim();
  29. }
  30. else
  31. {
  32. strColumns = strColumn;
  33. }
  34. }
  35. if (intTopCount > 0)
  36. {
  37. strCommand = string.Format("Select {1} From {2} FETCH FIRST {0} ROWS ONLY", intTopCount.ToString(), strColumns, strTableName);
  38. }
  39. else
  40. {
  41. strCommand = string.Format("Select {0} From {1} ", strColumns, strTableName);
  42. }
  43. if (strWhere != "") //設定過濾條件
  44. {
  45. strCommand += " Where " + strWhere;
  46. }
  47. if (strOrderBy != "") //取得排序欄位
  48. {
  49. strCommand += " Order By " + strOrderBy;
  50. }
  51. using (OracleDataAdapter sqlAdapter = new OracleDataAdapter(strCommand, sqlConn))
  52. {
  53. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  54. {
  55. sqlConn.Open();
  56. }
  57. sqlAdapter.Fill(dsData, "Result");
  58. }
  59. if (dsData.Tables.Count != 0)
  60. {
  61. return dsData.Tables["Result"];
  62. }
  63. else
  64. {
  65. return null;
  66. }
  67. }
  68. catch (Exception ex)
  69. {
  70. return null;
  71. throw ex;
  72. }
  73. }
  74. public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, string strWhere, OracleConnection sqlConn)
  75. {
  76. return GetTable(strTableName, intTopCount ,strColumnList, strWhere, "", sqlConn);
  77. }
  78. public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList, OracleConnection sqlConn)
  79. {
  80. return GetTable(strTableName, intTopCount, strColumnList,"","", sqlConn);
  81. }
  82. public static DataTable GetTable(string strTableName,int intTopCount, OracleConnection sqlConn)
  83. {
  84. return GetTable(strTableName, intTopCount, new string[] {"*"}, "", "", sqlConn);
  85. }
  86. public static DataTable GetTable(string strTableName, OracleConnection sqlConn)
  87. {
  88. return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn);
  89. }
  90. #endregion
  91. #region Oracle命令相關程式集
  92. public static OracleConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort)
  93. {
  94. try
  95. {
  96. string strConnectionString = "Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = " + strIP + ")(PORT = " + strPort + ")))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = " + strDBName + "))); User Id = " + strID + "; Password = " + strPWD + ";";
  97. OracleConnection sqlConn = new OracleConnection(strConnectionString);
  98. sqlConn.ConnectionString = strConnectionString;
  99. sqlConn.Open();
  100. return sqlConn;
  101. }
  102. catch (Exception ex)
  103. {
  104. ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.GetConn", ex);
  105. return null;
  106. }
  107. }
  108. public static DataTable GetSQLResult(string strSQL, OracleConnection sqlConn)
  109. {
  110. DataSet dsData = new DataSet();
  111. try
  112. {
  113. using (OracleDataAdapter sqlAdapter = new OracleDataAdapter(strSQL, sqlConn))
  114. {
  115. if (sqlConn == null)
  116. return null;
  117. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  118. {
  119. sqlConn.Open();
  120. }
  121. sqlAdapter.Fill(dsData, "Result");
  122. }
  123. if (dsData.Tables.Count != 0)
  124. {
  125. return dsData.Tables["Result"];
  126. }
  127. else
  128. {
  129. return null;
  130. }
  131. }
  132. catch(Exception ex)
  133. {
  134. ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.GetSQLResult", ex);
  135. throw ex;
  136. }
  137. }
  138. public static bool IsExist(string strSQL, OracleConnection sqlConn)
  139. {
  140. try
  141. {
  142. DataTable dtResult = GetSQLResult(strSQL, sqlConn);
  143. if (dtResult.Rows.Count > 0)
  144. { return true; }
  145. else
  146. { return false; }
  147. }
  148. catch (Exception ex)
  149. {
  150. throw ex;
  151. }
  152. }
  153. public static bool RunSQLNonReturn(string strSQL, OracleConnection sqlConn)
  154. {
  155. try
  156. {
  157. OracleCommand sqlCmd = new OracleCommand(strSQL, sqlConn);
  158. if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  159. {
  160. sqlConn.Open();
  161. }
  162. sqlCmd.ExecuteNonQuery();
  163. return true;
  164. }
  165. catch (Exception ex)
  166. {
  167. ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.RunSQLNonReturn", ex);
  168. return false;
  169. }
  170. }
  171. public static string GetSQLCount(string strSQL, OracleConnection sqlConn)
  172. {
  173. string strCount = "";
  174. DataTable dtTemp = GetSQLResult(strSQL, sqlConn);
  175. strCount = dtTemp.Rows.Count.ToString();
  176. //回傳查詢結果
  177. if(strCount == "")
  178. { return "0"; }
  179. else
  180. { return strCount; }
  181. }
  182. #endregion
  183. }
  184. }