.Net中如何连接到ODBC数据源
1.下载ODBC.NET (FrameWork 2.0以上默认未安装)
下载地址:http://www.microsoft.com/downloads/details.aspx?familyid=6ccd8427-1017-4f33-a062-d165078e32b1
2.创建项目,添加Microsoft.Data.ODBC.dll 引用
3.cs代码如下:
using System.Data;
using Microsoft.Data.Odbc;
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();
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();
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();
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();
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();
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。