How to use the odbc .NET Managed Provider in Visual C # .NET and Connection Strings

xiaoxiao2021-03-06  102

Suitable

This Article Was previously distribLished 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 Samples Test The Application Troubleshooting References

Summarythis Step-by-Step Article Describes How To Use The ODBC .NET Managed Provider in Visual C # .NET. This Article Also includeses Samples ARTINECLE 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 include 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 include 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://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/001/ 668 / msdncompositedoc.xml 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://msdn.microsoft .com / library / default.asp? url = / downloads / list / dataaccess.asp after you install the odbc .NET DATA Provider, Proceed to the next section to create the project.back 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. 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 Switch to code view, and add the following code immediately after the other using statements:. using System.Data;

Using Microsoft.Data.odbc;

Add Four Button Controls To Form1, And Label these Controls SQL Server, Jet, Oracle and DSN Respective.

Back to the top

Connection String Samples

Add The Following Code to the SQL Server Button:

{OdbcConnection cn; OdbcCommand cmd; string MyString; MyString = "Select * from Customers"; cn = new OdbcConnection ( "Driver = {SQL Server}; Server = mySQLServer; UID = sa; PWD = myPassword; Database = Northwind;") CMD = New ODBCCommand (MyString, CN); cn.open (); MessageBox.show ("Connected"); cn.close ();} add the following code to the Jet Button:

{OdbcConnection cn; OdbcCommand cmd; string MyString; MyString = "Select * from Titles"; cn = new OdbcConnection ( "Driver = {Microsoft Access Driver (* .mdb)}; DBQ = D: / Program Files / Microsoft Office / Office10 /Samples/northwind.mdb;uid=qpwd=; "); cmd = new odbccommand (mystring, cn); cn.open (); messagebox.show (" connection); cn.close ();

Add The Following Code to The Oracle Button:

{OdbcConnection cn; OdbcCommand cmd; string MyString; MyString = "Select * from Customers"; cn = new OdbcConnection ( "Driver = {Microsoft ODBC for Oracle}; Server = myOracleServer; UID = demo; PWD = demo;"); cmd = New ODBCCommand (MyString, CN); cn.open (); MessageBox.show ("Connected"); cn.close ();

Add The Following Code to the DSN Button:

{OdbcConnection cn; OdbcCommand cmd; string MyString; MyString = "Select * from Customers"; cn = new OdbcConnection ( "dsn = myDSN; UID = myUid; PWD = myPwd;"); cmd = new OdbcCommand (MyString, cn); Cn.open (); MessageBox.show ("Connected"); cn.close ();} Modify The OdbcConnection Strings as Appriate for your Environment.

Back to the top

Test The Client Application

.......................

Back to the top

TroubleshootingIf 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); "there is stay be no}

Finally

{Cn.close ();

Back to the top

Referencesfor More Information About .NET Managed Providers, Refer to The .Net Developer's Center or The Following Microsoft Web Site:

Inside .NET Managed Providershttp: //msdn.microsoft.com/library/default.asp url = / library / en-us / dndive / html / data010112001.aspFor more information about .NET, visit the .NET newsgroups The microsoft?. .public.dotNet.framework.odbcnet news estrappese. Original reference:

http://support.microsoft.com/default.aspx?scid=kb;n-us;q310988

转载请注明原文地址:https://www.9cbs.com/read-126111.html

New Post(0)