Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

OleDbException Class

The exception that is thrown when the underlying provider returns a warning or error for an OLE DB data source. This class cannot be inherited.

For a list of all members of this type, see OleDbException Members.


[Visual Basic]
NotInheritable Public Class OleDbException
   Inherits ExternalException
public sealed class OleDbException : ExternalException
public __gc __sealed class OleDbException : public
class OleDbException extends ExternalException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


This class is created whenever the .NET Framework Data Provider for OLE DB encounters an error generated from the server. (Client side errors are thrown as standard common language runtime exceptions.) OleDbException always contains at least one instance of OleDbError.

If the severity of the error is too great, the server may close the OleDbConnection. However, the user can reopen the connection and continue.


[Visual Basic, C#, C++] The following example generates an OleDbException due to a missing data source, and then displays the exception.

[Visual Basic] 
Public Sub ShowOleDbException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New OleDbConnection _
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

    Catch e As OleDbException
        Dim errorMessages As String
        Dim i As Integer

        For i = 0 To e.Errors.Count - 1
            errorMessages += "Index #" & i.ToString() & ControlChars.Cr _
                           & "Message: " & e.Errors(i).Message & ControlChars.Cr _
                           & "NativeError: " & e.Errors(i).NativeError & ControlChars.Cr _
                           & "Source: " & e.Errors(i).Source & ControlChars.Cr _
                           & "SQLState: " & e.Errors(i).SQLState & ControlChars.Cr
        Next i

       Dim log As System.Diagnostics.EventLog = New System.Diagnostics.EventLog()
       log.Source = "My Application"
       Console.WriteLine("An exception occurred. Please contact your system administrator.")
    End Try
End Sub

public void ShowOleDbException() 
   string mySelectQuery = "SELECT column1 FROM table1";
   OleDbConnection myConnection =
      new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=");
   OleDbCommand myCommand = new OleDbCommand(mySelectQuery,myConnection);

   catch (OleDbException e) 
     string errorMessages = "";

     for (int i=0; i < e.Errors.Count; i++)
         errorMessages += "Index #" + i + "\n" +
                          "Message: " + e.Errors[i].Message + "\n" +
                          "NativeError: " + e.Errors[i].NativeError + "\n" +
                          "Source: " + e.Errors[i].Source + "\n" +
                          "SQLState: " + e.Errors[i].SQLState + "\n";

     System.Diagnostics.EventLog log = new System.Diagnostics.EventLog();
     log.Source = "My Application";
     Console.WriteLine("An exception occurred. Please contact your system administrator.");

 void ShowOleDbException() 
    String* mySelectQuery = S"SELECT column1 FROM table1";
    OleDbConnection* myConnection =
       new OleDbConnection(S"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=");
    OleDbCommand* myCommand = new OleDbCommand(mySelectQuery,myConnection);

    catch (OleDbException* e) 
      String* errorMessages = S"";

      for (int i=0; i < e->Errors->Count; i++)
         errorMessages = String::Concat(
            String::Format( S"{0}Index #{1}\nMessage: {2}\n",
               e->Errors->Item[i]->Message ),
            String::Format( S"NativeError: {0}\nSource: {1}\nSQLState: {2}\n",
               e->Errors->Item[i]->SQLState )

      System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
      log->Source = S"My Application";
      Console::WriteLine(S"An exception occurred. Please contact your system administrator.");

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Namespace: System.Data.OleDb

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Data (in System.Data.dll)

See Also

OleDbException Members | System.Data.OleDb Namespace | OleDbError | OleDbErrorCollection

© 2015 Microsoft