Namespace: System.Data.Odbc
Summary
Provides classes for accessing data sources using the Open Database Connectivity (ODBC) API.
This namespace contains classes such as OdbcConnection
, OdbcCommand
, OdbcDataReader
, and OdbcDataAdapter
, which allow you to connect to, query, and manipulate data in ODBC-compliant data sources. It abstracts the underlying ODBC driver, providing a consistent .NET interface for data access.
Classes
- OdbcConnection Represents a connection to an ODBC data source.
- OdbcCommand Represents a Transact-SQL statement or stored procedure to execute against an ODBC data source.
- OdbcCommandBuilder Generates SQL statements automatically for reconciling changes between a data source and a DataSet.
- OdbcDataReader Provides a way of reading a forward-only stream of rows from an ODBC data source.
- OdbcComponent Provides common functionality for the Odbc classes.
- OdbcError Represents an error returned by an ODBC data source.
- OdbcErrorCollection Represents a collection of errors returned by an ODBC data source.
- OdbcException Represents an error from an ODBC data source.
- OdbcParameter Represents a parameter to an SqlCommand or an OdbcCommand.
- OdbcParameterCollection Represents a collection of parameters for a command that belongs to an Odbc data provider.
- OdbcRowUpdatingEventArgs Provides data for the RowUpdating event.
- OdbcRowUpdatedEventArgs Provides data for the RowUpdated event.
- OdbcTransaction Represents a transaction to be performed at a data source.
- OdbcDataAdapter Represents a DataSet in memory and retrieves rows from and writes rows to a data source.
Enums
- OdbcLevel Specifies the ODBC driver version.
- OdbcCommandType Specifies how to interpret a command string.
Example Usage
Here's a basic example demonstrating how to connect to an ODBC data source and retrieve data:
using System;
using System.Data;
using System.Data.Odbc;
public class OdbcExample
{
public static void Main(string[] args)
{
string connectionString = "DSN=MyODBCDataSource;Uid=myuser;Pwd=mypassword;";
string query = "SELECT CustomerID, CompanyName FROM Customers";
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
try
{
connection.Open();
Console.WriteLine("Connection opened successfully.");
using (OdbcCommand command = new OdbcCommand(query, connection))
{
using (OdbcDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine($"CustomerID: {reader["CustomerID"]}, CompanyName: {reader["CompanyName"]}");
}
}
}
}
catch (OdbcException ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
foreach (OdbcError error in ex.Errors)
{
Console.WriteLine($" - {error.Message}");
}
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
Console.WriteLine("Connection closed.");
}
}
}
}
}