1.下载ODBC.NET (FrameWork 2.0以上默认未安装)
2.创建项目,添加Microsoft.Data.ODBC.dll 引用
3.cs代码如下:
using System.Data; using Microsoft.Data.Odbc;
4.连接代码示例:
SqlServer:
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();
OLEDB JET:
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();
Oracle:
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();
DSN:
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();