OdbcException Class

Summary

Represents an exception generated by an Odbc 1.0 data provider.

Description
Members
Example

Description

The OdbcException class represents errors returned from an ODBC data source. This exception is thrown when an Odbc 1.0 data provider encounters an error. It provides information about the error, including the errors returned by the ODBC driver.

When an OdbcException is thrown, the Errors collection contains one or more OdbcError objects. Each OdbcError object provides details about a specific error, such as the native error code, a description of the error, and the SQL state.

Remarks

The Microsoft Data Access Components (MDAC) provide a common interface between data access technologies and data sources. The System.Data.Odbc classes provide access to data sources through an ODBC driver. An OdbcException is thrown when an error occurs during interaction with the ODBC data source.

The Errors property of the OdbcException contains a collection of OdbcError objects. You can iterate through this collection to retrieve detailed information about each error that occurred.

It is recommended to catch OdbcException exceptions specifically to handle errors related to ODBC operations.

Members

Constructors

public OdbcException()

Initializes a new instance of the OdbcException class.

public OdbcException(string message)

Initializes a new instance of the OdbcException class with a specified error message.

public OdbcException(string message, Exception innerException)

Initializes a new instance of the OdbcException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Properties

Name Description
Errors Gets a collection of OdbcError objects that contains detailed information about the error.
Message Gets a message that describes the current exception. (Inherited from System.Exception)
StackTrace Gets a string representation of the immediate frames of the call stack. (Inherited from System.Exception)
InnerException Gets an instance of System.Exception that is the cause of the current exception. (Inherited from System.Exception)

Example

The following C# code demonstrates how to catch an OdbcException and iterate through its Errors collection to display detailed error information.


using System;
using System.Data.Odbc;

public class OdbcExceptionExample
{
    public static void Main(string[] args)
    {
        string connectionString = "DRIVER={SQL Server};SERVER=myServerAddress;DATABASE=myDatabase;UID=myUsername;PWD=myPassword;";
        string sqlQuery = "SELECT * FROM NonExistentTable;"; // Intentionally invalid query

        using (OdbcConnection connection = new OdbcConnection(connectionString))
        {
            try
            {
                connection.Open();
                using (OdbcCommand command = new OdbcCommand(sqlQuery, connection))
                {
                    command.ExecuteNonQuery();
                }
            }
            catch (OdbcException odbcEx)
            {
                Console.WriteLine("An OdbcException occurred:");
                foreach (OdbcError error in odbcEx.Errors)
                {
                    Console.WriteLine("  NativeError: {0}", error.NativeError);
                    Console.WriteLine("  Message: {0}", error.Message);
                    Console.WriteLine("  SQLState: {0}", error.SQLState);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("An unexpected exception occurred: {0}", ex.Message);
            }
        }
    }
}