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.

333 lines
10 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 SQLUtility
  15. {
  16. #region 取得指定Table資料
  17. public static DataSet GetTable(string strTableName, string[] strColumnList , string strWhere, string strOrderBy)
  18. {
  19. DataSet dsData = new DataSet();
  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. string strCommand = string.Format("Select {0} From {1} ",strColumns, strTableName);
  35. if (strWhere != "") //設定過濾條件
  36. {
  37. strCommand += " Where " + strWhere;
  38. }
  39. if (strOrderBy != "") //取得排序欄位
  40. {
  41. strCommand += " Order By " + strOrderBy;
  42. }
  43. //using (SqlConnection sqlConn = GetConn(MainForm.strAccountingBookID))
  44. //{
  45. // using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strCommand, sqlConn))
  46. // {
  47. // if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  48. // {
  49. // sqlConn.Open();
  50. // }
  51. // sqlAdapter.Fill(dsData, "Result");
  52. // }
  53. //}
  54. return dsData;
  55. }
  56. catch (Exception ex)
  57. {
  58. throw ex;
  59. }
  60. }
  61. public static DataSet GetTable(string strTableName, string[] strColumnList, string strWhere)
  62. {
  63. return GetTable(strTableName, strColumnList, strWhere, "");
  64. }
  65. public static DataSet GetTable(string strTableName, string[] strColumnList)
  66. {
  67. return GetTable(strTableName, strColumnList,"","");
  68. }
  69. public static DataSet GetTable(string strTableName)
  70. {
  71. return GetTable(strTableName,new string[] {"*"}, "", "");
  72. }
  73. #endregion
  74. #region MS-SQL命令相關程式集
  75. //public static SqlConnection GetConn(string strOrg)
  76. //{
  77. // string strConn = "";
  78. // SqlConnection sqlConn = null;
  79. // switch(strOrg)
  80. // {
  81. // case "":
  82. // strConn = System.Configuration.ConfigurationManager.ConnectionStrings["OrigtekSQLConn"].ToString();
  83. // sqlConn = new SqlConnection(strConn);
  84. // break;
  85. // case "Origtek":
  86. // strConn = System.Configuration.ConfigurationManager.ConnectionStrings["OrigtekSQLConn"].ToString();
  87. // sqlConn = new SqlConnection(strConn);
  88. // break;
  89. // case "OrigtekEnergy":
  90. // strConn = System.Configuration.ConfigurationManager.ConnectionStrings["OEnergySQLConn"].ToString();
  91. // sqlConn = new SqlConnection(strConn);
  92. // break;
  93. // }
  94. // return sqlConn;
  95. //}
  96. //public static SqlConnection GetConn()
  97. //{
  98. // return GetConn("");
  99. //}
  100. public static DataSet GetSQLResult(string strSQL)
  101. {
  102. DataSet dsData = new DataSet();
  103. try
  104. {
  105. //using (SqlConnection sqlConn = GetConn(MainForm.strAccountingBookID))
  106. //{
  107. // using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSQL, sqlConn))
  108. // {
  109. // if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  110. // {
  111. // sqlConn.Open();
  112. // }
  113. // sqlAdapter.Fill(dsData, "Result");
  114. // }
  115. //}
  116. return dsData;
  117. }
  118. catch(Exception ex)
  119. {
  120. throw ex;
  121. }
  122. }
  123. public static bool IsExist(string strSQL)
  124. {
  125. try
  126. {
  127. DataSet sdResult = GetSQLResult(strSQL);
  128. DataTable dtResult = sdResult.Tables[0];
  129. if (dtResult.Rows.Count > 0)
  130. { return true; }
  131. else
  132. { return false; }
  133. }
  134. catch (Exception ex)
  135. {
  136. throw ex;
  137. }
  138. }
  139. public static bool RunSQLNonReturn(string strSQL)
  140. {
  141. try
  142. {
  143. //SqlConnection sqlConn = GetConn(MainForm.strAccountingBookID);
  144. //SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
  145. //if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
  146. //{
  147. // sqlConn.Open();
  148. //}
  149. //sqlCmd.ExecuteNonQuery();
  150. return true;
  151. }
  152. catch (Exception ex)
  153. {
  154. return false;
  155. }
  156. }
  157. public static string GetSQLCount(string strSQL)
  158. {
  159. string strCount = "";
  160. DataSet dsTemp = GetSQLResult(strSQL);
  161. strCount = dsTemp.Tables[0].Rows.Count.ToString();
  162. //回傳查詢結果
  163. if(strCount == "")
  164. { return "0"; }
  165. else
  166. { return strCount; }
  167. }
  168. #endregion
  169. #region 其它共用程式
  170. public static bool IsNumber(string strSource)
  171. {
  172. float output;
  173. return float.TryParse(strSource, out output);
  174. } //確認是否為數字
  175. public static bool IsDate(string strSource)
  176. {
  177. try
  178. {
  179. //System.Globalization.DateTimeFormatInfo dtInfo = new System.Globalization.DateTimeFormatInfo();
  180. //dtInfo.FullDateTimePattern = "yyyy/MM/dd";
  181. //DateTime dtOutput = DateTime.ParseExact(strSource, "F", dtInfo);
  182. DateTime dtOutput;
  183. if (DateTime.TryParse(strSource, out dtOutput))
  184. {
  185. return true;
  186. }
  187. else
  188. {
  189. return false;
  190. }
  191. }
  192. catch(Exception ex)
  193. {
  194. return false;
  195. }
  196. }
  197. public static string MarkNumber(double dbNumber,int intPoint) //將資料加上三位一撇
  198. {
  199. return string.Format("{0:N" + intPoint + "}", dbNumber);
  200. }
  201. public static string MarkNumber(int intNumber, int intPoint) //將資料加上三位一撇
  202. {
  203. return string.Format("{0:N" + intPoint + "}", intNumber);
  204. }
  205. public static string MarkNumber(string strNumber, int intPoint) //將資料加上三位一撇
  206. {
  207. if (strNumber.IndexOf("%") != -1)
  208. {
  209. return strNumber;
  210. }
  211. if (!string.IsNullOrEmpty(strNumber))
  212. {
  213. if (IsNumber(strNumber))
  214. {
  215. return MarkNumber(Convert.ToDouble(strNumber), intPoint);
  216. }
  217. }
  218. return "";
  219. }
  220. public static string MarkNumber(double dbNumber) //將資料加上三位一撇
  221. {
  222. return string.Format("{0:N0}", dbNumber);
  223. }
  224. public static string MarkNumber(int intNumber) //將資料加上三位一撇
  225. {
  226. return string.Format("{0:N0}", intNumber);
  227. }
  228. public static string MarkNumber(string strNumber) //將資料加上三位一撇
  229. {
  230. if (!string.IsNullOrEmpty(strNumber))
  231. {
  232. if (strNumber.IndexOf("%") != -1)
  233. {
  234. return strNumber;
  235. }
  236. if (IsNumber(strNumber))
  237. {
  238. return MarkNumber(Math.Round(Convert.ToDouble(strNumber)));
  239. }
  240. }
  241. return "";
  242. }
  243. public static void CreateDir(string strDirPath) //建立資料夾
  244. {
  245. string[] strDir = strDirPath.Split('\\');
  246. string strCreatePath = "";
  247. foreach (string strPath in strDir)
  248. {
  249. if(strPath != "")
  250. {
  251. strCreatePath += strPath + "\\";
  252. if (!Directory.Exists(strCreatePath))
  253. {
  254. Directory.CreateDirectory(strCreatePath);
  255. }
  256. }
  257. }
  258. }
  259. public static void WriteFile(string strFilePath ,string strContent) //撰寫檔案文字內容
  260. {
  261. using (StreamWriter swFile = new StreamWriter(strFilePath, true))
  262. {
  263. swFile.WriteLine(strContent);
  264. }
  265. }
  266. public static void WriteFile(string strFilePath, string[] strContent) //撰寫檔案文字內容
  267. {
  268. //判斷檔案是否存在
  269. string[] strPathArray = strFilePath.Split('\\');
  270. string strCHKPath = "";
  271. for (int intPath = 0; intPath < strPathArray.Length - 1; intPath ++ )
  272. {
  273. strCHKPath += strPathArray[intPath] + "\\";
  274. }
  275. //檢查資料夾是否存在
  276. if (!File.Exists(strCHKPath))
  277. {
  278. CreateDir(strCHKPath);
  279. }
  280. using (StreamWriter swFile = new StreamWriter(strFilePath, true))
  281. {
  282. foreach(string strContentText in strContent)
  283. {
  284. swFile.WriteLine(strContentText);
  285. }
  286. }
  287. }
  288. #endregion
  289. }
  290. }