您的位置:首页 > 其它

.NET开发中的事务处理大比拼

2014-11-24 00:51 513 查看
本文转载:http://www.cnblogs.com/jhxk/articles/2696307.html

http://liubaolongg.blog.163.com/blog/static/21386802201222631355218/

.NET事务

ADO.NET事务

在方法之前增加属性[AutoComplete(true)],这样如果方法执行时没有异常就默认提交,如果有异常则这个方法就会回滚。

using System;
using System.Data.SqlClient;
using System.EnterpriseServices;//企业级服务COM+事务
namespace ClassTran
{
[Transaction(TransactionOption.Required)]
public class OrderData2 : ServicedComponent
{
//自动事务
[AutoComplete(true)]
public string WorkTran()
{
string msg = "";
string conString = "data source=127.0.0.1;database=codematic;
user id=sa;password=";
SqlConnection myConnection = new SqlConnection(conString);
myConnection.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
try
{
myCommand.CommandText = "update P_Product set Name='电脑2'
where Id=52";
myCommand.ExecuteNonQuery();
myCommand.CommandText = "update P_Product set Name='电脑3'
where Id=53";
myCommand.ExecuteNonQuery();
msg ="成功!";
}
catch (Exception ex)
{
msg = "失败:"+ex.Message;
}
finally
{
myConnection.Close();
}
return msg;
}
}
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: