ODBC、OLE连接各种数据库的连接字符串

简介

我们在使用数据库的时候,首先需要打开这个数据库。
我们可以找到集中类型的数据库,每个都使用不同的连接方式。
下面列举了一下主要的数据库的连接字符串



A:使用ODBC方式

1:dBASE连接字符串
strConnection = _T("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;"
                   "Dbq=c:\\DatabasePath;");


2:Excel连接字符串


strConnection = _T("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;"
    bq=C:\\DatabasePath\\DBSpreadSheet.xls;DefaultDir=c:\\databasepath;");


3:Text连接字符串


strConnection = _T("Driver={Microsoft Text Driver (*.txt; *.csv)};"
        "Dbq=C:\\DatabasePath\\;Extensions=asc,csv,tab,txt;");

4:Visual FoxPro连接字符串

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
    ourceType=DBC;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");

strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
    "SourceType=DBF;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");


5:Access连接字符串


strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
        "Dbq=c:\\DatabasePath\\dbaccess.mdb;Uid=;Pwd=;");



6::SQL Server连接字符串

strConnection = _T("Driver={SQL Server};Server=MyServerName;"
        "Trusted_Connection=no;"
        "Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");

7:Oracle连接字符串

strConnect = _T("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;"
        "Uid=MyUsername;Pwd=MyPassword;");


8:MySQL连接字符串
strConnect = _T("Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
     "Database=MyDatabase;User=MyUserName;Password=MyPassword;Option=4;");

9:SyBase连接字符串
strConnect = _T("Driver={Sybase System 10};Srvr=MyServerName;Uid=MyUsername;"
       "Pwd=myPassword;");

10:SqlAnyWhere连接字符串
strConnect = _T("ODBC;Driver=Sybase SQL Anywhere 5.0;"
    "DefaultDir=C:\\DatabasePath\;Dbf=C:\\SqlAnyWhere50\\MyDatabase.db;"
    "Uid=MyUsername;Pwd=MyPassword;Dsn=\"\";");


B:使用OLE方式

1:SQL Server连接字符串
标准的:strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
        "Initial Catalog=MyDatabaseName;"
        "User Id=MyUsername;Password=MyPassword;");


2:MySQL 连接字符串
strConnection = _T("Provider=MySQLProv;Data Source=test");

3:AS400连接字符串
strConnect = _T("Provider=IBMDA400;Data source=myAS400;User Id=myUsername;"
     "Password=myPassword;");

4:DB2连接字符串
使用TCPID
strConnect = _T("Provider=DB2OLEDB;Network Transport Library=TCPIP;"
        "Network Address=130.120.110.001;"
        "Initial Catalog=MyCatalog;Package Collection=MyPackageCollection;"
        "Default Schema=MySchema;User ID=MyUsername;Password=MyPassword;");
使用APPC
strConnect = _T("Provider=DB2OLEDB;APPC Local LU Alias=MyLocalLUAlias;"
        "APPC Remote LU Alias=MyRemoteLUAlias;Initial Catalog=MyCatalog;"
        "Package Collection=MyPackageCollection;Default Schema=MySchema;"
        "User ID=MyUsername;Password=MyPassword;");


5:Microsoft Jet连接字符串
标准的:strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
        "Data Source=C:\\DatabasePath\\MmDatabase.mdb;"
        "User Id=admin;Password=;");

6:使用OLE连接某一个Excel表
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
        "Data Source=C:\\DatabasePath\\DBSpreadSheet.xls;"
        "Extended Properties=\"\"Excel 8.0;HDR=Yes;\"\";");

注意 HDR=Yes 是第一行是字段,会自动过滤

7:OLAP连接字符串
strConnect = _T("Provider=MSOLAP;Data Source=MyOLAPServerName;"
     "Initial Catalog=MyOLAPDatabaseName;");

8:Oracle连接字符串

trConnect = _T("Provider=MSDAORA;Data Source=MyOracleDB;User Id=myUsername;"
     "Password=myPassword;");

8:Data Link Connections字符串

strConnection = _T("File Name=c:\\DataBasePath\\DatabaseName.udl;");


文章转至英文网站:http://www.codeproject.com/Articles/2304/ADO-Connection-Strings

本文地址: http://www.xszlo.com/article/2012-12-02/7557.html,转发请保留这个地址,谢谢

你可能感兴趣的:(www.xszlo.com)