-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Description
I am converting a commercial product from .NET Framework 4.7 to .NET 8. Some of our unit tests are failing and the cause seems to be an unexpected behaviour difference in the System.Data.OleDb.OleDbException object. After deliberately executing invalid SQL, the Errors property on the resulting exception is an empty collection instead of containing the expected OleDbError object.
In production, we use the OleDbError.NativeError number to handle errors coming from SQL Server differently depending on e.g. whether the error reflects a temporary connection issue as opposed to a syntax error.
Reproduction Steps
Install Visual Studio with the .NET desktop and data storage workloads
Create a VB.NET console project
Add the System.Data.OleDb 8.0.0 package and import the namespace
Add the following code to Main():
Using objConn As New OleDbConnection()
objConn.ConnectionString = "Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="""";Data Source=(localdb)\MSSQLLocalDB;Initial File Name="""";Server SPN="""""
objConn.Open()
Using dbcComm As New OleDbCommand("select * from NonExistentTable", objConn)
Try
dbcComm.ExecuteNonQuery()
Catch ex As OleDbException
Console.WriteLine($"{ex.Errors(0).NativeError}: {ex.Errors(0).Message}")
End Try
End Using
End UsingTwo test case solutions (one for .NET 8, one for .NET Framework 4.7.2):
OleDbErrorTestCase.zip
Expected behavior
Should output the following line:
208: Invalid object name 'NonExistentTable'.
Actual behavior
Throws an exception because Errors is an empty collection:
System.ArgumentOutOfRangeException
HResult=0x80131502
Message=Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Source=System.Private.CoreLib
StackTrace:
at System.Collections.ArrayList.get_Item(Int32 index)
at System.Data.OleDb.OleDbErrorCollection.get_Item(Int32 index)
at OleDbErrorTestCase_NET8.Program.Main(String[] args) in C:\Users\jkerk\source\repos\OleDbErrorTestCase_NET8\OleDbErrorTestCase_NET8\Program.vb:line 12
Regression?
Same code works as expected in .NET Framework 4.7.2.
Known Workarounds
No response
Configuration
Visual Studio Enterprise 2022 17.9.5
.NET 8.0
System.Data.OleDb 8.0.0
Windows 10 x64
Same behaviour was observed with a SQL Server 2019 instance so I don't think the use of MSSQLLocalDB in the test case is relevant.
Other information
The observed behaviour is contrary to the documentation, which states "The OleDbErrorCollection class always contains at least one instance of the OleDbError class."