CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  ASP.NET

DataAdapter做update更新时能够使用事务吗?

楼主macd004()2005-06-19 23:50:00 在 .NET技术 / ASP.NET 提问

最好附源码 谢谢各位 问题点数:100、回复次数:8Top

1 楼fancyf(凡瑞)回复于 2005-06-19 23:55:32 得分 20

可以在指定DataAdapter.xxxCommand的时候指定事务:  
  dataAdapter.InsertCommand   =   new   SqlCommand("text",   conn,   transaction);Top

2 楼boytomato(深爱一人叫颖的女孩!)回复于 2005-06-20 06:30:57 得分 80

public   void   RunOdbcTransaction(string   myConnString)  
  {  
        OdbcConnection   myConnection   =   new   OdbcConnection(myConnString);  
        myConnection.Open();  
   
        OdbcCommand   myCommand   =   myConnection.CreateCommand();  
        OdbcTransaction   myTrans;  
   
        //   Start   a   local   transaction  
        myTrans   =   myConnection.BeginTransaction(IsolationLevel.ReadCommitted);  
        //   Assign   transaction   object   for   a   pending   local   transaction  
        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();  
        }  
        catch   (OdbcException   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();  
    }  
  }  
  Top

3 楼boytomato(深爱一人叫颖的女孩!)回复于 2005-06-20 06:31:31 得分 0

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();  
          //   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();  
              }  
              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();  
          }  
  }  
  Top

4 楼boytomato(深爱一人叫颖的女孩!)回复于 2005-06-20 07:24:15 得分 0

其实基本上都一样的...  
   
  System.Data.SqlClient.SqlConnection   mCn  
          =new   System.Data.SqlClient.SqlConnection("...");  
        System.Data.SqlClient.SqlDataAdapter   mDa  
          =new   System.Data.SqlClient.SqlDataAdapter("...",mCn);  
        System.Data.SqlClient.SqlTransaction   mTr  
          =mCn.BeginTransaction();  
        try  
        {  
           
          /  
  /...............  
          mDa.UpdateCommand.Transaction=   mTr;     //这一句很关键。。      
   
      mDa.Update(...);  
          mTr.Commit();  
        }  
        catch  
        {  
          mTr.Rollback();  
        }  
   
  Top

5 楼boytomato(深爱一人叫颖的女孩!)回复于 2005-06-20 07:26:15 得分 0

 
  你也可以在存贮过程中直接这样写...  
   
  BEGIN   TRANS  
  DECLARE     @orderDetailsDrror   int,@productError   int    
  DELETE   FROM   "order   Details"   where   ProdcutID=42  
  Select   @orderDetailsError==@@ERROR  
  DELETE   FROM   Products   where   ProdutId=42  
  Select   @ProductError=@@ERROR  
  if   @orderDetailsError=0   AND   @productError=0  
  COMMIT   TRANS  
  ELSE  
  ROLLBACK   TRANSTop

6 楼chx_xuxu(逍遥客)回复于 2005-06-20 08:01:28 得分 0

顶Top

7 楼macd004()回复于 2005-06-20 12:08:47 得分 0

太感谢boytomato(深爱一人叫颖的女孩!)     绝对够用啦Top

8 楼lovedogdog(天目湖鱼头)回复于 2005-09-05 11:58:04 得分 0

markTop

相关问题

  • 在用DataAdapter更新数据时如何进行事务处理较好?
  • 菜鸟问题:DataAdapter在Update更新时出现的错误
  • 请教 事务更新问题
  • 请问在更新数据库操作,如何实现事务。
  • 如何应用事务进行多表更新?
  • DataAdapter 更新数据时候出错
  • 在事务中,往数据库批次更新多条记录,如何再取得更新的记录?
  • 如何对DataAdapter的updata()方法进行事务处理
  • 如何保证主表/明细表的更新在同一事务内完成??
  • 事务是否只于数据的更新有关,而与查询无关呢?

关键词

  • producterror
  • dataadapter
  • mycommand
  • mytrans
  • myconnection
  • transaction
  • commit

得分解答快速导航

  • 帖主:macd004
  • fancyf
  • boytomato

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo