Export (0) Print
Expand All

OdbcException Class

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

Namespace: System.Data.Odbc
Assembly: System.Data (in system.data.dll)

[SerializableAttribute] 
public sealed class OdbcException : DbException
/** @attribute SerializableAttribute() */ 
public final class OdbcException extends DbException
SerializableAttribute 
public final class OdbcException extends DbException

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.

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


System.Object
   System.Exception
     System.SystemException
       System.Runtime.InteropServices.ExternalException
         System.Data.Common.DbException
          System.Data.Odbc.OdbcException

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

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1

Community Additions

ADD
Show:
© 2014 Microsoft