国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在ADO.NET中使用事務保護數據的完整性(4)

2024-07-10 13:03:25
字體:
來源:轉載
供稿:網友
實施事務

既然我們已經看了類和成員,讓我們來看一下基本的實施情況.接下來的代碼是一個簡單的情況,使用事務來保證兩個存儲過程-一個從表中刪除庫存,另一個增加庫存在另個表中,或同時執行,或失敗.

using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.data.sqlclient;
using system.data.sqltypes;

…public void sptransaction(int partid, int numbermoved, int siteid)
{
// create and open the connection.
sqlconnection conn = new sqlconnection();
string connstring = "server=sqlinstance;database=test;"
+ "integrated security=sspi";
conn.connectionstring = connstring;
conn.open();

// create the commands and related parameters.
// cmddebit debits inventory from the warehouseinventory
// table by calling the debitwarehouseinventory
// stored procedure.
sqlcommand cmddebit =
new sqlcommand("debitwarehouseinventory", conn);
cmddebit.commandtype = commandtype.storedprocedure;
cmddebit.parameters.add("@partid", sqldbtype.int, 0, "partid");
cmddebit.parameters["@partid"].direction =
parameterdirection.input;
cmddebit.parameters.add("@debit", sqldbtype.int, 0, "quantity");
cmddebit.parameters["@debit"].direction =
parameterdirection.input;

// cmdcredit adds inventory to the siteinventory
// table by calling the creditsiteinventory
// stored procedure.
sqlcommand cmdcredit =
new sqlcommand("creditsiteinventory", conn);
cmdcredit.commandtype = commandtype.storedprocedure;
cmdcredit.parameters.add("@partid", sqldbtype.int, 0, "partid");
cmdcredit.parameters["@partid"].direction =
parameterdirection.input;
cmdcredit.parameters.add
("@credit", sqldbtype.int, 0, "quantity");
cmdcredit.parameters["@credit"].direction =
parameterdirection.input;
cmdcredit.parameters.add("@siteid", sqldbtype.int, 0, "siteid");
cmdcredit.parameters["@siteid"].direction =
parameterdirection.input;

// begin the transaction and enlist the commands.
sqltransaction tran = conn.begintransaction();
cmddebit.transaction = tran;
cmdcredit.transaction = tran;

try
{
// execute the commands.
cmddebit.parameters["@partid"].value = partid;
cmddebit.parameters["@debit"].value = numbermoved;
cmddebit.executenonquery();

cmdcredit.parameters["@partid"].value = partid;
cmdcredit.parameters["@credit"].value = numbermoved;
cmdcredit.parameters["@siteid"].value = siteid;
cmdcredit.executenonquery();

// commit the transaction.
tran.commit();
}
catch(sqlexception ex)
{
// roll back the transaction.
tran.rollback();

// additional error handling if needed.
}
finally
{
// close the connection.
conn.close();
}
}




// commit the outer transaction.

tran.commit();

}

catch(oledbexception ex)

{

//roll back the transaction.

tran.rollback();



//additional error handling if needed.

}

finally

{

// close the connection.

conn.close();

}

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建宁县| 辽阳县| 工布江达县| 云阳县| 会昌县| 大竹县| 牙克石市| 仙游县| 昌吉市| 浮山县| 凤庆县| 东乡县| 正宁县| 盘锦市| 肥城市| 会东县| 乐至县| 兴仁县| 湘西| 育儿| 罗城| 班戈县| 大竹县| 巴南区| 雷州市| 宝山区| 栾城县| 萨迦县| 盘山县| 禄劝| 德兴市| 镇雄县| 汾西县| 湖北省| 辽阳县| 革吉县| 营口市| 山阴县| 丽水市| 潮州市| 赤壁市|