using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using System.Configuration; namespace ManagementSystem.Utility { public static class MSSQLUtility { #region 取得指定Table資料 public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList , string strWhere, string strOrderBy, SqlConnection sqlConn) { DataSet dsData = new DataSet(); string strCommand = ""; try { string strColumns = ""; foreach (string strColumn in strColumnList) //取得欄位陣列 { if (strColumns != "") { strColumns += "," + strColumn.Trim(); } else { strColumns = strColumn; } } if (intTopCount > 0) { strCommand = string.Format("Select Top {0} {1} From {2} ", intTopCount.ToString(), strColumns, strTableName); } else { strCommand = string.Format("Select {0} From {1} ", strColumns, strTableName); } if (strWhere != "") //設定過濾條件 { strCommand += " Where " + strWhere; } if (strOrderBy != "") //取得排序欄位 { strCommand += " Order By " + strOrderBy; } using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strCommand, sqlConn)) { if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.Fill(dsData, "Result"); } if (dsData.Tables.Count != 0) { return dsData.Tables["Result"]; } else { return null; } } catch (Exception ex) { return null; throw ex; } } public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, string strWhere, SqlConnection sqlConn) { return GetTable(strTableName, intTopCount ,strColumnList, strWhere, "", sqlConn); } public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList, SqlConnection sqlConn) { return GetTable(strTableName, intTopCount, strColumnList,"","", sqlConn); } public static DataTable GetTable(string strTableName,int intTopCount, SqlConnection sqlConn) { return GetTable(strTableName, intTopCount, new string[] {"*"}, "", "", sqlConn); } public static DataTable GetTable(string strTableName, SqlConnection sqlConn) { return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn); } #endregion #region MS-SQL命令相關程式集 public static SqlConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort) { try { string strConnectionString = "server=" + strIP + ";database=" + strDBName + ";uid=" + strID + ";pwd=" + strPWD; SqlConnection sqlConn = new SqlConnection(strConnectionString); sqlConn.ConnectionString = strConnectionString; sqlConn.Open(); return sqlConn; } catch (Exception ex) { ErrorHandler.WriteErrorLog("MSSQLUtilityClass.cs", ex); return null; } } public static DataTable GetSQLResult(string strSQL, SqlConnection sqlConn) { DataSet dsData = new DataSet(); try { using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSQL, sqlConn)) { if (sqlConn == null) return null; if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlAdapter.Fill(dsData, "Result"); } if (dsData.Tables.Count != 0) { return dsData.Tables["Result"]; } else { return null; } } catch(Exception ex) { return null; throw ex; } } public static bool IsExist(string strSQL, SqlConnection sqlConn) { try { DataTable dtResult = GetSQLResult(strSQL, sqlConn); if (dtResult.Rows.Count > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } } public static bool RunSQLNonReturn(string strSQL, SqlConnection sqlConn) { try { SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態 { sqlConn.Open(); } sqlCmd.ExecuteNonQuery(); return true; } catch (Exception ex) { return false; } } public static string GetSQLCount(string strSQL, SqlConnection sqlConn) { string strCount = ""; DataTable dtTemp = GetSQLResult(strSQL, sqlConn); strCount = dtTemp.Rows.Count.ToString(); //回傳查詢結果 if(strCount == "") { return "0"; } else { return strCount; } } #endregion } }