Export (0) Print
Expand All

OdbcException Class

Note: This namespace, class, or member is supported only in version 1.1 of the .NET Framework.

The exception that is generated when a warning or error is returned by an ODBC data source. This class cannot be inherited.

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

System.Object
   System.Exception
      System.SystemException
         System.Data.Odbc.OdbcException

[Visual Basic]
<Serializable>
NotInheritable Public Class OdbcException
   Inherits SystemException
[C#]
[Serializable]
public sealed class OdbcException : SystemException
[C++]
[Serializable]
public __gc __sealed class OdbcException : public SystemException
[JScript]
public
   Serializable
class OdbcException extends SystemException

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 OdbcDataAdapter encounters an error generated by the server (Client-side errors are raised as standard common language runtime exceptions.). It always contains at least one instance of OdbcError.

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

Example

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

[Visual Basic] 
Public Sub ShowOdbcException()
    Dim mySelectQuery As String = "SELECT column1 FROM table1"
    Dim myConnection As New OdbcConnection _
       ("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;")
    Dim myCommand As New OdbcCommand(mySelectQuery, myConnection)
    Try
        myCommand.Connection.Open()
    Catch e As OdbcException
        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.ToString() & ControlChars.Cr _
                           & "Source: " & e.Errors(i).Source & ControlChars.Cr _
                           & "SQL: " & 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 ShowOdbcException() 
{
   string mySelectQuery = "SELECT column1 FROM table1";
   OdbcConnection myConnection =
      new OdbcConnection("DRIVER={SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;");
   OdbcCommand myCommand = new OdbcCommand(mySelectQuery,myConnection);
   try 
   {
      myCommand.Connection.Open();
   }
   catch (OdbcException 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.ToString() + "\n" +
                          "Source: " + e.Errors[i].Source + "\n" +
                          "SQL: " + 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 ShowOdbcException()
    {
        String* mySelectQuery = S"SELECT column1 FROM table1";
        OdbcConnection* myConnection = new OdbcConnection(S"DRIVER= {SQL Server};SERVER=MyServer;Trusted_connection=yes;DATABASE=northwind;");
        OdbcCommand* myCommand = new OdbcCommand(mySelectQuery, myConnection);
        try
        {
            myCommand->Connection->Open();
        }
        catch (OdbcException* e)
        {
            String* errorMessages = S"";

            for (int i=0; i < e->Errors->Count; i++)
            {
                errorMessages = String::Concat(
                    S"Index #: ", __box(i), S"\n",
                    S"Message: ", e->Errors->Item[i]->Message, S"\n",
                    S"NativeError: ", e->Errors->Item[i]->NativeError, S"\n",
                    S"Source: ", e->Errors->Item[i]->Source, S"\n",
                    S"SQL: ", e->Errors->Item[i]->SQLState, S"\n");
            }

            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.Odbc

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

OdbcException Members | System.Data.Odbc Namespace | OdbcError | OdbcErrorCollection

Show:
© 2014 Microsoft