以下代码有什么问题?怎么才能让事务回滚??
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();        }
        
    }    //如果事务存在则共享事务
    [Transaction(TransactionOption.Supported)]
    public class Test : ServicedComponent     //所有COM+服务的基类
    {
        //自动事务处理属性
        [AutoComplete]
        public void add1()
        {
            SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=sa");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into TestTable values(1,'张三')", con);
            cmd.ExecuteNonQuery();        }
        //自动事务处理属性
        [AutoComplete]
        public void add2()
        {
            SqlConnection con = new SqlConnection("server=.;database=Test;uid=sa;pwd=sa");
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into TestTables values(2,'张四')", con);
            cmd.ExecuteNonQuery();        }
    }
}

解决方案 »

  1.   


    把  public class Program 也加上 [Transaction(TransactionOption.Supported)] 应该写在Main中
     static void Main(string[] args) 
            { 
                try
                {
                Test test = new Test(); 
                test.add1(); 
                test.add2(); 
                ContextUtil.SetComplete();
                }
                catch()
                { ContextUtil.SetAbort();}        } 
      

  2.   

    使用TransactionScope 吧,比COM+要好
    如下示例:static void Main(string[] args)
    {
    //如果存在事务则使用该事务,不存在则创建新事务
    using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required))
    {
    try
    {
    Program p = new Program();
    p.add1();
    p.add2();
    //没有异常则提交
    scope.Complete();
    Console.WriteLine("插入成功");
    Console.ReadLine();
    }
    catch (Exception)
    {
    //出现异常则回滚
    scope.Dispose();
    Console.WriteLine("插入失败");
    Console.ReadLine();
    }
    }}
      

  3.   

    需要把它做成com+ 这个是什么意思?请解释清楚下,谢谢,或者给个详细地址