午夜国产狂喷潮在线观看|国产AⅤ精品一区二区久久|中文字幕AV中文字幕|国产看片高清在线

    異步的 SQL 數(shù)據(jù)庫(kù)封裝(1)
    來(lái)源:易賢網(wǎng) 閱讀:1337 次 日期:2015-08-31 15:37:13
    溫馨提示:易賢網(wǎng)小編為您整理了“異步的 SQL 數(shù)據(jù)庫(kù)封裝(1)”,方便廣大網(wǎng)友查閱!

    引言

    我一直在尋找一種簡(jiǎn)單有效的庫(kù),它能在簡(jiǎn)化數(shù)據(jù)庫(kù)相關(guān)的編程的同時(shí)提供一種異步的方法來(lái)預(yù)防死鎖。

    我找到的大部分庫(kù)要么太繁瑣,要么靈活性不足,所以我決定自己寫(xiě)個(gè)。

    使用這個(gè)庫(kù),你可以輕松地連接到任何 SQL-Server 數(shù)據(jù)庫(kù),執(zhí)行任何存儲(chǔ)過(guò)程或 T-SQL 查詢,并異步地接收查詢結(jié)果。這個(gè)庫(kù)采用 C# 開(kāi)發(fā),沒(méi)有其他外部依賴。

    背景

    你可能需要一些事件驅(qū)動(dòng)編程的背景知識(shí),但這不是必需的。

    使用

    這個(gè)庫(kù)由兩個(gè)類組成:

    BLL (Business Logic Layer) 提供訪問(wèn)MS-SQL數(shù)據(jù)庫(kù)、執(zhí)行命令和查詢并將結(jié)果返回給調(diào)用者的方法和屬性。你不能直接調(diào)用這個(gè)類的對(duì)象,它只供其他類繼承.

    DAL (Data Access Layer) 你需要自己編寫(xiě)執(zhí)行SQL存儲(chǔ)過(guò)程和查詢的函數(shù),并且對(duì)于不同的表你可能需要不同的DAL類。

    首先,你需要像這樣創(chuàng)建 DAL 類:

    namespace SQLWrapper

    {

    public class DAL : BLL

    {

    public DAL(string server, string db, string user, string pass)

    {

    base.Start(server, db, user, pass);

    }

    ~DAL()

    {

    base.Stop(eStopType.ForceStopAll);

    }

    ///////////////////////////////////////////////////////////

    // TODO: Here you can add your code here...

    }

    }

    由于BLL類維護(hù)著處理異步查詢的線程,你需要提供必要的數(shù)據(jù)來(lái)拼接連接字符串。千萬(wàn)別忘了調(diào)用`Stop`函數(shù),否則析構(gòu)函數(shù)會(huì)強(qiáng)制調(diào)用它。

    NOTE:如果需要連接其他非MS-SQL數(shù)據(jù)庫(kù),你可以通過(guò)修改BLL類中的`CreateConnectionString`函數(shù)來(lái)生成合適的連接字符串。

    為了調(diào)用存儲(chǔ)過(guò)程,你應(yīng)該在DAL中編寫(xiě)這種函數(shù):

    public int MyStoreProcedure(int param1, string param2)

    {

    // 根據(jù)存儲(chǔ)過(guò)程的返回類型創(chuàng)建用戶數(shù)據(jù)

    StoredProcedureCallbackResult userData = new StoredProcedureCallbackResult(eRequestType.Scalar);

    // 在此定義傳入存儲(chǔ)過(guò)程的參數(shù),如果沒(méi)有參數(shù)可以省略 <span style="line-height:1.5;font-size:9pt;">userData.Parameters = new System.Data.SqlClient.SqlParameter[] { </span>

    new System.Data.SqlClient.SqlParameter("@param1", param1),

    new System.Data.SqlClient.SqlParameter("@param2", param2),

    };

    // Execute procedure...

    if (!ExecuteStoredProcedure("usp_MyStoreProcedure", userData))

    throw new Exception("Execution failed");

    // 等待執(zhí)行完成...

    // 等待時(shí)長(zhǎng)為 <userdata.tswaitforresult>

    // 執(zhí)行未完成返回 <timeout>

    if (WaitSqlCompletes(userData) != eWaitForSQLResult.Success)

    throw new Exception("Execution failed");

    // Get the result...

    return userData.ScalarValue;

    }

    正如你所看到的,存儲(chǔ)過(guò)程的返回值類型可以是`Scalar`,`Reader`和`NonQuery`。對(duì)于 `Scalar`,`userData`的`ScalarValue`參數(shù)有意義(即返回結(jié)果);對(duì)于`NonQuery`,`userData`的 `AffectedRows`參數(shù)就是受影響的行數(shù);對(duì)于`Reader`類型,`ReturnValue`就是函數(shù)的返回值,另外你可以通過(guò) `userData`的`resultDataReader`參數(shù)訪問(wèn)recordset。

    再看看這個(gè)示例:

    public bool MySQLQuery(int param1, string param2)

    {

    // Create user data according to return type of store procedure in SQL(這個(gè)注釋沒(méi)有更新,說(shuō)明《注釋是魔鬼》有點(diǎn)道理)

    ReaderQueryCallbackResult userData = new ReaderQueryCallbackResult();

    string sqlCommand = string.Format("SELECT TOP(1) * FROM tbl1

    WHERE code = {0} AND name LIKE &apos;%{1}%&apos;", param1, param2);

    // Execute procedure...

    if (!ExecuteSQLStatement(sqlCommand, userData))

    return false;

    // Wait until it finishes...

    // Note, it will wait (userData.tsWaitForResult)

    // for the command to be completed otherwise returns <timeout>

    if (WaitSqlCompletes(userData) != eWaitForSQLResult.Success)

    return false;

    // Get the result...

    if(userData.resultDataReader.HasRows && userData.resultDataReader.Read())

    {

    // Do whatever you want....

    int field1 = GetIntValueOfDBField(userData.resultDataReader["Field1"], -1);

    string field2 = GetStringValueOfDBField(userData.resultDataReader["Field2"], null);

    Nullable<datetime> field3 = GetDateValueOfDBField(userData.resultDataReader["Field3"], null);

    float field4 = GetFloatValueOfDBField(userData.resultDataReader["Field4"], 0);

    long field5 = GetLongValueOfDBField(userData.resultDataReader["Field5"], -1);

    }

    userData.resultDataReader.Dispose();

    return true;

    }

    在這個(gè)例子中,我們調(diào)用 `ExecuteSQLStatement` 直接執(zhí)行了一個(gè)SQL查詢,但思想跟 `ExecuteStoredProcedure` 是一樣的。

    我們使用 `resultDataReader` 的 `.Read()` 方法來(lái)迭代處理返回的結(jié)果集。另外提供了一些helper方法來(lái)避免疊代中由于NULL字段、GetIntValueOfDBField 等引起的異常。

    更多信息請(qǐng)查看數(shù)據(jù)庫(kù)
    易賢網(wǎng)手機(jī)網(wǎng)站地址:異步的 SQL 數(shù)據(jù)庫(kù)封裝(1)
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2025國(guó)考·省考課程試聽(tīng)報(bào)名

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)