This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
The ODBC.NET data provider installation adds Microsoft.Data.Odbc namespace to the namespace, which defines the classes for ODBC data providers. To use the ODBC.NET data provider, you must add a using statement for the Microsoft.Data.Odbc namespace to your application:
- using Microsoft.Data.Odbc;
You've seen how to use the SQL and OleDb data providers in previous articles. Working with the ODBC data provider is no different from working with the SQL and OleDb data providers. Unlike the SQL and OleDb data providers, however, the ODBC data provider is defined in Microsoft.Data.Odbc namespace. You must add a reference to this namespace before you start using the ODBC data provider classes.
The ODBC data provider defines similar classes and a class hierarchy as the SQL and Oledb data providers. Further, you can use the ODBC classes as you've used the SQL and OleDb classes. Table 11-1 defines the ODBC.NET data provider classes.
Table 11-1: The ODBC.NET Data Provider Classes
|
CLASS
|
DESCRIPTION
|
|
OdbcCommand
|
Similar to OleDbCommand and SqlCommand, this class represents an SQL statement or stored procedure to execute against a data source.
|
|
OdbcCommandBuilder
|
Similar to OleDbCommandBuilder and SqlCommandBuilder, this class automatically generates select, insert, update, and delete SQL commands.
|
|
OdbcConnection
|
Represents a connection.
|
|
OdbcDataAdapter
|
Represents a data adapter.
|
|
OdbcDataReader
|
Represents a data reader.
|
|
OdbcError
|
Represents errors and warnings.
|
|
OdbcErrorCollection
|
Represents collection of errors and warnings.
|
|
OdbcException
|
Represents an ODBC exception class.
|
|
OdbcParameter
|
Represents an ODBC parameter.
|
|
OdbcParameterCollection
|
Represents a parameter collection.
|
|
OdbcTransaction
|
Represents a transaction.
|
As you can see from table 11-1, the ODBC data provider has connection, command, data adapter, parameter, exception, and errors, command builder, data reader, transaction, and other classes similar to the Sql and OleDb data providers. To use the ODBC data provider classes, you create a connection object, fill data from the connection to a data adapter or a data reader, and then display the data.
Now I'll show you an example of how to access data from a data source using the ODBC data provider. In this example, I'll use the access 2000 Northwind database as the data source.
Before creating a connection, the first thing you need to understand is the connection string. The connection string for OdbcConnection contains a data source driver and the data source path with an optional user ID and password. Optionally, you can also use an ODBC Data Source Name (DSN) as a connection string. You create a DSN from the ODBC Administration.



Join the conversation! Your thoughts help the community grow.