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.

204 lines
6.4 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 Npgsql;
  12. namespace ManagementSystem.Utility
  13. {
  14. public static class PostgreSQLUtility
  15. {
  16. #region 取得指定Table資料
  17. public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, string strWhere, string strOrderBy, NpgsqlConnection sqlConn)
  18. {
  19. DataSet dsData = new DataSet();
  20. try
  21. {
  22. string strColumns = "";
  23. string strCommand = "";
  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}\" LIMIT {0}", 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 (NpgsqlDataAdapter sqlAdapter = new NpgsqlDataAdapter(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, NpgsqlConnection sqlConn)
  75. {
  76. return GetTable(strTableName, intTopCount, strColumnList, strWhere, "", sqlConn);
  77. }
  78. public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, NpgsqlConnection sqlConn)
  79. {
  80. return GetTable(strTableName, intTopCount, strColumnList, "", "", sqlConn);
  81. }
  82. public static DataTable GetTable(string strTableName, int intTopCount, NpgsqlConnection sqlConn)
  83. {
  84. return GetTable(strTableName, intTopCount, new string[] { "*" }, "", "", sqlConn);
  85. }
  86. public static DataTable GetTable(string strTableName, NpgsqlConnection sqlConn)
  87. {
  88. return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn);
  89. }
  90. #endregion
  91. #region MS-SQL命令相關程式集
  92. public static NpgsqlConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort)
  93. {
  94. try
  95. {
  96. string strConnectionString = "Host=" + strIP + ";Port="+ strPort + ";Database=" + strDBName + ";Username=" + strID + ";Password=" + strPWD;
  97. NpgsqlConnection sqlConn = new NpgsqlConnection(strConnectionString);
  98. sqlConn.ConnectionString = strConnectionString;
  99. sqlConn.Open();
  100. return sqlConn;
  101. }
  102. catch (Exception ex)
  103. {
  104. ErrorHandler.WriteErrorLog("PostgreSQLUtilityClass.cs", ex);
  105. return null;
  106. }
  107. }
  108. public static DataTable GetSQLResult(string strSQL, NpgsqlConnection sqlConn)
  109. {
  110. DataSet dsData = new DataSet();
  111. try
  112. {
  113. using (NpgsqlDataAdapter sqlAdapter = new NpgsqlDataAdapter(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. return null;
  135. throw ex;
  136. }
  137. }
  138. public static bool IsExist(string strSQL, NpgsqlConnection 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, NpgsqlConnection sqlConn)
  154. {
  155. try
  156. {
  157. NpgsqlCommand sqlCmd = new NpgsqlCommand(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. return false;
  168. }
  169. }
  170. public static string GetSQLCount(string strSQL, NpgsqlConnection sqlConn)
  171. {
  172. string strCount = "";
  173. DataTable dtTemp = GetSQLResult(strSQL, sqlConn);
  174. strCount = dtTemp.Rows.Count.ToString();
  175. //回傳查詢結果
  176. if (strCount == "")
  177. { return "0"; }
  178. else
  179. { return strCount; }
  180. }
  181. #endregion
  182. }
  183. }