注意:
1、已经完成程序集强名称
2、已经成功将程序集注册进COM+应用程序当中
3、程序正常运行
就是无法回滚~~~有AutoComplete属性后,里面的方法还要加try...catch吗?using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.EnterpriseServices;namespace Project
{
public class Program
{ static void Main(string[] args)
{
Test test = new Test();
test.add1();
test.add2();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.EnterpriseServices;
using System.Data.SqlClient;namespace Project
{
//如果事务存在则共享事务
[Transaction(TransactionOption.Required)]
public class Test : System.EnterpriseServices.ServicedComponent //所有COM+服务的基类
{
//自动事务处理属性
[AutoComplete]
public void add1()
{
SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456");
con.Open();
SqlCommand cmd = new SqlCommand("insert into TestTable values(1,'张三')", con);
cmd.ExecuteNonQuery();
ContextUtil.SetComplete();
}
//自动事务处理属性
[AutoComplete]
public void add2()
{
SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456");
con.Open();
//这里将发生异常,故意写错表名
SqlCommand cmd = new SqlCommand("insert into TestTables values(2,'张四')", con);
cmd.ExecuteNonQuery();
ContextUtil.SetComplete(); }
}
}
1、已经完成程序集强名称
2、已经成功将程序集注册进COM+应用程序当中
3、程序正常运行
就是无法回滚~~~有AutoComplete属性后,里面的方法还要加try...catch吗?using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.EnterpriseServices;namespace Project
{
public class Program
{ static void Main(string[] args)
{
Test test = new Test();
test.add1();
test.add2();
Console.ReadLine();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.EnterpriseServices;
using System.Data.SqlClient;namespace Project
{
//如果事务存在则共享事务
[Transaction(TransactionOption.Required)]
public class Test : System.EnterpriseServices.ServicedComponent //所有COM+服务的基类
{
//自动事务处理属性
[AutoComplete]
public void add1()
{
SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456");
con.Open();
SqlCommand cmd = new SqlCommand("insert into TestTable values(1,'张三')", con);
cmd.ExecuteNonQuery();
ContextUtil.SetComplete();
}
//自动事务处理属性
[AutoComplete]
public void add2()
{
SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=123456");
con.Open();
//这里将发生异常,故意写错表名
SqlCommand cmd = new SqlCommand("insert into TestTables values(2,'张四')", con);
cmd.ExecuteNonQuery();
ContextUtil.SetComplete(); }
}
}
{
}
catch
{
ContextUtil.SetAbort();
throw;
}
finally
{
ServiceDomain.Leave();
}
http://www.cnblogs.com/jonescheng/archive/2008/07/22/1249043.html
static void Main(string[] args)
{
try
{
Test test = new Test();
test.add1();
test.add2();
ContextUtil.SetComplete();
Console.ReadLine(); }
catch
{
ContextUtil.SetAbort();
}
}
你那两个方法里面就不要写事务的东西了,在执行的方法里面控制
如果你是.Net 2.0的话就用TransactionScope,不要用COM+了,TransactionScope已经集合了Transaction与COM+的优点
用户根本不需要考虑是简单事务还是分布式事务. 新模型会自动根据事务中涉及的对象资源判断使用何种事务管理器. 简而言之, 对于任何的事务用户只要使用同一种方法进行处理示例代码:using(TransactionScope scope = new TransactionScope())
{
/**//* Perform transactional work here */
//No errors - commit transaction
scope.Complete();
}另外对嵌套事务和事务的隔离级别也提供了支持using(TransactionScope scope1 = new TransactionScope())
//Default is Required
{
using(TransactionScope scope2 = new
TransactionScope(TransactionScopeOption.Required))
{}
using(TransactionScope scope3 = new
TransactionScope(TransactionScopeOption.RequiresNew))
{}
using(TransactionScope scope4 = new
TransactionScope(TransactionScopeOption.Suppress))
{}}TransactionScopeOption属性作用可以看看MSDN