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.
207 lines
6.8 KiB
207 lines
6.8 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 Oracle.ManagedDataAccess.Client;
|
|
|
|
|
|
|
|
namespace ManagementSystem.Utility
|
|
{
|
|
public static class OracleUtility
|
|
{
|
|
|
|
#region 取得指定Table資料
|
|
public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList , string strWhere, string strOrderBy, OracleConnection 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} FETCH FIRST {0} ROWS ONLY", 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 (OracleDataAdapter sqlAdapter = new OracleDataAdapter(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, OracleConnection sqlConn)
|
|
{
|
|
return GetTable(strTableName, intTopCount ,strColumnList, strWhere, "", sqlConn);
|
|
}
|
|
|
|
public static DataTable GetTable(string strTableName,int intTopCount, string[] strColumnList, OracleConnection sqlConn)
|
|
{
|
|
return GetTable(strTableName, intTopCount, strColumnList,"","", sqlConn);
|
|
}
|
|
|
|
public static DataTable GetTable(string strTableName,int intTopCount, OracleConnection sqlConn)
|
|
{
|
|
return GetTable(strTableName, intTopCount, new string[] {"*"}, "", "", sqlConn);
|
|
}
|
|
|
|
public static DataTable GetTable(string strTableName, OracleConnection sqlConn)
|
|
{
|
|
return GetTable(strTableName, 0, new string[] { "*" }, "", "", sqlConn);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Oracle命令相關程式集
|
|
public static OracleConnection GetConn(string strIP, string strDBName, string strID, string strPWD, string strPort)
|
|
{
|
|
try
|
|
{
|
|
string strConnectionString = "Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = " + strIP + ")(PORT = " + strPort + ")))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = " + strDBName + "))); User Id = " + strID + "; Password = " + strPWD + ";";
|
|
OracleConnection sqlConn = new OracleConnection(strConnectionString);
|
|
sqlConn.ConnectionString = strConnectionString;
|
|
sqlConn.Open();
|
|
return sqlConn;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.GetConn", ex);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public static DataTable GetSQLResult(string strSQL, OracleConnection sqlConn)
|
|
{
|
|
DataSet dsData = new DataSet();
|
|
try
|
|
{
|
|
using (OracleDataAdapter sqlAdapter = new OracleDataAdapter(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)
|
|
{
|
|
ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.GetSQLResult", ex);
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
public static bool IsExist(string strSQL, OracleConnection 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, OracleConnection sqlConn)
|
|
{
|
|
try
|
|
{
|
|
OracleCommand sqlCmd = new OracleCommand(strSQL, sqlConn);
|
|
if (sqlConn.State == ConnectionState.Closed) //判斷連線狀態
|
|
{
|
|
sqlConn.Open();
|
|
}
|
|
sqlCmd.ExecuteNonQuery();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorHandler.WriteErrorLog("OracleSQLUtilityClass.RunSQLNonReturn", ex);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static string GetSQLCount(string strSQL, OracleConnection sqlConn)
|
|
{
|
|
string strCount = "";
|
|
DataTable dtTemp = GetSQLResult(strSQL, sqlConn);
|
|
strCount = dtTemp.Rows.Count.ToString();
|
|
|
|
//回傳查詢結果
|
|
if(strCount == "")
|
|
{ return "0"; }
|
|
else
|
|
{ return strCount; }
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|