The release number of this article has been CHS310988
For Microsoft Visual Basic .NET versions of this article, see
310985.
This article references the following Microsoft .NET Framework Class Bank Name Space:
Microsoft.Data.odbc
This task content
summary
Download the ODBC .NET Managed Provider Create a Project Connection String Demand Test Application Troubleshooting Reference
SUMMARY This step guide describes how to use the ODBC .NET managed provider in Visual C # .NET. This article also includes some example connection strings, you can use these strings to access your data.
The ODBC .NET data provider is an additional component of the Microsoft .NET Framework Software Development Kit (SDK). It provides the same access method for this machine OLE DB provider with the OLE DB .NET data provider for the access method of this ODBC driver. Although the ODBC .NET data provider is used to use all compatible ODBC drivers, only the following drivers are used to test it:
Microsoft SQL ODBC Driver ó? Óú Oracle Microsoft ODBC Driver Microsoft Jet ODBC Driver
Note: If the .NET Framework SDK (it is provided with Visual Studio .NET), the installation of this download will fail. As part of the installation process of this download program,
Microsoft.Data.odbc Namespace is added to Global Jump Cache, and the addition method is the Gacutil.exe utility provided with the .NET Framework SDK.
Back to top
Download ODBC .NET Provider
ODBC .NET hosting providers can be downloaded from the following Microsoft Web site: http://msdn.microsoft.com/downloads/default.asp?url=/downloads/sample.asp?url=/msdn-files/027/001/ 668 / MSDNCompositiveoc.xml ODBC .NET Data Provider also requires the Microsoft Data Access Component (MDAC) version 2.7 or higher. The latest version of MDAC can be downloaded from the following Microsoft Web site: http://www.microsoft.com/data/download.htm After installation of the ODBC .NET data provider, continue the next section to create a project.
Back to top
Create a project
Start Microsoft Visual Studio Net. Create a new Visual C # Windows application project. The 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 the Code view, then the following code is next to other USING statements after these statements: use system.data;
Using Microsoft.Data.odbc; Add four button controls to FORM1, which is marked as SQL Server, Jet, Oracle, and DSN, respectively.
Back to top
Connection string example
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 =; pwd =; ");
CMD = New Odbccommand (MyString, CN);
Cn.open ();
MessageBox.show ("Connected");
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 string according to your environment accordingly.
Back to top
Test client application
Press F5 to compile and run the application. Click Each button. You will receive a message box that prompts you have successfully connected to your data.
Back to top
Troubleshooting If an error is encountered when connecting to the data source (for example, you use an incorrect password, user ID or database name) unless you capture the specific error message, you will receive the following general error message:
An unhandled.data.odbc.odbcexception 'odbc.odbsoft.data.odbc.dll.addom information: system error To help you solve this problem, you can solve this problem.
Try-catch-finally block is added to the code. E.g:
Try
{
Cn.open ();
}
Catch (ODBCEXCEPTION EX)
{
Messagebox.show (ex.Message);
}
Finally
{
Cn.close ();
}
Back to top
For more information about the .NET hosting provider, please visit the .NET developer center or the following Microsoft Web site:
.NET hosted provider probe http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data010112001.asp related to .NET, please visit. Net newsgroup. Microsoft.public.dotNet.framework.odbcnet newsgroup has been established for this release.
Back to top
The information in this article applies to:
Microsoft ADO.NET (provided with .NET Frame) Microsoft Visual C # .NET (2002)
Recent Updated: 2002-8-1 (1.0) Keyword Kbhowto KbhowTomaster KBODBC KBSYSTEMDATA KB310988