1、手动提交

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.EnterpriseServices;

namespace prjEnterprise
{
    [Transaction(TransactionOption.Required)]
    public class clsES : System.EnterpriseServices.ServicedComponent
    {
        public SqlConnection Conn;

        public void dbAccess(int pID1, int nScore)
        {
            try
            {
                SqlConnection Conn = new SqlConnection("user id=sa;password=;Initial Catalog=myDataBase;Data Source=.;");
                Conn.Open();
                SqlCommand sqlCommand = new SqlCommand("UPDATE ComAction SET userScore = " + nScore + " WHERE userID = " + pID1, Conn);
                sqlCommand.ExecuteNonQuery();

                ContextUtil.SetComplete();
                Conn.Close();
            }
            catch (Exception e)
            {
                ContextUtil.SetAbort();

                throw e;
            }
            finally
            {

            }
        }
    }
}

2、自动提交

在数据记录部分为事务性操作,一条失败全部回滚。一上方法的缺点就不用说了,总之是个没头脑的方法,面向对象,应该申请课程相关的方法在课程类中实现,费用操作的方法在费用类中实现,应用层在一个申请课程的方法中调用相关类,但有个问题,如果面向对象则事务如何实现,要知道以上三条SQL语句应该是要不都成功如果有一句不成功应该全部回滚,那应该怎么实现看如下代码:


using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.EnterpriseServices;
namespace test
{
 /** <summary>
 /// 客户类
/// </summary>
public class Client
{
 //客户的申请选课方法
 private void Apply()
 {
  //实例化过程类
  MyCourse myCourse=new MyCourse();
  //过程中加入一个课程类
   myCourse.Course.Add(new Elective());
   //过程中加入一个费用类
   myCourse.Course.Add(new Tuition());
   //实例化事务类
  MyTransaction mTransaction=new MyTransaction();
   try
   {
   //用事务类的提交方法提交过程类,过程类中的任何方法异常将导致回滚
    mTransaction.MyCommit(myCourse);
   }
   catch(Exception e)
  {
     Console.WriteLine(e.Message);
  }
   }
 }

 /** <summary>
 /// 费用类
 /// </summary>
 public class Tuition:Iwork
 {
  //实现费用相关的数据库操作
  public void Working()
 {
  OleDbConnection oConn=new OleDbConnection("server=localhost");
  oConn.Open();
  OleDbCommand oCmd=new OleDbCommand();
  oCmd.Connection=oConn;
 oCmd.CommandText="insert into table1 values('test',1)";
 oCmd.ExecuteNonQuery();
 oCmd.CommandText="update table2 set ttt='test'";
 oCmd.ExecuteNonQuery();
 oConn.Close();
}
 }
/** <summary>
 /// 选课类
/// </summary>
public class Elective:Iwork
{
 //实现选课相关的数据库操作
 public void Working()
 {
  OleDbConnection oConn=new OleDbConnection("server=localhost");
  oConn.Open();
  OleDbCommand oCmd=new OleDbCommand();
  oCmd.Connection=oConn;
   oCmd.CommandText="insert into table3 values('test',10)";
   oCmd.ExecuteNonQuery();
  oConn.Close();
 }
}

/** <summary>
/// 事务类
 /// </summary>
 [Transaction(TransactionOption.Required)]
 public class MyTransaction:ServicedComponent
 {
 //提交方法,AutoComplete属性指定异常自动回滚
 [AutoComplete]
  public void MyCommit(MyCourse l_mCourse)
 {
    l_mCourse.MyRuning();
 }
}
/** <summary>
/// 过程类
/// </summary>
 public class MyCourse
 {
  private ArrayList alCourse;
 public MyCourse()
 {
   alCourse=new ArrayList();
  }
  //保存代调用类
  public ArrayList Course
   {
  get
   {
     return alCourse;
  }
  }
 //运行Course中类的Wroking方法
  public void MyRuning()
 {
   foreach(object o in alCourse)
  {
    if(o is Iwork)
    {
    Iwork iw=(Iwork)o;
    iw.Working();
  }
  } 
 }
}
interface Iwork
{
 void Working();
 }
}

 

以上代码实现用Com+中的事务处理实现我们想要的方法。