Files
17168ERP/web/App_Code/Cus_GetSQL.cs
2025-08-29 01:27:25 +08:00

307 lines
13 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.OleDb;
namespace Cus_GetSQL
{
public class Get_Data
{
// 全域變數。從 Stored Procedure 回傳的單一數值,取得符合查詢條件的資料列總筆數
public int _totalRecords = 0;
// 取得 Web.config 連線字串的名稱
public string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["shopConn"].ToString();
public Get_Data()
{
}
#region WebForm ObjectDataSource SELECT
// true 表示為 ObjectDataSource 要 SELECT 時的預設值
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public DataSet Get_ObjectDataSource_Val(string TableName_val, string ColumnName_val, string OrderColumn_val, string Where_val, int startRowIndex, int maxinumRows, string sortExpression)
{
// 最後三個參數startRowIndex, maxinumRows, sortExpressionObjectDataSource 控制項會自動傳入,開發人員不用手動撰碼傳值
DataSet ds = null;
try
{
//HttpContext.Current.Response.Write("變數=" & eztrust)
//檢查所帶進來的參數是否有值(如果為empty會發生錯誤)
if (TableName_val == null | Convert.ToString(TableName_val)=="")
{
TableName_val = "";
}
if (ColumnName_val == null | Convert.ToString(ColumnName_val) == "")
{
ColumnName_val = "";
}
if (OrderColumn_val == null | Convert.ToString(OrderColumn_val) == "")
{
OrderColumn_val = "";
}
if (Where_val == null | Convert.ToString(Where_val) == "")
{
Where_val = "";
}
// 參數: (Table名稱、查詢Column名稱集合、Where條件(包括模糊查詢關鍵字)、排序方式(按標題搜尋前)、起始撈取的資料列索引、GridView每頁要顯示的筆數、排序方式(按標題搜尋後))
ds = SelectData(TableName_val.Trim(), ColumnName_val.Trim(), Where_val, OrderColumn_val.Trim(), startRowIndex, maxinumRows, sortExpression);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
//顯示錯誤訊息,如資料庫存取發生錯誤,或網路中斷、資料庫關機
}
return ds;
}
#endregion
#region SELECT ( Stored Procedure )
// true 表示為 ObjectDataSource 要 SELECT 時的預設值
[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public DataSet SelectData(string strTableName, string strColumnName, string strSqlWhere, string strDefaultOrderColumn, int startRowIndex, int maxinumRows, string sortExpression)
{
OleDbConnection sqlConn = null;
OleDbCommand sqlCmd = null;
OleDbDataAdapter sqlAdapter = null;
DataSet ds = null;
string strOrderBy = null;
try
{
sqlConn = new OleDbConnection(strConnString);
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
ds = new DataSet();
// 取得排序資料 (昇幕或降幕)
if (string.IsNullOrEmpty(sortExpression))
{
//排序欄位。第一次進入頁面時sortExpression 為空值,所以以開發人員在層指定的欄位排序
strOrderBy = strDefaultOrderColumn;
}
else if ((!string.IsNullOrEmpty(sortExpression)) & (sortExpression.IndexOf(" DESC") == -1))
{
// 排序欄位。若使用者按下了 GridView 某個欄位的 title此時 sortExpression 有值,且要由小到大排序
strOrderBy = sortExpression + " ASC";
}
else if ((!string.IsNullOrEmpty(sortExpression)) & (sortExpression.IndexOf(" DESC") != -1))
{
// 刪除 ObjectDataSource 自動在最後面加的「 DESC」字樣(5個字元)
sortExpression = sortExpression.Remove(sortExpression.Length - 5, 5);
// 排序欄位。若使用者按下了 GridView 某個欄位的 title此時 sortExpression 有值,且要由大到小排序
strOrderBy = sortExpression + " DESC";
}
// SELECT SQL 的 WHERE 條件 (包含使用者輸入的模糊查詢關鍵字)
if (string.IsNullOrEmpty(strSqlWhere))
{
// 若沒加這句,則當 WHERE 條件為空值時(使用者未輸入搜尋關鍵字),丟到 Stored Procedure 裡後會 Error
strSqlWhere = " 1=1 ";
}
sqlCmd = new OleDbCommand("dbo.pager_eztrust", sqlConn);
// 系統中每支程式共用的預存程序名稱
sqlCmd.CommandType = CommandType.StoredProcedure;
// 資料來源為 Stored Procedure(預存程序)
sqlCmd.Parameters.Add(new OleDbParameter("@StartRowIndex", startRowIndex));
// 目前頁面,要撈的資料,的第一筆資料的索引 (第一筆為 0依序加 1依此類推)
sqlCmd.Parameters.Add(new OleDbParameter("@PageSize", maxinumRows));
// GridView 每頁要顯示的資料筆數
sqlCmd.Parameters.Add(new OleDbParameter("@tableName", strTableName));
sqlCmd.Parameters.Add(new OleDbParameter("@columnName", strColumnName));
sqlCmd.Parameters.Add(new OleDbParameter("@sqlWhere", strSqlWhere));
sqlCmd.Parameters.Add(new OleDbParameter("@orderBy", strOrderBy));
sqlCmd.Parameters.Add(new OleDbParameter("@rowCount", SqlDbType.Int));
// Stored Procedure 會傳回一個數字(符合查詢條件的資料總筆數)
sqlCmd.Parameters["@rowCount"].Direction = ParameterDirection.Output;
// 用來在 GridView 下方的「頁碼列」顯示頁碼之用(Ex: 1 2 3 4 ... 55)
sqlAdapter = new OleDbDataAdapter(sqlCmd);
sqlAdapter.Fill(ds);
_totalRecords = Convert.ToInt32(sqlCmd.Parameters["@rowCount"].Value);
// 總筆數 (要回傳至前端 .aspx給 GridView 下方的「頁碼列」顯示用)
HttpContext.Current.Session["s_RecordTotalCount"] = _totalRecords;
// 存至 Session以便在.aspx.vb中取用後用來在 GridView 下方的「頁碼列」中顯示頁碼之用
Console.Write("tc: " + _totalRecords);
int jj = ds.Tables[0].Rows.Count;
// 實際從資料庫撈回來,儲存在 AP Server 記憶體中 DataSet/DataTable 的資料筆數
Console.Write(jj);
// GridView 每頁要呈現的資料筆數
}
}
catch (OleDbException ex)
{
Console.WriteLine(ex.ToString());
throw;
}
catch (Exception ex)
{
throw new Exception("發生 SqlClient 以外的資料庫錯誤: " + ex.Message);
}
finally
{
sqlCmd.Dispose();
sqlAdapter.Dispose();
ds.Dispose();
if (sqlConn.State == ConnectionState.Open)
{
sqlConn.Close(); sqlConn.Dispose();
}
sqlConn.Dispose();
}
return ds;
}
#endregion
#region Stored Procedure GridView (Ex: 1 2 3 4 ... 55)
public int getRecordCount(string TableName_val, string ColumnName_val, string OrderColumn_val, string Where_val)
{
return _totalRecords;
}
#endregion
}
public struct GetSqlData
{
public string tableName;
public string columnName;
public string sqlWhere;
public string sort;
public int startRowIndex;
public int maxinumRows;
public DataSet selectData()
{
string db = ConfigurationManager.ConnectionStrings["shopConn"].ConnectionString;
string p_name = ConfigurationManager.ConnectionStrings["shopConn"].ProviderName;
//ProviderName
MyWeb.sql sql = new MyWeb.sql();
OleDbConnection sqlConn = new OleDbConnection(db);
OleDbCommand sqlCmd = null;
OleDbDataAdapter sqlAdapter = null;
DataSet ds = null;
string strOrderBy = null;
try
{
sqlConn.Open();
if (sqlConn.State == ConnectionState.Open)
{
ds = new DataSet();
// SELECT SQL 的 WHERE 條件 (包含使用者輸入的模糊查詢關鍵字)
if (string.IsNullOrEmpty(sqlWhere))
{
// 若沒加這句,則當 WHERE 條件為空值時(使用者未輸入搜尋關鍵字),丟到 Stored Procedure 裡後會 Error
sqlWhere = " 1=1 ";
}
sqlCmd = new OleDbCommand("dbo.pager_eztrust", sqlConn);
// 系統中每支程式共用的預存程序名稱
sqlCmd.CommandType = CommandType.StoredProcedure;
// 資料來源為 Stored Procedure(預存程序)
sqlCmd.Parameters.Add(new OleDbParameter("@StartRowIndex", startRowIndex));
// 目前頁面,要撈的資料,的第一筆資料的索引 (第一筆為 0依序加 1依此類推)
sqlCmd.Parameters.Add(new OleDbParameter("@PageSize", maxinumRows));
// 每頁要顯示的資料筆數
sqlCmd.Parameters.Add(new OleDbParameter("@tableName", tableName + " "));
sqlCmd.Parameters.Add(new OleDbParameter("@columnName", columnName));
sqlCmd.Parameters.Add(new OleDbParameter("@sqlWhere", sqlWhere));
sqlCmd.Parameters.Add(new OleDbParameter("@orderBy", sort));
sqlCmd.Parameters.Add(new OleDbParameter("@rowCount", SqlDbType.Int));
// Stored Procedure 會傳回一個數字(符合查詢條件的資料總筆數)
sqlCmd.Parameters["@rowCount"].Direction = ParameterDirection.Output;
// 用來在 GridView 下方的「頁碼列」顯示頁碼之用(Ex: 1 2 3 4 ... 55)
sqlAdapter = new OleDbDataAdapter(sqlCmd);
sqlAdapter.Fill(ds);
int totalRecords = Convert.ToInt32(sqlCmd.Parameters["@rowCount"].Value);
// 回傳回來的總筆數
HttpContext.Current.Session["s_RecordTotalCount"] = totalRecords;
// 存至 Session以便在.aspx.vb中取用後
}
}
catch (OleDbException ex)
{
Console.WriteLine(ex.ToString());
throw;
}
catch (Exception ex)
{
throw new Exception("發生 SqlClient 以外的資料庫錯誤: " + ex.Message);
}
finally
{
sqlCmd.Dispose();
sqlAdapter.Dispose();
ds.Dispose();
if (sqlConn.State == ConnectionState.Open)
{
sqlConn.Close(); sqlConn.Dispose();
}
sqlConn.Dispose();
}
return ds;
}
}
}
namespace Cus_SQL
{
#region sql陣列組成字串傳回
public class SQL_Array
{
public string sql_str(ArrayList fs)
{
string functionReturnValue = null;
for (int i = 0; i <= fs.Count - 1; i++)
{
if (functionReturnValue != null)
{ functionReturnValue += " AND "; }
functionReturnValue += fs[i];
}
return functionReturnValue;
}
}
#endregion
}