本文參照下列 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 提供者不允許巢式交易。
其他相關資訊
重現行為的步驟
-
啟動 Microsoft Visual Studio .NET。
-
在 Windows 中建立 Visual Basic 應用程式專案。 表單 1 預設為建立。
-
按兩下 [表單1》 以開啟 [程式碼> 視窗。
-
以下列程式碼取代 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 -
請根據環境修改連接字串。
-
執行應用程式。 請注意,您收到「症狀」一節中所列的錯誤。
參考
若要瞭解詳細資訊,請按一下下方的文章編號,以在 Microsoft 知識庫中查看文章:
306649 PRB:當您使用 OLE DB 提供者為提供者套用巢SQL錯誤