.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;

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();



 

你可能感兴趣的:(.net)