Edit

Share via


OdbcException Class

Definition

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

public ref class OdbcException sealed : System::Data::Common::DbException
public ref class OdbcException sealed : SystemException
public sealed class OdbcException : System.Data.Common.DbException
[System.Serializable]
public sealed class OdbcException : SystemException
[System.Serializable]
public sealed class OdbcException : System.Data.Common.DbException
type OdbcException = class
    inherit DbException
[<System.Serializable>]
type OdbcException = class
    inherit SystemException
[<System.Serializable>]
type OdbcException = class
    inherit DbException
Public NotInheritable Class OdbcException
Inherits DbException
Public NotInheritable Class OdbcException
Inherits SystemException
Inheritance
Inheritance
Attributes

Examples

The following example generates an OdbcException because of a missing data source, and then displays the exception.

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.");
   }
}
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 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

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.

For general information about handling exceptions for a .NET Framework data provider, see SqlException.

Properties

BatchCommand

If this DbException was thrown when executing a DbBatch, references the specific DbBatchCommand which triggered the exception.

(Inherited from DbException)
Data

Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
DbBatchCommand

When overridden in a derived class, if this DbException was thrown when executing a DbBatch, references the specific DbBatchCommand which triggered the exception.

(Inherited from DbException)
ErrorCode

Gets the HRESULT of the error.

(Inherited from ExternalException)
Errors

Gets a collection of one or more OdbcError objects that give detailed information about exceptions generated by the .NET Framework Data Provider for ODBC.

HelpLink

Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult

Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException

Gets the Exception instance that caused the current exception.

(Inherited from Exception)
IsTransient

Indicates whether the error represented by this DbException could be a transient error, i.e. if retrying the triggering operation may succeed without any other change.

(Inherited from DbException)
Message

Gets a message that describes the current exception.

Message

Gets a message that describes the current exception.

(Inherited from Exception)
Source

Gets the name of the ODBC driver that generated the error.

SqlState

For database providers which support it, contains a standard SQL 5-character return code indicating the success or failure of the database operation. The first 2 characters represent the class of the return code (e.g. error, success), while the last 3 characters represent the subclass, allowing detection of error scenarios in a database-portable way.

For database providers which don't support it, or for inapplicable error scenarios, contains null.

(Inherited from DbException)
StackTrace

Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite

Gets the method that throws the current exception.

(Inherited from Exception)

Methods

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException()

When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext)
Obsolete.

This member overrides GetObjectData(SerializationInfo, StreamingContext).

GetType()

Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
ToString()

Returns a string that contains the HRESULT of the error.

(Inherited from ExternalException)
ToString()

Creates and returns a string representation of the current exception.

(Inherited from Exception)

Events

SerializeObjectState
Obsolete.

Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

Applies to

See also