ODBCConnection Class
System.Data.Odbc.OdbcConnection
Represents a connection to an ODBC data source.
The OdbcConnection
class is used to establish a connection to an ODBC data source.
It supports various connection string attributes to configure the connection, such as the data source name (DSN), UID, PWD, and driver.
Syntax
public sealed class OdbcConnection : DbConnection, ICloneable
Table of Contents
Introduction
The OdbcConnection
class is part of the ADO.NET
data provider for ODBC. It allows .NET applications to interact with any data source for which an ODBC driver is available.
This class provides a managed interface to the underlying ODBC driver, enabling operations such as opening and closing connections,
executing commands, and retrieving data.
OdbcConnection
class, you must have the appropriate ODBC driver installed on your system.
You can configure Data Source Names (DSNs) using the ODBC Data Source Administrator tool.
Properties
Name | Description |
---|---|
ConnectionString | Gets or sets the connection string used to open the ODBC data source. |
ConnectionTimeout | Gets the time to wait while trying to establish a connection to the data source before terminating the attempt and raising an error. |
Database | Gets the name of the database to which the user is connected. |
DataSource | Gets the name of the data source. |
State | Gets the current state of the connection. |
ServerVersion | Gets the version of the ODBC data source to which the client is connected. |
Methods
Name | Description |
---|---|
Open() | Opens a database connection with the properties and settings described by the ConnectionString . |
Close() | Closes the connection to the data source. |
CreateCommand() | Creates and returns an OdbcCommand object associated with the OdbcConnection . |
Dispose() | Releases the resources used by the OdbcConnection . |
BeginTransaction(IsolationLevel isolationLevel) | Starts a database transaction. |
Events
Name | Description |
---|---|
StateChange | Occurs when the state of the connection changes. |
Remarks
When you create an instance of OdbcConnection
, the following actions are performed:
- The
ConnectionString
property is initialized to an empty string. - The
ConnectionTimeout
property is initialized to 30 seconds. - The
State
property is initialized toClosed
.
You can pass a ConnectionString
in the constructor or set it after creating the connection object.
A typical connection string for an ODBC data source might look like this:
Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=C:\mydatabase.accdb;
Or using a DSN:
DSN=MyDataSourceName;Uid=myuser;Pwd=mypassword;
OdbcConnection
when you are finished with it.
Using a using
statement in C# is the recommended way to manage the lifecycle of the connection and ensure it is disposed of correctly.
Examples
Opening an ODBC Connection
The following example demonstrates how to open an OdbcConnection
using a connection string.
using System;
using System.Data.Odbc;
public class Example
{
public static void Main(string[] args)
{
string connectionString = "Driver={SQL Server};Server=myServerName;Database=myDatabase;Uid=myUsername;Pwd=myPassword;";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
try
{
connection.Open();
Console.WriteLine("Connection opened successfully!");
Console.WriteLine("Database: " + connection.Database);
Console.WriteLine("State: " + connection.State);
}
catch (OdbcException ex)
{
Console.WriteLine("Error connecting to database: " + ex.Message);
}
// The connection is automatically closed when exiting the 'using' block
}
Console.WriteLine("Connection closed.");
}
}
Executing a Command
This example shows how to create an OdbcCommand
and execute it.
using System;
using System.Data.Odbc;
public class Example
{
public static void Main(string[] args)
{
string connectionString = "Driver={SQL Server};Server=myServerName;Database=myDatabase;Uid=myUsername;Pwd=myPassword;";
string query = "SELECT COUNT(*) FROM Customers";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
try
{
connection.Open();
using (OdbcCommand command = new OdbcCommand(query, connection))
{
object result = command.ExecuteScalar();
if (result != null)
{
Console.WriteLine("Number of customers: " + result.ToString());
}
}
}
catch (OdbcException ex)
{
Console.WriteLine("Error executing command: " + ex.Message);
}
}
}
}