Microsoft KB Archive/310988

= How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings =

Article ID: 310988

Article Last Modified on 8/6/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q310988



For a Microsoft Visual Basic .NET version of this article, see 310985.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * Microsoft.Data.Odbc

IN THIS TASK
SUMMARY
 * Download the ODBC .NET Managed Provider
 * Create the Project
 * Connection String Samples
 * Test the Application
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step article describes how to use the ODBC .NET Managed Provider in Visual C# .NET. This article also includes samples connection strings that you can use to access your data.

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

NOTE: If the .NET Framework SDK (which is included with Visual Studio .NET) is not installed, the setup for this download fails. As part of the setup for this download, the Microsoft.Data.ODBC namespace is added to the Global Assembly Cache by using the Gacutil.exe utility that is included with the .NET Framework SDK.

back to the top

Download the ODBC .NET Provider
 Download the ODBC .NET Managed Provider from the following Microsoft Web site:

http://www.microsoft.com/downloads/details.aspx?familyid=6ccd8427-1017-4f33-a062-d165078e32b1

 The ODBC .NET Data Provider also requires that you install Microsoft Data Access Components (MDAC) version 2.7 or later. You can download the latest version of MDAC from the following Microsoft Web site:

http://msdn2.microsoft.com/en-us/data/aa937695.aspx

 After you install the ODBC .NET Data Provider, proceed to the next section to create the project.

back to the top

Create the Project
 Start Microsoft Visual Studio NET. Create a new Visual C# Windows Application project. Form1 is added to the project by default. From the Project menu, click Add Reference.</li> On the .NET tab, click Microsoft.Data.ODBC.dll. After the Microsoft.Data.ODBC.dll assembly appears in the list of selected components, click OK.</li>  Switch to Code view, and add the following code immediately after the other using statements: using System.Data; using Microsoft.Data.Odbc; </li> Add four Button controls to Form1, and label these controls SQL Server, Jet, Oracle and DSN respectively.</li></ol>

back to the top

Connection String Samples
  Add the following code to the SQL Server button: {       OdbcConnection cn; OdbcCommand cmd; string MyString;

MyString=&quot;Select * from Customers&quot;;

cn= new OdbcConnection(&quot;Driver={SQL Server};Server=mySQLServer;UID=sa;                              PWD=myPassword;Database=Northwind;&quot;);

cmd=new OdbcCommand(MyString,cn); cn.Open;

MessageBox.Show(&quot;Connected&quot;);

cn.Close; }                   </li>  Add the following code to the Jet button: {       OdbcConnection cn; OdbcCommand cmd; string MyString;

MyString=&quot;Select * from Titles&quot;; cn= new OdbcConnection(&quot;Driver={Microsoft Access Driver (*.mdb)};           DBQ=D:\\Program Files\\Microsoft Office\\Office10\\Samples\\Northwind.mdb;UID=;PWD=;&quot;); cmd=new OdbcCommand(MyString,cn); cn.Open; MessageBox.Show(&quot;Connected&quot;);

cn.Close; }                   </li>  Add the following code to the Oracle button: {      OdbcConnection cn; OdbcCommand cmd; string MyString;

MyString=&quot;Select * from Customers&quot;;

cn= new OdbcConnection(&quot;Driver={Microsoft ODBC for Oracle};Server=myOracleServer;                              UID=demo;PWD=demo;&quot;);

cmd=new OdbcCommand(MyString,cn); cn.Open;

MessageBox.Show(&quot;Connected&quot;);

cn.Close; }                   </li>  Add the following code to the DSN button: {      OdbcConnection cn; OdbcCommand cmd; string MyString;

MyString=&quot;Select * from Customers&quot;;

cn= new OdbcConnection(&quot;dsn=myDSN;UID=myUid;PWD=myPwd;&quot;);

cmd=new OdbcCommand(MyString,cn);

cn.Open; MessageBox.Show(&quot;Connected&quot;);

cn.Close; }                                                                                    </li> Modify the OdbcConnection strings as appropriate for your environment.</li></ol>

back to the top

Test the Client Application

 * 1) Press the F5 key to compile and run the application.
 * 2) Click each button. You receive a message box, which states that you have successfully connected to your data.

back to the top

Troubleshooting
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);<BR/>   There should be no <BR/> }       finally {              cn.Close; } back to the top

<div class="references_section">