C# 连接数据库(ODBC、Oledb、Oracle)

ODBC

OdbcConnection.ConnectionString

Oledb

OleDbConnection.ConnectionString

OracleClient

OracleConnection.ConnectionString

  • ODBC

开放数据库连接(Open Database Connectivity,ODBC)是微软公司开放服务结构(WOSA,Windows Open Services Architecture)中有关数据库的一个组成部分。ODBC可以连接多种数据库。

可能的连接字符串:

 /* 可能的连接字符串:
                    "Driver={SQL Server};Server=(local);Trusted_Connection=Yes;Database=AdventureWorks;"

                    "Driver={Microsoft ODBC for Oracle};Server=ORACLE8i7;Persist Security Info=False;Trusted_Connection=Yes"

                    "Driver={Microsoft Access Driver (*.mdb)};DBQ=c:\bin\Northwind.mdb"

                    "Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\bin\book1.xls"

                    "Driver={Microsoft Text Driver (*.txt; *.csv)};DBQ=c:\bin"

                    "DSN=dsnname"
            */
View Code

连接实例:

 string connStr = string.Format("Driver={2}Microsoft Access Driver (*.mdb){3};DBQ={0};PWD={1};", mdbPath, dbPassWord,"{","}");//连接本地带密码的mdb
            connStr = string.Format("Driver={2}Oracle in OraDb11g_home1{3};Server=orcl;Persist Security Info=False;Trusted_Connection=Yes;UID={0};PWD={1}",
                "aqgis", "1", "{", "}");//连接oracle数据库aqgis,密码1----Oracle in OraDb11g_home1为驱动名称
            OdbcConnection OdbcConn = new OdbcConnection(connStr);
            OdbcConn.Open();
            OdbcDataAdapter pAdapter = new OdbcDataAdapter("select * from MDARESULT", OdbcConn);
            DataSet pOdbcDataset = new DataSet();
            pAdapter.Fill(pOdbcDataset);
            OdbcConn.Close();

其中Oracle in OraDb11g_home1为驱动名称,如下图:

也就是说,在上图中所有安装的驱动程序,都可以通过ODBC进行调用。特别注意,驱动的名称必须与系统写的完全一致,空格也不可以省略!!

下面的例子都是打开同一个mdb实例,使用了不同的驱动程序,方法:

 string pDriverName = "Driver do Microsoft Access (*.mdb)";//空格不可以省略
            pDriverName = "Microsoft Access Driver (*.mdb)";//空格不可以省略
            pDriverName = "Microsoft Access Driver (*.mdb, *.accdb)";//空格不可以省略
            pDriverName = "Microsoft Access-Treiber (*.mdb)";//空格不可以省略

            connStr = string.Format("Driver={0}{1}{2};DBQ={3};PWD={4};", "{", pDriverName, "}", mdbPath, dbPassWord);//本地;mdbPath-文件路径;dbPassWord-mdb文件密码,没有可去掉。
            OdbcConnection OdbcConn = new OdbcConnection(connStr);
            OdbcConn.Open();
            OdbcDataAdapter pAdapter = new OdbcDataAdapter("select * from MDARESULT", OdbcConn);
            DataSet pOdbcDataset = new DataSet();
            pAdapter.Fill(pOdbcDataset);
            OdbcConn.Close();

 

  • Oledb

可能的连接字符串:

"Provider=MSDAORA; Data Source=ORACLE8i7;Persist Security Info=False;Integrated Security=Yes"

"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\bin\LocalAccess40.mdb"

"Provider=SQLOLEDB;Data Source=(local);Integrated Security=SSPI"

例子:

  //带密码的mdb-两种写法OLEDB.4.0
            //string connstr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source={0};User Id=Admin;PassWord={1};", mdbPath, dbPassWord);
            string connstr = string.Format("Provider=Microsoft.Jet.OLEDB.4.0 ;Data Source={0};Jet oledb:database password={1};", mdbPath, dbPassWord);
            //OLEDB.12.0(可打开.accdb)
            //string connstr = string.Format("Provider=Microsoft.ACE.OLEDB.12.0 ;Data Source={0};Jet oledb:database password={1};", mdbPath, dbPassWord);

            OleDbConnection pConn = new OleDbConnection(connstr);
            pConn.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter();


            DataSet db_filePath = new System.Data.DataSet();
            adapter.SelectCommand = new OleDbCommand(sqlstr, pConn);
            adapter.Fill(db_filePath);

Oledb也可以像ODBC那样,使用不同的驱动连接数据库,只需要修改驱动提供名称(Provide)即可。

新建文本文档,将文本文档重命名为:“打开数据链接属性对话框(Oledb驱动列表).udl”,双击该文件即可打开下面的对话框:

列表中的驱动程序都可以用来进行数据库操作!

  • Oracle

例子:

string connectionString = string.Format("Data Source={0};User Id={1};Password={2};", "orcl", "aqgis", "1");//orcl实例名,aqgis用户名
            using (System.Data.OracleClient.OracleConnection connection = new System.Data.OracleClient.OracleConnection(connectionString))
            {
                connection.Open();
                System.Data.OracleClient.OracleDataAdapter pAda = new System.Data.OracleClient.OracleDataAdapter("select * from MDARESULT", connection);
                DataSet pOracleDt = new DataSet();
                pAda.Fill(pOracleDt);
            }

 

posted @ 2016-12-09 11:45  Hxxxxxxyyyyyy  阅读(6653)  评论(0编辑  收藏  举报