使用 Microsoft 登录
登录或创建帐户。
你好,
使用其他帐户。
你有多个帐户
选择要登录的帐户。

本文参考了以下 Microsoft .NET Framework类库命名空间:

  • System.Data

  • System.Data.OleDb

  • System.Data.SqlClient

症状

在 ADO.NET 中为 SQL SERVER (OLEDBSQL) 使用 Microsoft OLE DB 提供程序时,如果尝试在会话中启动多个事务,将收到以下错误消息:

System.Data.OleDb.OleDbException:无法在此会话中启动更多事务

原因

根据设计,用于 SQL Server 的 OLE DB 提供程序不允许嵌套事务。

更多信息

重现行为的步骤

  1. 启动 Microsoft Visual Studio .NET。

  2. 在 .NET Windows创建 Visual Basic 应用程序项目。 默认情况下创建 Form1。

  3. 双击"Form1"打开"代码"窗口。

  4. 将 Form1 中的代码替换为以下代码:

    Imports System.Data
    Imports System.Data.OleDb
    Imports System.Data.SqlClient

    Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim cn As New OleDb.OleDbConnection()
    Dim cmd As New OleDb.OleDbCommand()
    Dim mycmd As New OleDb.OleDbCommand()

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.Container

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.Name = "Form1"
    Me.Text = "Form1"

    End Sub

    #End Region

    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    cn.ConnectionString = "Provider=SQLOLEDB;Data Source=YourServer;" & _
    "Initial Catalog=pubs;User ID=YourUserID;Password=YourPassword"
    cn.Open()
    cmd.Connection = cn
    mycmd.Connection = cn
    cmd.CommandText = "Insert into stores (stor_id) values('9876')"
    Dim myTrans As OleDb.OleDbTransaction
    myTrans = cn.BeginTransaction(IsolationLevel.ReadCommitted)

    cmd.Transaction = myTrans

    Try
    cmd.ExecuteNonQuery()

    myTrans.Begin()
    myTrans.Commit()
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try

    End Sub
    End Class
  5. 根据环境修改连接字符串。

  6. 运行应用程序。 请注意,收到"症状"部分中列出的错误。

参考

有关其他信息,请单击以下文章编号,查看 Microsoft 知识库中的文章:

306649 PRB:使用适用于提供程序的 OLE DB 提供程序实现嵌套SQL错误

需要更多帮助?

需要更多选项?

了解订阅权益、浏览培训课程、了解如何保护设备等。

社区可帮助你提出和回答问题、提供反馈,并听取经验丰富专家的意见。

此信息是否有帮助?

你对语言质量的满意程度如何?
哪些因素影响了你的体验?
按“提交”即表示你的反馈将用于改进 Microsoft 产品和服务。 你的 IT 管理员将能够收集此数据。 隐私声明。

谢谢您的反馈!

×