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.3 KiB

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