Dim cmd As New SqlCommand("insert into T_VisitReserve (full_name,entry_date) values (@p,getdate())", objcn)
        Try
            objcn.Open()
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            Exp = ex
        Finally
            objcn.Close()
        End Try
我想在上面的sql文中加roll back功能,该怎么加?

解决方案 »

  1.   

    trans=conn.BeginTransaction();
    cmd.Transaction=trans;
    cmd.ExecuteNonQuery();
    ....
    //数据库操作提交
    trans.Commit();
    conn.Close();
      

  2.   

    try
       trans=objn.BeginTransaction();
       cmd.Transaction=trans;
       cmd.ExecuteNonQuery();
       trans.Commit();
    Catch ex As Exception
                Exp = ex
            Finally
                objcn.Close()
            End Try
    这样吗?那rollback在哪的?
      

  3.   

    Finally
    rollback
                objcn.Close()
            End Try
      

  4.   

    try
       trans=objn.BeginTransaction();
       cmd.Transaction=trans;
       cmd.ExecuteNonQuery();
       trans.Commit();
    Catch ex As Exception
                Exp = ex
            Finally
                rollback
                objcn.Close()
            End Try
    是这样吗?
      

  5.   

    try
    {
    trans=conn.BeginTransaction();
    cmd.Transaction=trans;
    cmd.ExecuteNonQuery();
    ....
    //数据库操作提交
    trans.Commit();
    conn.Close();
    }
    catch(Exception se)
    {
    errorMessage=se.Message;
    trans.Rollback();
    conn.Close();
    return false;
    }
      

  6.   

    objn后面我怎么点不出BeginTransaction方法呀?
      

  7.   

    可以使用 Connection 和 Transaction 对象启动、提交和回滚事务。下面的步骤用于执行事务。
    若要执行事务,请执行下列操作:
     
    1、调用 Connection 对象的 BeginTransaction 方法来标记事务的开始。BeginTransaction 方法返回对 Transaction 的引用。该引用将分配给登记在事务中的 Command 对象。
     
    2、将 Transaction 对象分配给要执行的 Command 的 Transaction 属性。如果通过活动的 Transaction 对象对 Connection 执行 Command,但该 Transaction 对象尚未分配给 Command 的 Transaction 属性,则将引发异常。 3、执行所需的命令。 4、调用 Transaction 对象的 Commit 方法来完成事务,或调用 Rollback 方法来取消事务。 
    以下代码示例使用 Microsoft® SQL Server™ 上的 ADO.NET 来演示事务逻辑。[Visual Basic]
    Dim myConnection As SqlConnection = New SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;")
    myConnection.Open()' Start a local transaction.
    Dim myTrans As SqlTransaction = myConnection.BeginTransaction()' Enlist the command in the current transaction.
    Dim myCommand As SqlCommand = myConnection.CreateCommand()
    myCommand.Transaction = myTransTry
      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 e As Exception
      Try
        myTrans.Rollback()
      Catch ex As SqlException
        If Not myTrans.Connection Is Nothing Then
          Console.WriteLine("An exception of type " & ex.GetType().ToString() & _
                            " was encountered while attempting to roll back the transaction.")
        End If
      End Try  Console.WriteLine("An exception of type " & e.GetType().ToString() & _
                        "was encountered while inserting the data.")
      Console.WriteLine("Neither record was written to database.")
    Finally
      myConnection.Close()
    End Try
    [C#]
    SqlConnection myConnection = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;Integrated Security=SSPI;");
    myConnection.Open();// Start a local transaction.
    SqlTransaction myTrans = myConnection.BeginTransaction();// Enlist the command in the current transaction.
    SqlCommand myCommand = myConnection.CreateCommand();
    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();
    }