• λ我爱Aspx >> Asp.Net >> 在C#中使用COM+实现事务控制
  • 在C#中使用COM+实现事务控制

  • :未知  Դ:非寒日志  :2007-4-21 0:45:29  ؼ:c#,com,com+
  • private static string add2 = "insert into sample values('test1',22)";

    //the error sql statement

    //there is not table “sample”

    public TxCfgClass() {}

    private void ExecSQL(string init, string sql)

    {

    SqlConnection conn = new SqlConnection(init);

    SqlCommand cmd = conn.CreateCommand();

    cmd.CommandText = sql;

    conn.Open();

    cmd.ExecuteNonQuery();

    conn.Close();

    }

    //添加一条记录到数据库

    public void Add()

    {

    try

    {

    //在一数据库中插入一条记录

    ExecSQL(init1, add1);

    Console.WriteLine("the operation in the same database completely");

    //在另外一个数据库中插入两条记录

    //这次执行的是一个错误的SQL语句

    ExecSQL(init2, add2);

    Console.WriteLine("the operation in the other database

    completely");

    Console.WriteLine("Record(s) added, press enter...");

    Console.Read();

    }

    catch(Exception e)

    {

    //事务回滚

    ContextUtil.SetAbort();

    Console.WriteLine("Because there are some errors in the operation ,so transcation abort");

    Console.WriteLine("The error is " + e.Message);

    Console.WriteLine("abort successfully");

    Console.Read();

    }

    }

    }

    }

    2:程序说明:

    添加命名空间 using System.EnterpriseServices;因为本程序使用了其中的ContextUtil类

    [ Transaction(TransactionOption.Required) ] 说明DLL需要事务支持。

    本程序的TxCfgClass 类从ServicedComponent类中继承,这样并不会影响该类,而只是在该类中添加了两个额外的方法,这两个方法可以使代码共享变得更加容易。

    Ҷƪл˵?
  • һƪ幸福的人不看结局
    һƪ使用C#调用外部Ping命令获取网络连接情况