.net 里不是有BeginTrans();CommitTrans();RollbackTrans();么?

解决方案 »

  1.   

    public void RunOleDbTransaction(string myConnString)
    {
       OleDbConnection myConnection = new OleDbConnection(myConnString);
       myConnection.Open();   OleDbCommand myCommand = new OleDbCommand();
       OleDbTransaction myTrans;   // Start a local transaction
       myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);
       // Assign transaction 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)
       {
         myTrans.Rollback();
         Console.WriteLine(e.ToString());
         Console.WriteLine("Neither record was written to database.");
       }
       finally
       {
         myConnection.Close();
       }
    }
      

  2.   

    TO: wxqq2001(就让我用一生等待)
    谢谢。 但我使用了一个类封装了ADO操作。我可以让执行不满足条件时抛出一个EXCEPTION,有没有在页面上进行事务处理的语句。
      

  3.   

    事务有三个级别,存储过程级别,数据库级别和页面级别!
    CREATE  PROCEDURE dbo.xxxx
    ..............

    AS DECLARE @_IS_BARGAIN CHAR(1)
    --开始事务
    BEGIN TRAN JASON

    --更新
    UPDATE BID_ProductClass
    SET BARGAIN_TIME_START = @BARGAIN_TIME_START, 
          BARGAIN_TIME_END = @BARGAIN_TIME_END , @_IS_BARGAIN = IS_BARGAIN
    WHERE (ID = @ID)

    .......
    --提交事务
    IF @@ERROR <> 0
    GOTO ERRORHANDLE

    COMMIT TRAN JASON
        RETURN 0
    --回滚事务
    ERRORHANDLE:
    ROLLBACK TRAN JASON
            COMMIT TRAN JASON
    RETURN 1
    数据库级别的就是楼上的那样最后是页面级别的,导入名称空间system.enterpriseservices
    用静态方法
    在try的末尾用contextutil.setcomplete()
    在catch的末尾用contextuitl.setabort()
    如意这个名称控件不是默认的,必须添加引用,然后在codegehind里导入,vb:import c#:using
      

  4.   

    wxqq2001(就让我用一生等待) 和 zhanqiangz(闲云野鹤) 都可以。建议在数据库里使用事务。
      

  5.   

    到我的blog看看这篇文章:服务组件在业务逻辑中的应用
    http://blog.csdn.net/billy_zh/archive/2004/07/17/43748.aspx
      

  6.   

    .net framework文档里面搜索sqltransaction有源代码
      

  7.   

    到SQL里面写好事务,再进行操作不行吗?
      

  8.   

    1,SqlServer存储过程的事务处理
    一种比较通用的出错处理的模式大概如下:
    Create procdure prInsertProducts
    (
     @intProductId int,
     @chvProductName varchar(30),
     @intProductCount int
    )
    AS
    Declare @intErrorCode int
    Select @intErrorCode=@@Error
    Begin transaction
     if @intErrorCode=0
       begin
         -insert products
         insert products(ProductID,ProductName,ProductCount) 
         values(@intProductId,@chvProductName,@intProductCount)
         Select @intErrorCode=@@Error --每执行完一条t-sql语句马上进行检测,并把错误号保存到局部变量中
       end
     if @intErrorCode=0
       begin 
         -update products
         update products set ProductName='MicroComputer' where ProductID=5
         Select @intErrorCode=@@Error
       end
    if @intErrorCode=0
       commit transaction
    else
       rollback transaction Return @intErrorCode --最好返回错误代号给调用的存储过程或应用程序2,.Net中使用事务处理
    SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;"); 
    myConnection.Open(); SqlTransaction myTrans = myConnection.BeginTransaction(); //使用New新生成一个事务 
    SqlCommand myCommand = new SqlCommand(); 
    myCommand.Transaction = myTrans; try 

    myCommand.CommandText = "Update Address set location='23 rain street' where userid='0001'"; 
    myCommand.ExecuteNonQuery(); 
    myTrans.Commit(); 
    Console.WriteLine("Record is udated."); 

    catch(Exception e) 

    myTrans.Rollback(); 
    Console.WriteLine(e.ToString()); 
    Console.WriteLine("Sorry, Record can not be updated."); 

    finally 

    myConnection.Close(); 
    }
      

  9.   

    1、直接用myConnection.BeginTransaction
    2、我认为可以用存储过程,只是可能要复杂一点
      

  10.   

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

  11.   

    多谢各位。我的总结:对事务的处理最好的办法是把事务塞进存储过程中,不定参数可由XML传入。分数只有一点,只能一人几分。不好意思。