Summary

Represents a connection to an Open Database Connectivity (ODBC) data source. This class cannot be inherited.

Syntax

public sealed class OdbcConnection : MarshalByRefObject, IDbConnection, ICloneable

Constructors

ODBCConnection()

public OdbcConnection()

Initializes a new instance of the OdbcConnection class.

ODBCConnection(string connectionString)

public OdbcConnection(string connectionString)

Initializes a new instance of the OdbcConnection class with the specified connection string.

Parameters

  • connectionString: A string that contains the connection string to open the ODBC data source.

Fields

The OdbcConnection class does not expose any fields.

Properties

ConnectionString

public override string ConnectionString { get; set; }

Gets or sets the connection string used to connect to the data source.

ConnectionTimeout

public int ConnectionTimeout { get; }

Gets the time-out period for the connection to establish an attempt to return control to the application. The default is 15 seconds.

Database

public string Database { get; }

Gets the name of the current database or the database to be accessed after a connection is opened.

DataSource

public string DataSource { get; }

Gets the data source name (DSN) of the ODBC connection.

Handle

public IntPtr Handle { get; }

Gets the ODBC handle for the connection. Use this property to access the underlying native ODBC connection handle.

ServerVersion

public string ServerVersion { get; }

Gets a string that represents the version of the ODBC driver. This is not the version of the ODBC driver manager.

Methods

Close()

public void Close()

Closes the connection to the data source. This is the preferred method of closing any open connection.

CreateCommand()

public IDbCommand CreateCommand()

Creates and returns an IDbCommand object associated with the OdbcConnection.

Dispose()

protected override void Dispose(bool disposing)

Releases the unmanaged resources and, optionally, releases the managed resources.

Open()

public void Open()

Opens a connection to the ODBC data source specified by the ConnectionString property.

Events

The OdbcConnection class does not expose any public events.

Implements

Interface Member
IDbConnection ConnectionString
IDbConnection ConnectionTimeout
IDbConnection Database
IDbConnection Dispose
IDbConnection Open
IDbConnection Close
IDbConnection CreateCommand
ICloneable Clone

Remarks

The OdbcConnection class represents a unique session with an ODBC data source. The OdbcConnection object, by itself, does not provide access to the data source. To access data, you must create an OdbcCommand object and execute it against the OdbcConnection.

You can use the OdbcConnection class to connect to any data source for which an ODBC driver is installed on the client computer.

The OdbcConnection object is intended to be used within a single application domain.

The OdbcConnection class supports the connection string attribute.

Example:

// Requires: using System.Data.Odbc;

public void OpenOdbcConnection()
{
    string connectionString = "DSN=MyDataSource;UID=myuser;PWD=mypassword;";
    using (OdbcConnection connection = new OdbcConnection(connectionString))
    {
        try
        {
            connection.Open();
            Console.WriteLine("Connection opened successfully!");

            using (OdbcCommand command = new OdbcCommand("SELECT * FROM MyTable", connection))
            {
                using (OdbcDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Console.WriteLine(reader[0]);
                    }
                }
            }
        }
        catch (OdbcException ex)
        {
            Console.WriteLine("Error connecting to ODBC source: " + ex.Message);
        }
    }
}