VB.NET 中启动ADO.NET事务,实现对数据库操作的整体性。

翻译 2006年05月21日 08:50:00

示例
[Visual Basic, C#] 下面的示例创建一个 SqlConnection 和一个 SqlTransaction。它还演示如何使用 BeginTransaction、Commit 和 Rollback 方法。
[Visual Basic, C#] 注意   此示例显示如何使用 BeginTransaction 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。
[Visual Basic]
Public Sub RunSqlTransaction(myConnString As String)
    Dim myConnection As New SqlConnection(myConnString)
    myConnection.Open()
   
    Dim myCommand As SqlCommand = myConnection.CreateCommand()
    Dim myTrans As SqlTransaction
   
    ' Start a local transaction
    myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted, "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 e As Exception
      Try
        myTrans.Rollback("SampleTransaction")
      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
End Sub 'RunSqlTransaction
[C#]
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(IsolationLevel.ReadCommitted,"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();
    }
}

举报

相关文章推荐

C# ADO.NET_数据库操作的封装

1.App.config                             providerName="Syst...

关于ADO.NET数据库操作中的增删改查讲解

我们要连接数据库之后就要对它进行增删改查,这里我们就详细的分析一下吧。对于ADO.NET数据库操作Insert,Update,Delete等单向操作,对于插入、删除、修改等操作,由于是客户端应用程序向...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

我封装的ADO.NET对数据库操作经典类

using System;using System.Collections;using System.Collections.Specialized;using System.Runtime.Remo...

我封装的ADO.NET对数据库操作经典类

Posted on 2005-12-03 00:18 李天平 阅读(7467) 评论(11) 编辑 收藏 using System; using System.Coll...

ADO.NET通用数据库操作类

最近经常看到一些人在秀其多年来积累经验而成的数据库操作类,在此我也秀一把。 本人对ORM相关的东西是极不喜欢的,可能与平常接触到的项目所操作的数据量都较大、业务较复杂吧。另对那种通过自己编写实体类,...

封装的ADO.NET对数据库操作经典类 代码

using System;using System.Collections;using System.Collections.Specialized;using System.Runtime.Remo...
  • zz_mm
  • zz_mm
  • 2011-07-11 10:52
  • 557
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)