国产探花免费观看_亚洲丰满少妇自慰呻吟_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();

}

}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 基隆市| 抚顺市| 桑植县| 阿合奇县| 敖汉旗| 南部县| 铁岭市| 礼泉县| 博客| 临桂县| 孝感市| 兰溪市| 南宁市| 兰坪| 凉城县| 巍山| 平和县| 丹东市| 汕尾市| 奉新县| 肇州县| 武威市| 宜兰县| 陈巴尔虎旗| 津南区| 凤阳县| 阳新县| 德令哈市| 翁牛特旗| 洪湖市| 潜江市| 涟源市| 大冶市| 沛县| 互助| 湘乡市| 乐至县| 琼中| 鞍山市| 南投县| 纳雍县|