Table of Contents

Class SQL

Namespace
ZapLib
Assembly
ZapLib.dll

SQL Server 連線查詢輔助工具

public class SQL
Inheritance
SQL
Inherited Members

Constructors

SQL(bool)

初始化 SQL 連線物件,將使用 .config 中的資料庫連線資訊進行連線

public SQL(bool transaction = false)

Parameters

transaction bool

是否開啟 transaction

SQL(string, bool)

初始化 SQL 連線物件,嘗試從 .config 中抓取指定名稱的連線字串或 直接給與連線字串 進行連線

public SQL(string connectionString, bool transaction = false)

Parameters

connectionString string

指定的連線字串名稱 或 連線字串

transaction bool

是否開啟 transaction

SQL(string, string, string, string, bool)

初始化 SQL 連線物件,將使用指定的連線資訊進行連線

public SQL(string dbHost, string dbName, string dbAct, string dbPwd, bool transaction = false)

Parameters

dbHost string

資料庫位置

dbName string

資料庫名稱

dbAct string

連線帳號

dbPwd string

連線密碼

transaction bool

是否開啟 transaction

Properties

ApplicationIntent

資料庫工作負載類型,預設為 ReadWrite

public string ApplicationIntent { get; set; }

Property Value

string

Cmd

目前使用的資料庫命令物件

public SqlCommand Cmd { get; }

Property Value

SqlCommand

EnableDBAlwaysOn

是否啟用 DB Always On 才有的 ApplicationIntent=ReadOnly 架構,預設為 false。 也可於 Config 中指定 EnableDBAlwaysOn 為 True。

public bool EnableDBAlwaysOn { get; set; }

Property Value

bool

Encrypt

資料庫已經安裝憑證,是否對資料進行 SSL 加密,預設 false

public bool Encrypt { get; set; }

Property Value

bool

IsConn

是否已經與資料庫連線成功

public bool IsConn { get; }

Property Value

bool

MultiSubnetFailover

資料庫是否在不同子網路上的可用性群組,預設為 false

public bool MultiSubnetFailover { get; set; }

Property Value

bool

SQLDBReplaceRules

資料庫名稱取代規則

public List<Tuple<string, string>> SQLDBReplaceRules { get; set; }

Property Value

List<Tuple<string, string>>

SQLReadOnly

必須設置 EnableDBAlwaysOn 設置為 true 時,此設定才會生效 (否則為空操作)。 如果此值為 true,且 EnableDBAlwaysOn 也為 true 時,會自動將 sql connection string 中的 ApplicationIntent 設置為 ReadOnly。 此數值預設為 false

public bool SQLReadOnly { get; set; }

Property Value

bool

Timeout

資料庫連線逾時秒數,預設 30 秒

public int Timeout { get; set; }

Property Value

int

TraceCode

追蹤馬, 同一個追蹤馬表是同一個SQL物件

public string TraceCode { get; }

Property Value

string

Tran

目前使用的資料庫交易物件 (需先啟用 transaction)

public SqlTransaction Tran { get; }

Property Value

SqlTransaction

TrustServerCertificate

資料庫傳輸通道使否開啟 SSL 加密,預設為 false

public bool TrustServerCertificate { get; set; }

Property Value

bool

Methods

BuildConnectionString()

建構連線字串

public string BuildConnectionString()

Returns

string

重新補全過的連線字串

BuildconnString()

[已棄用] 建構連線字串。請改用 BuildConnectionString()

[Obsolete("方法已重新命名為 BuildConnectionString,原拼字錯誤的版本將於下一個 major 版本移除")]
public string BuildconnString()

Returns

string

重新補全過的連線字串

Close()

手動關閉資料庫連線

public void Close()

Connect()

手動連線資料庫,可以使用 IsConn 來確認使否連線成功

public void Connect()

Connet()

[已棄用] 手動連線資料庫。請改用 Connect()

[Obsolete("方法已重新命名為 Connect,原拼字錯誤的版本將於下一個 major 版本移除")]
public void Connet()

