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.
170 lines
5.2 KiB
170 lines
5.2 KiB
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 Npgsql;
|
|
|
|
|
|
namespace ManagementSystem.Utility
|
|
{
|
|
public static class MSSQLUtility
|
|
{
|
|
|
|
#region 取得指定Table資料
|
|
public static DataSet GetTable(string strTableName, string[] strColumnList , string strWhere, string strOrderBy)
|
|
{
|
|
DataSet dsData = new DataSet();
|
|
try
|
|
{
|
|
string strColumns = "";
|
|
foreach (string strColumn in strColumnList) //取得欄位陣列
|
|
{
|
|
if (strColumns != "")
|
|
{
|
|
strColumns += "," + strColumn.Trim();
|
|
}
|
|
else
|
|
{
|
|
strColumns = strColumn;
|
|
}
|
|
}
|
|
string strCommand = string.Format("Select {0} From {1} ",strColumns, strTableName);
|
|
|
|
if (strWhere != "") //設定過濾條件
|
|
{
|
|
strCommand += " Where " + strWhere;
|
|
}
|
|
|
|
if (strOrderBy != "") //取得排序欄位
|
|
{
|
|
strCommand += " Order By " + strOrderBy;
|
|
}
|
|
|
|
//using (SqlConnection sqlConn = GetConn(MainForm.strAccountingBookID))
|
|
//{
|
|
// using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strCommand, sqlConn))
|
|
// {
|
|
// if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
|
|
// {
|
|
// sqlConn.Open();
|
|
// }
|
|
// sqlAdapter.Fill(dsData, "Result");
|
|
// }
|
|
//}
|
|
return dsData;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static DataSet GetTable(string strTableName, string[] strColumnList, string strWhere)
|
|
{
|
|
return GetTable(strTableName, strColumnList, strWhere, "");
|
|
}
|
|
|
|
public static DataSet GetTable(string strTableName, string[] strColumnList)
|
|
{
|
|
return GetTable(strTableName, strColumnList,"","");
|
|
}
|
|
|
|
public static DataSet GetTable(string strTableName)
|
|
{
|
|
return GetTable(strTableName,new string[] {"*"}, "", "");
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
#region MS-SQL命令相關程式集
|
|
public static SqlConnection GetConn(string strIP, string strDBName, string strID, string strPWD)
|
|
{
|
|
string strConnectionString = "server="+ strIP +";database=" + strDBName + ";uid=" + strID + ";pwd=" + strPWD;
|
|
SqlConnection sqlConn = new SqlConnection(strConnectionString);
|
|
sqlConn.ConnectionString = strConnectionString;
|
|
return sqlConn;
|
|
}
|
|
|
|
public static DataSet GetSQLResult(string strSQL, SqlConnection sqlConn)
|
|
{
|
|
DataSet dsData = new DataSet();
|
|
try
|
|
{
|
|
|
|
using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSQL, sqlConn))
|
|
{
|
|
if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
|
|
{
|
|
sqlConn.Open();
|
|
}
|
|
sqlAdapter.Fill(dsData, "Result");
|
|
}
|
|
|
|
return dsData;
|
|
}
|
|
catch(Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static bool IsExist(string strSQL, SqlConnection sqlConn)
|
|
{
|
|
try
|
|
{
|
|
DataSet sdResult = GetSQLResult(strSQL, sqlConn);
|
|
DataTable dtResult = sdResult.Tables[0];
|
|
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 = "";
|
|
DataSet dsTemp = GetSQLResult(strSQL, sqlConn);
|
|
strCount = dsTemp.Tables[0].Rows.Count.ToString();
|
|
|
|
//回傳查詢結果
|
|
if(strCount == "")
|
|
{ return "0"; }
|
|
else
|
|
{ return strCount; }
|
|
}
|
|
#endregion
|
|
|
|
|
|
|
|
}
|
|
}
|