| Printable Version
ODBC .NET
By Kamran Shakil
The ODBC .NET Data Provider is an add-on component to the Microsoft .NET Framework Software Development Kit (SDK). It provides access to native ODBC drivers the same way that the OLE DB .NET Data Provider provides access to native OLE DB Providers. Although the ODBC .NET Data Provider is intended to work with all compliant ODBC drivers, it has only been tested with the following drivers:
-Microsoft SQL ODBC Driver
-Microsoft ODBC Driver for Oracle
-Microsoft Jet ODBC Driver
As popular as OLE DB and SQL Server are, they are not the only databases and access paths Microsoft has supported. As discussed in the previous chapter, OLE DB is next step forward from the Open Database Connectivity standard (ODBC). However, many product vendors are still "stuck in the 20th century" by supporting only COM-based ODBC providers. This .NET Data Provider behaves like the OleDB provider in terms of bridging COM functionality in the .NET environment.
If you encounter a problem when you connect to your data source (for example, if you use an incorrect password, User ID, or database name), you receive the following generic error message unless you trap for a specific error message:
An unhandled exception of type 'Microsoft.Data.Odbc.OdbcException' occurred in Microsoft.Data.Odbc.dll. Additional information: System Error
To provide more information about the error and to assist in troubleshooting, you can add a try-catch-finally block to the code. For example:
try
{
cn.Open();
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message);
}
finally
{
cn.Close();
}
A simple and just a bird's eye view , as how to proceed in odbc.net in Microsoft .NET SDK.
Open Microsoft Visual Studio NET. Create a new Visual Basic Windows Application project. Form1 is added to the project by default. From the Project menu, click Add Reference. On the .NET tab, click Microsoft.Data.ODBC.dll.
After the icrosoft.Data.ODBC.dll assembly appears in the list of selected components, click OK. Switch to Code view, and add the following code immediately before the Public Class Form1 code:
Imports System.Data
Imports Microsoft.Data.ODBC
Add four Button controls to Form1, and label these controls SQL Server, Jet, Oracle and DSN respectively.
Cheers and happy .neting!
|