tailieunhanh - Connecting to an ODBC Data Source

[ Team LiB ] Recipe Connecting to an ODBC Data Source Problem You want to access your data source using an ODBC provider from your .NET application. Solution Use the ODBC .NET data provider to access data exposed through an ODBC driver. | Team LiB Recipe Connecting to an ODBC Data Source Problem You want to access your data source using an ODBC provider from your .NET application. Solution Use the ODBC .NET data provider to access data exposed through an ODBC driver. The sample code contains a single event handler Connect Creates an OdbcDataAdapter and uses it to fill a DataTable with the Category table from the Northwind sample database. The default view of the table is bound to a data grid on the form. The C code is shown in Example 1-1. Example 1-1. File Namespaces variables and constants using System using using using . . . private void connectButton_Click object sender e Create the DataAdapter. String sqlSelect SELECT CategorylD CategoryName Description FROM Categories OdbcDataAdapter da new OdbcDataAdapter sqlSelect Odbc_ConnectString Create the table fill it and bind the default view to the grid. DataTable dt new DataTable dt Discussion The ODBC .NET data provider communicates with native ODBC drivers through COM interop .NET s interoperability layer for COM . The following ODBC providers are guaranteed to be compatible with the data provider Microsoft SQL Server ODBC Driver Microsoft ODBC Driver for Oracle Microsoft Access Jet ODBC Driver The .NET data provider for ODBC connects to ODBC data sources through the OdbcConnection object. The ODBC driver connection string is specified using the ConnectionString property. It includes settings needed to establish the connection to the data source. The connection string format closely matches the ODBC connection string format. Additionally you can specify an ODBC data source name DSN or file DSN by setting the ConnectionString attribute to DSN myDSN or FileDSN myFileDSN . For more information about specifying ODBC connection strings see the topic SQLDriverConnect