How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
来源:互联网 发布:数据解决方案 编辑:程序博客网 时间:2024/04/30 19:15
适用于
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:
back to the top
back to the top
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:
This article refers to the following Microsoft .NET Framework Class Library namespace:
- Microsoft.Data.Odbc
IN THIS TASK
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
back to the top
Download the ODBC .NET Provider
- Download the ODBC .NET Managed Provider from the following Microsoft Web site:
- 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.
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 respectively.
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=;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:Modify the OdbcConnection strings as appropriate for your{
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();
} environment.
Test the Client Application
- Press the F5 key to compile and run the application.
- Click each button. You receive a message box, which states that you have successfully connected to your data.
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();
}
{
cn.Open();
}
catch (OdbcException ex)
{
MessageBox.Show(ex.Message);<BR/> There should be no <BR/>
}
finally
{
cn.Close();
}
REFERENCES
For more information about .NET managed providers, refer to the .NET Developer's Center or the following Microsoft Web site:Inside .NET Managed Providers
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndive/html/data010112001.asp
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- asp.net connection database compare bewteen ado.net Managed provider to ado.net sql managed provider
- How to use composition and inheritance in visual c# ?
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- How to Use the Visual Studio .NET Application Center Test
- How to check the MSOLAP Provider used in connection string
- How to authenticate against the Active Directory by using Forms authentication and Visual C# .NET
- HOW TO: Set a Windows Hook in Visual C# .NET
- HOW TO: Set a Windows Hook in Visual C# .NET
- how to use javascript to control the usercotrol in the asp.net
- How to use the System Restore API to save and to restore system data in Visual C++
- ADO.NET Managed Provider
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- How to use Events in the Context of C#
- How To: Use Membership in ASP.NET 2.0
- How To: Use Role Manager in ASP.NET 2.0
- How To: Use Windows Authentication in ASP.NET 2.0
- 小心修改集合的大小
- HTML4.0语法教学
- 正则表达式
- SCEA之路--6. Enterprise JavaBeans
- 这是我们公司总结的一些关于中文乱码问题的一些解决方案和经验和大家分享!
- How To Use the ODBC .NET Managed Provider in Visual C# .NET and Connection Strings
- JSP的运行内幕
- 微软高级软件研发主管研修计划(Architect 2000)培训感想
- Dotnet WinForm 建立 FAQ (1)
- Dotnet WinForm 建立 FAQ (2)
- Sybase PowerBuilder9.0综述
- 摩托罗拉惠普联手在手机网络市场上推广 Linux
- 微软打“安全牌”坚决反击 Linux
- 嵌入式 Linux 系统侵蚀微软和风河的市场