Microsoft Docs

ODBCConnection Class

Namespace: System.Data.OdbcClient

Assembly: System.Data (in System.Data.dll)

Provides a way to connect to an ODBC data source.

Remarks

The ODBCConnection class is used to establish a connection to an ODBC data source. You can use the connection string to specify the parameters for the connection, such as the driver name, server, database, user ID, and password.

When you use the ODBCConnection object, it is recommended that you create it within a using statement to ensure that the connection is properly closed and disposed of, even if an error occurs.

Important Security Considerations

Never embed a password directly in a connection string. Instead, use a secure method for storing and retrieving credentials, such as Windows Authentication or a secure configuration file.

Syntax

public sealed class ODBCConnection : DbConnection, ICloneable

Members

Constructors

Name Description
ODBCConnection() Initializes a new instance of the ODBCConnection class.
ODBCConnection(string connectionString) Initializes a new instance of the ODBCConnection class with the specified connection string.

Properties

Name Description
ConnectionString Gets or sets the connection string used to open the ODBC data source.
ConnectionTimeout Gets the time in seconds to wait for the data source to respond to the open attempt.
Database Gets the name of the database to access after the connection is opened.
DataSource Gets the name of the data source.
State Gets a string describing the current state of the connection.

Methods

Name Description
Open() Opens a database connection with the properties and values specified by the ConnectionString property.
Close() Closes the connection to the data source.
BeginTransaction() Starts a database transaction.
CreateCommand() Creates and returns a DbCommand object associated with the ODBCConnection.

Example

Connecting to an ODBC Data Source

using System;
using System.Data.Odbc;

public class Example
{
    public static void Main(string[] args)
    {
        // Replace with your actual connection string
        string connectionString =
            "Driver={SQL Server};" +
            "Server=myServerAddress;" +
            "Database=myDataBase;" +
            "Uid=myUsername;" +
            "Pwd=myPassword;";

        using (ODBCConnection connection = new ODBCConnection(connectionString))
        {
            try
            {
                connection.Open();
                Console.WriteLine("Connection opened successfully.");
                Console.WriteLine($"Current state: {connection.State}");

                // Perform database operations here...

            }
            catch (OdbcException ex)
            {
                Console.WriteLine($"Error connecting to the database: {ex.Message}");
            }
            finally
            {
                if (connection.State != System.Data.ConnectionState.Closed)
                {
                    connection.Close();
                    Console.WriteLine("Connection closed.");
                }
            }
        }
    }
}

See Also