首页 > 开发 > 综合 > 正文

在数据库中开始一个事务

2024-07-21 02:22:54
字体:
来源:转载
供稿:网友


下面的示例创建一个 oracleconnection 和一个 oracletransaction。它还演示了如何使用 begintransaction、commit 和 rollback 方法。

public void runoracletransaction(string myconnstring)
{
oracleconnection myconnection = new oracleconnection(myconnstring);
myconnection.open();

oraclecommand mycommand = myconnection.createcommand();
oracletransaction mytrans;

// start a local transaction
mytrans = myconnection.begintransaction(isolationlevel.readcommitted);
// assign transaction object for a pending local transaction
mycommand.transaction = mytrans;

try
{
mycommand.commandtext = "insert into dept (deptno, dname, loc) values (50, 'technology', 'denver')";
mycommand.executenonquery();
mycommand.commandtype= commandtype.storedprocedure;
mycommand.commandtext="prc_test";
mycommand.executenonquery();
mytrans.commit();
console.writeline("both records are written to database.");
}
catch(exception e)
{
mytrans.rollback();
console.writeline(e.tostring());
console.writeline("neither record was written to database.");
}
finally
{
myconnection.close();
}
}



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表