|
|
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; using MySqlConnector;
namespace ManagementSystem.Utility { public static class MySQLUtility { #region 取得指定Table資料
public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList , string strWhere, string strOrderBy, MySqlConnection 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 {1} From {2} Limit {0}", 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 (MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(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, MySqlConnection sqlConn) { return GetTable(strTableName, intTopCount, strColumnList, strWhere, "", sqlConn); }
public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, MySqlConnection sqlConn) { return GetTable(strTableName, intTopCount, strColumnList,"","", sqlConn); }
public static DataTable GetTable(string strTableName, int intTopCount, MySqlConnection sqlConn) { return GetTable(strTableName, intTopCount,new string[] {"*"}, "", "", sqlConn); }
public static DataTable GetTable(string strTableName, MySqlConnection sqlConn) { return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn); }
#endregion
#region MyQL命令相關程式集
public static MySqlConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort) { try { string strConnectionString = "server=" + strIP + ";port=" + strPort + ";database=" + strDBName + ";user id=" + strID + ";password=" + strPWD + ";charset=utf8;"; MySqlConnection sqlConn = new MySqlConnection(strConnectionString); sqlConn.ConnectionString = strConnectionString; sqlConn.Open(); return sqlConn; } catch (Exception ex) { ErrorHandler.WriteErrorLog("MySQLUtilityClass.cs", ex); return null; } }
public static DataTable GetSQLResult(string strSQL, MySqlConnection sqlConn) { DataSet dsData = new DataSet(); try { using (MySqlDataAdapter sqlAdapter = new MySqlDataAdapter(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, MySqlConnection 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, MySqlConnection sqlConn) { try { MySqlCommand sqlCmd = new MySqlCommand(strSQL, sqlConn); if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
{ sqlConn.Open(); } sqlCmd.ExecuteNonQuery(); return true; } catch (Exception ex) { return false; } }
public static string GetSQLCount(string strSQL, MySqlConnection sqlConn) { string strCount = ""; DataTable dtTemp = GetSQLResult(strSQL, sqlConn); strCount = dtTemp.Rows.Count.ToString();
//回傳查詢結果
if(strCount == "") { return "0"; } else { return strCount; } } #endregion
} }
|