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.

System.Object
   System.Exception
      System.SystemException
         System.Runtime.InteropServices.ExternalException
            System.Data.OleDb.OleDbException

[Visual Basic]
<Serializable>
NotInheritable Public Class OleDbException
   Inherits ExternalException
[C#]
[Serializable]
public sealed class OleDbException : ExternalException
[C++]
[Serializable]
public __gc __sealed class OleDbException : public
   ExternalException
[JScript]
public
   Serializable
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.

Remarks

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.

Example

[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 _
       ("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=")
    Dim myCommand As New OleDbCommand(mySelectQuery, myConnection)

    Try
        myCommand.Connection.Open()
    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"
       log.WriteEntry(errorMessages)
       Console.WriteLine("An exception occurred. Please contact your system administrator.")
    End Try
End Sub

[C#] 
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);

   try 
   {
      myCommand.Connection.Open();
   }
   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";
     log.WriteEntry(errorMessages);
     Console.WriteLine("An exception occurred. Please contact your system administrator.");
   }
}

[C++] 
public:
 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);

    try 
    {
       myCommand->Connection->Open();
    }
    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",
               errorMessages,
               __box(i),
               e->Errors->Item[i]->Message ),
            String::Format( S"NativeError: {0}\nSource: {1}\nSQLState: {2}\n",
               __box(e->Errors->Item[i]->NativeError),
               e->Errors->Item[i]->Source,
               e->Errors->Item[i]->SQLState )
         );
      }

      System::Diagnostics::EventLog* log = new System::Diagnostics::EventLog();
      log->Source = S"My Application";
      log->WriteEntry(errorMessages);
      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.

Requirements

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

Show:
© 2015 Microsoft