Exec<T>(string, object)

手動執行預存程序,需自行控制可能發生的錯誤

[Obsolete("Exec<T> 即將棄用,請改用 QuickExec<T> 取代,詳情:http://10.190.173.136/SideProject/ZapLib/issues/30")]
public T Exec<T>(string sql, object param = null)

Parameters

sql string

預存程序名稱

param object

語法中的參數化資料

Returns

T

綁定預存程序輸出數值的資料模型

Type Parameters

T

將返回資料綁定到指定類型 T

GetConnection()

取得主要的資料庫連線物件

public SqlConnection GetConnection()

Returns

SqlConnection

GetErrorMessage()

取得所有資料庫的錯誤訊息

public string GetErrorMessage()

Returns

string

Query(string, object)

手動執行查詢命令,需自行控制可能發生的錯誤

public SqlDataReader Query(string sql, object param = null)

Parameters

sql string

查詢語法

param object

語法中的參數化資料

Returns

SqlDataReader

返回 SqlDataReader 物件可自行控制

QuickBulkCopy(DataTable, string)

自動開啟連線並執行大量資料寫入作業,執行完畢後自動關閉連線

public bool QuickBulkCopy(DataTable data, string tableName)

Parameters

data DataTable

儲存大量資料的資料表物件

tableName string

寫入的資料表名稱

Returns

bool

是否寫入成功

QuickDynamicQuery(string, object, bool)

[有風險] 自動開啟連線並執行查詢語法,執行完畢後自動關閉連線

public dynamic[] QuickDynamicQuery(string sql, object param = null, bool isfetchall = true)

Parameters

sql string

查詢語法

param object

語法中的參數化資料

isfetchall bool

是否取出所有資料,預設為 true,否則只取出 1 筆

Returns

dynamic[]

綁定查詢語法輸出表格的動態資料陣列

QuickExec<T>(string, object)

自動開啟連線並執行預存程序,執行完畢後自動關閉連線

public T QuickExec<T>(string sql, object param = null)

Parameters

sql string

預存程序名稱

param object

語法中的參數化資料

Returns

T

綁定預存程序輸出數值的資料模型

Type Parameters

T

將返回資料綁定到指定類型

QuickQuery<T>(string, object, bool)

自動開啟連線並執行查詢語法,執行完畢後自動關閉連線

public T[] QuickQuery<T>(string sql, object param = null, bool isfetchall = true)

Parameters

sql string

查詢語法

param object

語法中的參數化資料

isfetchall bool

是否取出所有資料,預設為 true,否則只取出 1 筆

Returns

T[]

綁定查詢語法輸出表格的資料模型陣列

Type Parameters

T

將返回資料綁定到指定類型

SQLDBReplace(string)

如果 config 中有指定 SQLDBReplace=true 則會啟動此功能,會抓取 config 中所有 db name 取代的設定,在 SQL 執行前進行檢查, 目前不分大小寫支援4種 Pattern,分別為: xxx.dbo, [xxx].[dbo], [xxx].dbo 與 xxx.[dbo] ,如果不是以上 pattern 則不會取代

public string SQLDBReplace(string sql)

Parameters

sql string

Returns

string

dynamicFetch(SqlDataReader, bool)

手動執行取出查詢結果的資料,並綁定到動態資料中

public dynamic[] dynamicFetch(SqlDataReader r, bool fetchAll = true)

Parameters

r SqlDataReader

資料讀取物件

fetchAll bool

是否取出所有資料,預設為 true,否則只取出 1 筆

Returns

dynamic[]

綁定資料的動態資料陣列

fetch<T>(SqlDataReader, bool)

手動執行取出查詢結果的資料,並綁定到指定的資料模型中

public T[] fetch<T>(SqlDataReader r, bool fetchAll = true)

Parameters

r SqlDataReader

資料讀取物件

fetchAll bool

是否取出所有資料,預設為 true,否則只取出 1 筆

Returns

T[]

綁定資料的模型陣列

Type Parameters

T

將資料綁定到指定類型