Felmeddelande visas när du använder OLE DB-provider för SQL Server i ADO.NET: "Det går inte att starta fler transaktioner i den här sessionen"

Den här artikeln refererar till följande Namnområden för Microsoft .NET Framework Klassbibliotek:

  • System.Data
  • System.Data.OleDb
  • System.Data.SqlClient

Symptom

Om du använder Microsoft OLE DB-providern för SQL Server (OLEDBSQL) i ADO.NET får du följande felmeddelande om du försöker starta fler än en transaktion i en session:

Obs

System.Data.OleDb.OleDbException: Det går inte att starta fler transaktioner i den här sessionen

Orsak

Som standard tillåter OLE DB-providern för SQL Server inte kapslade transaktioner.

Mer information

Steg för att reproducera beteendet

  1. Starta Microsoft Visual Studio .NET.

  2. Skapa ett Windows-programprojekt i Visual Basic .NET. Formulär1 skapas som standard.

  3. Dubbelklicka på Formulär1 för att öppna fönstret Kod.

  4. Ersätt koden i Formulär1 med följande kod:

    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. Ändra anslutningssträng efter behov för din miljö.

  6. Kör programmet. Observera att du får felet som visas i avsnittet Symptom.

Referenser

Om du vill ha mer information klickar du på artikelnumret nedan för att visa artikeln i Microsoft Knowledge Base:

306649 PRB: Fel när du implementerar kapslad transaktion med OLE DB-provider för SQL-provider