JSFK_365的专栏

努力!努力!再努力!学习!学习!再学习!

JSFK_365
最近评论
文章分类
收藏
    相册
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    转载 C#.net事务处理代码收藏

    新一篇: 在select语句中增加行号 | 旧一篇: c++ TADOConnection TADOQuery TDataSetProvider TClientDataSet 动态创建

    public   void   RunSqlTransaction(string   myConnString)  
        {  
              SqlConnection   myConnection   =   new   SqlConnection(myConnString);  
              myConnection.Open();  
       
              SqlCommand   myCommand   =   myConnection.CreateCommand();  
              SqlTransaction   myTrans;  
       
              //   Start   a   local   transaction  
              myTrans   =   myConnection.BeginTransaction("SampleTransaction");  
              //   Must   assign   both   transaction   object   and   connection  
              //   to   Command   object   for   a   pending   local   transaction  
              myCommand.Connection   =   myConnection;  
              myCommand.Transaction   =   myTrans;  
       
              try  
              {  
                  myCommand.CommandText   =   "Insert   into   Region   (RegionID,   RegionDescription)   VALUES   (100,   'Description')";  
                  myCommand.ExecuteNonQuery();  
                  myCommand.CommandText   =   "Insert   into   Region   (RegionID,   RegionDescription)   VALUES   (101,   'Description')";  
                  myCommand.ExecuteNonQuery();  
                  myTrans.Commit();  
                  Console.WriteLine("Both   records   are   written   to   database.");  
              }  
              catch(Exception   e)  
              {  
                  try  
                  {  
                      myTrans.Rollback("SampleTransaction");  
                  }  
                  catch   (SqlException   ex)  
                  {  
                      if   (myTrans.Connection   !=   null)  
                      {  
                          Console.WriteLine("An   exception   of   type   "   +   ex.GetType()   +  
                                                              "   was   encountered   while   attempting   to   roll   back   the   transaction.");  
                      }  
                  }  
               
                  Console.WriteLine("An   exception   of   type   "   +   e.GetType()   +  
                                                      "   was   encountered   while   inserting   the   data.");  
                  Console.WriteLine("Neither   record   was   written   to   database.");  
              }  
              finally    
              {  
                  myConnection.Close();  
              }  
       

    发表于 @ 2007年06月19日 17:21:00|评论(loading...)|编辑

    新一篇: 在select语句中增加行号 | 旧一篇: c++ TADOConnection TADOQuery TDataSetProvider TClientDataSet 动态创建

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © JSFK_365