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.4 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 PostgreSQLUtility
{
#region 取得指定Table資料
public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, string strWhere, string strOrderBy, NpgsqlConnection sqlConn)
{
DataSet dsData = new DataSet();
try
{
string strColumns = "";
string strCommand = "";
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 (NpgsqlDataAdapter sqlAdapter = new NpgsqlDataAdapter(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, NpgsqlConnection sqlConn)
{
return GetTable(strTableName, intTopCount, strColumnList, strWhere, "", sqlConn);
}
public static DataTable GetTable(string strTableName, int intTopCount, string[] strColumnList, NpgsqlConnection sqlConn)
{
return GetTable(strTableName, intTopCount, strColumnList, "", "", sqlConn);
}
public static DataTable GetTable(string strTableName, int intTopCount, NpgsqlConnection sqlConn)
{
return GetTable(strTableName, intTopCount, new string[] { "*" }, "", "", sqlConn);
}
public static DataTable GetTable(string strTableName, NpgsqlConnection sqlConn)
{
return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn);
}
#endregion
#region MS-SQL命令相關程式集
public static NpgsqlConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort)
{
try
{
string strConnectionString = "Host=" + strIP + ";Port="+ strPort + ";Database=" + strDBName + ";Username=" + strID + ";Password=" + strPWD;
NpgsqlConnection sqlConn = new NpgsqlConnection(strConnectionString);
sqlConn.ConnectionString = strConnectionString;
sqlConn.Open();
return sqlConn;
}
catch (Exception ex)
{
ErrorHandler.WriteErrorLog("PostgreSQLUtilityClass.cs", ex);
return null;
}
}
public static DataTable GetSQLResult(string strSQL, NpgsqlConnection sqlConn)
{
DataSet dsData = new DataSet();
try
{
using (NpgsqlDataAdapter sqlAdapter = new NpgsqlDataAdapter(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, NpgsqlConnection 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, NpgsqlConnection sqlConn)
{
try
{
NpgsqlCommand sqlCmd = new NpgsqlCommand(strSQL, sqlConn);
if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
{
sqlConn.Open();
}
sqlCmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
}
public static string GetSQLCount(string strSQL, NpgsqlConnection sqlConn)
{
string strCount = "";
DataTable dtTemp = GetSQLResult(strSQL, sqlConn);
strCount = dtTemp.Rows.Count.ToString();
//回傳查詢結果
if (strCount == "")
{ return "0"; }
else
{ return strCount; }
}
#endregion
}
}