[FORWARD]ODBC 各种数据库连接串
Overview
Generally, one of the first steps when you are trying to work with databases is open it. You can find several types of those, and each have a different mode of connection. When you try to connect with your database sometimes, you don't know the correct connection string that you must use. It is for that I wrote this article. I wanted to compile the connection strings to the majority of known databases...
ODBC DSN Less Connection
ODBC Driver for dBASE
strConnection = _T("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;"
"Dbq=c:\\DatabasePath;");
Note: You must specify the filename in the SQL statement... For example:
CString strQuery = _T("Select Name, Address From Clients.dbf");
ODBC Driver for Excel
strConnection = _T("Driver={Microsoft Excel Driver (*.xls)};DriverId=790;"
bq=C:\\DatabasePath\\DBSpreadSheet.xls;DefaultDir=c:\\databasepath;");
ODBC Driver for Text
strConnection = _T("Driver={Microsoft Text Driver (*.txt; *.csv)};"
"Dbq=C:\\DatabasePath\\;Extensions=asc,csv,tab,txt;");
If you are using tab delimited files, you must create the schema.ini file, and you must inform theFormat=TabDelimited option in your connection string.
Note: You must specify the filename in the SQL statement... For example:
CString strQuery = _T("Select Name, Address From Clients.csv");
Visual FoxPro
If you are using a database Container, the connection string is the following:
strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
ourceType=DBC;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");
If you are working without a database container, you must change the SourceType parameter by DBF as in the following connection string:
strConnection = _T("Driver={Microsoft Visual Foxpro Driver};UID=;"
"SourceType=DBF;SourceDB=C:\\DatabasePath\\MyDatabase.dbc;Exclusive=No");
ODBC Driver for Access
strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=c:\\DatabasePath\\dbaccess.mdb;Uid=;Pwd=;");
If you are using a Workgroup (System database): you need to inform the SystemDB Path, the User Name and itspassword. For that, you have two solutions: inform the user and password in the connection string or in the moment of the open operation. For example:
strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=C:\\VC Projects\\ADO\\Samples\\AdoTest\\dbTestSecurity.mdb;"
"SystemDB=C:\\Program Files\\Microsoft Office\\Office\\SYSTEM.mdw;"
"Uid=Carlos Antollini;Pwd=carlos");
or may be:
strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=C:\\VC Projects\\ADO\\Samples\\AdoTest\\dbTestSecurity.mdb;"
"SystemDB=C:\\Program Files\\Microsoft Office\\Office\\SYSTEM.mdw;");
if(pDB.Open(strConnection, "DatabaseUser", "DatabasePass"))
{
DoSomething();
pDB.Close();
}
If you want to open in Exclusive mode:
strConnection = _T("Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=c:\\DatabasePath\dbaccess.mdb;Exclusive=1;");
ODBC Driver for SQL Server
For Standard security:
strConnection = _T("Driver={SQL Server};Server=MyServerName;"
"Trusted_Connection=no;"
"Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");
For Trusted Connection security (Microsoft Windows NT integrated security):
strConnection = _T("Driver={SQL Server};Server=MyServerName;"
"Database=myDatabaseName;Uid=;Pwd=;");
Also, you can use the parameter Trusted_Connection that indicates that you are using the Microsoft Windows NT Authentication Mode to authorize user access to the SQL Server database. For example:
strConnection = _T("Driver={SQL Server};Server=MyServerName;"
"Database=MyDatabaseName;Trusted_Connection=yes;");
If the SQL Server is running in the same computer, you can replace the name of the server by the word (local) like in the following sample:
strConnection = _T("Driver={SQL Server};Server=(local);"
"Database=MyDatabaseName;Uid=MyUserName;Pwd=MyPassword;");
If you want to connect with a remote SQL Server, you must inform the address, the port, and the Network Library to use:
The Address parameter must be an IP address and must include the port. The Network parameter can be one of the following:
dbnmpntwWin32 Named PipesdbmssocnWin32 Winsock TCP/IPdbmsspxnWin32 SPX/IPXdbmsvinnWin32 Banyan VinesdbmsrpcnWin32 Multi-Protocol (Windows RPC)
For more information, see Q238949.
strConnection = _T("Driver={SQL Server};Server=130.120.110.001;"
"Address=130.120.110.001,1052;Network=dbmssocn;Database=MyDatabaseName;"
"Uid=myUsername;Pwd=myPassword;");
ODBC Driver for Oracle
For the current Oracle ODBC driver from Microsoft:
strConnect = _T("Driver={Microsoft ODBC for Oracle};Server=OracleServer.world;"
"Uid=MyUsername;Pwd=MyPassword;");
For the older oracle ODBC driver from Microsoft:
strConnect = _T("Driver={Microsoft ODBC Driver for Oracle};"
"ConnectString=OracleServer.world;Uid=myUsername;Pwd=myPassword;");
ODBC Driver for MySQL
If you want to connect to a local database, you can use a connection string like the following:
strConnect = _T("Driver={MySQL ODBC 3.51 Driver};Server=localhost;"
"Database=MyDatabase;User=MyUserName;Password=MyPassword;Option=4;");
If you want to connect with a remote database, you need to specify the name of the server or its IP in the Serverparameter. If the Port is distinct to 3306 (default port), you must specify it.
strConnect = _T("Driver={mySQL ODBC 3.51 Driver};Server=MyRemoteHost;"
"Port=3306;Option=4;Database=MyDatabase;Uid=MyUsername;Pwd=MyPassword;");
The parameter Option can be one or more of the following values:
1- The client can't handle that MyODBC returns the real width of a column.2- The client can't handle that MySQL returns the true value of affected rows. If this flag is set then MySQL returns 'found rows' instead. One must have MySQL 3.21.14 or newer to get this to work.4- Make a debug log in c:\myodbc.log. This is the same as puttingMYSQL_DEBUG=d:t:O,c::\myodbc.login AUTOEXEC.BAT.8- Don't set any packet limit for results and parameters.16- Don't prompt for questions even if driver would like to prompt.32- Enable or disable the dynamic cursor support. This is not allowed in MyODBC 2.50.64- Ignore use of database name in 'database.table.column'.128- Force use of ODBC manager cursors (experimental).256- Disable the use of extended fetch (experimental).512- PadCHARfields to full column length.1024-SQLDescribeCol()will return fully qualified column names.2048- Use the compressed server/client protocol.4096- Tell server to ignore space after function name and before '(' (needed by PowerBuilder). This will make all function names keywords!8192- Connect with named pipes to a MySQLd server running on NT.16384- ChangeLONGLONGcolumns toINTcolumns (some applications can't handleLONGLONG).32768- Return 'user' asTable_qualifierandTable_ownerfrom SQLTables (experimental).65536- Read parameters from the client and ODBC groups from my.cnf.131072- Add some extra safety checks (should not be needed but...).
If you want to have multiple options, you should add the above flags! For example: 16 + 1024 = 1030 and useOption= 1030;.
For more information, Go to MyODBC Reference Manual.
ODBC Driver for AS400
strConnect = _T("Driver={Client Access ODBC Driver (32-bit)};System=myAS400;"
"Uid=myUsername;Pwd=myPassword;");
ODBC Driver for SyBase
strConnect = _T("Driver={Sybase System 10};Srvr=MyServerName;Uid=MyUsername;"
"Pwd=myPassword;");
ODBC Driver for Sybase SQL AnyWhere
strConnect = _T("ODBC;Driver=Sybase SQL Anywhere 5.0;"
"DefaultDir=C:\\DatabasePath\;Dbf=C:\\SqlAnyWhere50\\MyDatabase.db;"
"Uid=MyUsername;Pwd=MyPassword;Dsn=\"\";");
DSN Connection
ODBC DSN
strConnect = _T("DSN=MyDSN;Uid=MyUsername;Pwd=MyPassword;");
OLE DB Provider
OLE DB Provider for SQL Server
For Standard security:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
"Initial Catalog=MyDatabaseName;"
"User Id=MyUsername;Password=MyPassword;");
For Trusted Connection security (Microsoft Windows NT integrated security):
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
"Initial Catalog=MyDatabaseName;"
"Integrated Security=SSPI;");
If you want to connect to a "Named Instance" (SQL Server 2000), you must to specify Data Source=Servere Name\Instance Name like in the following example:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName\MyInstanceName;"
"Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;");
If you want to connect with a SQL Server running on the same computer, you must specify the keyword (local) in the Data Source like in the following example:
strConnect = _T("Provider=sqloledb;Data Source=(local);"
"Initial Catalog=myDatabaseName;"
"User ID=myUsername;Password=myPassword;");
To connect to SQL Server running on a remote computer (via an IP address):
strConnect = _T("Provider=sqloledb;Network Library=DBMSSOCN;"
"Data Source=130.120.110.001,1433;"
"Initial Catalog=MyDatabaseName;User ID=MyUsername;"
"Password=MyPassword;");
OLE DB Provider for MySQL (By Todd Smith)
strConnection = _T("Provider=MySQLProv;Data Source=test");
Where test is the name of MySQL database. Also, you can replace the name of the database by the followingconnection string: server=localhost;DB=test.
OLE DB Provider for AS400
strConnect = _T("Provider=IBMDA400;Data source=myAS400;User Id=myUsername;"
"Password=myPassword;");
For more information, see: Using the OLE DB Provider for AS/400 and VSAM.
OLE DB Provider for Active Directory
strConnect = _T("Provider=ADSDSOObject;User Id=myUsername;Password=myPassword;");
For more information, see: Microsoft OLE DB Provider for Microsoft Active Directory Service.
OLE DB Provider for DB2
If you are using a TCP/IP connection:
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;");
If you are using APPC connection:
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;");
For more information, see: Using the OLE DB Provider for DB2.
OLE DB Provider for Microsoft Jet
- Connecting to an Access file using the JET OLE DB Provider:
Using Standard security:
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\DatabasePath\\MmDatabase.mdb;"
"User Id=admin;Password=;");If you are using a Workgroup (System database):
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\DataBasePath\\mydb.mdb;"
"Jet OLEDB:System Database=MySystem.mdw;");
pRs.Open(strConnect, "MyUsername", "MyPassword"); - Connecting to an Excel Spreadsheet using the JET OLE DB Provider:
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;"
"Data Source=C:\\DatabasePath\\DBSpreadSheet.xls;"
"Extended Properties=\"\"Excel 8.0;HDR=Yes;\"\";");Note: If
"HDR=Yes", the provider will not include the first row of the selection into the recordset. If"HDR=No", the provider will include the first row of the cell range (or named ranged) into the recordset.For more information, see: Q278973.
- Connecting to a Text file using the JET OLE DB Provider:
strConnect =
_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\DatabasePath\\;"
"Extended Properties=\"\"text;"
"HDR=Yes;FMT=Delimited;\"\";");Note: You must specify the filename in the SQL statement... For example:
CString strQuery = _T("Select Name, Address From Clients.txt");For more information, see: Q262537.
- Connecting to an Outlook 2000 personal mail box using the JET OLE DB Provider: (By J. Cardinal)
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Outlook 9.0;"
"MAPILEVEL=;DATABASE=C:\\Temp\\;")Replace c:\temp with any temporary folder. It will create a schema file in that folder when you open it which shows all the fields available. Blank
MAPILEVELindicates top level of folders). - Connecting to an Exchange mail box through JET: (By J. Cardinal)
strConnect = _T("Provider=Microsoft.Jet.OLEDB.4.0;Exchange 4.0;"
"MAPILEVEL=Mailbox - Pat Smith|;DATABASE=C:\\Temp\\;")You must replace c:\temp with any temporary folder.
Replace Pat Smith with the name of the mail box and you must keep vertical pipe character | to indicate top level of folders. Place sub folder after vertical pipe if accessing specific folder.
Note: you can enter queries against the mail store just like a database... For example:
CString strQuery = _T("SQL "SELECT Contacts.* FROM Contacts;");For more information, see: The Jet 4.0 Exchange/Outlook IISAM.
OLE DB Provider for ODBC Databases
If you want to connect with a Microsoft Access database:
strConnect = _T("Provider=MSDASQL;Driver={Microsoft Access Driver (*.mdb)};"
"Dbq=c:\\DatabasePath\\MyDatabase.mdb;Uid=MyUsername;Pwd=MyPassword;");
If you want to connect with a SQL Server database:
strConnect = _T("Provider=MSDASQL;Driver={SQL Server};Server=MyServerName;"
"Database=MyDatabaseName;Uid=MyUsername;Pwd=MyPassword;");
If you want to use DSN:
strConnect = _T("Provider=MSDASQL;PersistSecurityInfo=False;"
"Trusted_Connection=Yes;"
"Data Source=MyDSN;catalog=MyDatabase;");
For more information, see: Microsoft OLE DB Provider for ODBC.
OLE DB Provider for OLAP
Microsoft� OLE DB for Online Analytical Processing (OLAP) is a set of objects and interfaces that extends the ability of OLE DB to provide access to multidimensional data stores.
strConnect = _T("Provider=MSOLAP;Data Source=MyOLAPServerName;"
"Initial Catalog=MyOLAPDatabaseName;");
Connection using HTTP:
This feature enables a client application to connect to an Analysis server through Microsoft� Internet Information Services (IIS) by specifying a URL in the Data Source property in the client application's connection string. Thisconnection method allows PivotTable� Service to tunnel through firewalls or proxy servers to the Analysis server. A special Active Server Pages (ASP) page, Msolap.asp, enables the connection through IIS. The directory in which this file resides must be included as part of the URL when connecting to the server (for example,http://www.myserver.com/myolap/).
Using a URL
strConnect = _T("Provider=MSOLAP;Data Source=http://MyOLAPServerName/;"
"Initial Catalog=MyOLAPDatabaseName;");
Using SSL
strConnect = _T("Provider=MSOLAP;Data Source=https://MyOLAPServerName/;"
"Initial Catalog=MyOLAPDatabaseName;");
For more information, see: OLE DB for OLAP, Connecting Using HTTP.
OLE DB Provider for Oracle
OLE DB Provider for Oracle (from Microsoft)
The Microsoft OLE DB Provider for Oracle allows ADO to access Oracle databases.
strConnect = _T("Provider=MSDAORA;Data Source=MyOracleDB;User Id=myUsername;"
"Password=myPassword;");
For more information, see: Microsoft OLE DB Provider for Oracle.
OLE DB Provider for Oracle (from Oracle).
For Standard security:
strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;"
"User Id=myUsername;Password=myPassword;");
For a Trusted connection:
- OS Authenticated connect setting user ID to "/":
strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;"
"User Id=/;Password=;"); - OS Authenticated connect using OSAuthent:
strConnect = _T("Provider=OraOLEDB.Oracle;Data Source=MyOracleDB;OSAuthent=1;")Note: "
Data Source=" must be set to the appropriate Net8 name which is known to the naming method in use. For example, for Local Naming, it is the alias in the tnsnames.ora file; for Oracle Names, it is the Net8 Service Name.
For more information, see: Oracle Provider for OLE DB Developer's Guide.
OLE DB Provider for Visual FoxPro
strConnect = _T("Provider=vfpoledb;"
"Data Source=C:\\DatabasePath\\MyDatabase.dbc;");
For more information, see: Microsoft OLE DB Provider for Visual FoxPro.
OLE DB Provider for Index Server (By Chris Maunder)
strConnect = _T("provider=msidxs;Data Source=MyCatalog;");
For more information, see: Microsoft OLE DB Provider for Microsoft Indexing Service.
OLE DB Data Link Connections
Data Link File - UDL
strConnection = _T("File Name=c:\\DataBasePath\\DatabaseName.udl;");
If you want to create a Data Link File, you can make a new empty text file, change its extension by .udl, then double click over the file, and the operating system calls for you the Data Link app.

[FORWARD]ODBC 各种数据库连接串的更多相关文章
- 【EF 2】浅谈ADO数据模型生成串(二):数据库连接串分析
导读:上篇博客中介绍了ADO生成串的前一部分,本篇博客结合报错,接着介绍剩下的部分. 一.代码展示 <span style="font-family:KaiTi_GB2312;font ...
- [奇思异想]使用Zookeeper管理数据库连接串
背景 有一套特定规格的应用(程序+数据库),当有业务需求时,就需要多部署应用,并且所有的应用都使用一个共同的后台来管理.应用新增后,如何通知后台更新连接串成了一个关键的问题.于是就产生了使用ZooKe ...
- 使用电脑ODBC测试数据库连接方法
使用电脑ODBC测试数据库连接方法 一.打开电脑的控制面板——管理工具——数据源(ODBC),在用户dsn页面中点击添加按钮,选择IBM DB2 ODBC DRIVER,点击完成. 二.在弹出的配置页 ...
- C#_数据库连接串的配置
数据库的连接串 数据库连接字符串的拼写规则的决定条件: 连接的数据库的类型:SQL Server,Oracle,MySQL,Acess,MogoDB,Visual FoxPro(dBASE),Exce ...
- 使用Aes对称加密解密Web.Config数据库连接串
现在很多公司开始为了保证数据库的安全性,通常会对Web.Config的数据库连接字符串进行加密.本文将介绍学习使用Aes加密解密数据库连接字符串.本文采用MySql数据库. AES概念简述 AES 是 ...
- 生成64位代码的mdb数据库连接串Provider的设置
生成32位程序的mdb连接串的 Provider为:Provider=Microsoft.Jet.OLEDB.4.0 而生成64位代码时,则需要使用如下的 Provider Provider=Micr ...
- oracle数据库连接串格式
oracle常用链接串格式:jdbc:oracle:thin:@//<host>:<port>/<service_name> jdbc:oracle:thin:@& ...
- VS2017增加数据库连接串
新装VS2017后,按照三石提供的MVC入门教程,创建MVC应用后,能生成数据库文件(在APP_DATA目录),但无法创建连接,服务器路径一直不正确. 解决方法: 1.重新打开VS2017安装界面,选 ...
- 常见ODBC及OLEDB连接串的写法
书 名:性能测试进阶指南——LoadRunner11实战 作 者:陈霁 关于数据库连接串的写法
随机推荐
- eclipse的debug模式经常自动跳到TheadPoolExecutor的断点
1.问题:eclipse的debug模式经常自动跳到TheadPoolExecutor的断点,实在烦人 解决方法是: 在eclipse中选择Window->Preference->Java ...
- 关于一些blog优化
有很多的好看的$java\ script$ 可以大大的增加$blog$的好看度. 这里,本宝宝就列举几个 upd:不定期更新 1.有木有觉得背景的小姐姐和雪花特效极其的配啊啊啊!!! 页面定制CSS插 ...
- codeforces 1096 题解
A: 发现最优的方案一定是选 $ l $ 和 $ 2 * l $,题目保证有解,直接输出即可 #include <bits/stdc++.h> #define Fast_cin ios:: ...
- 2017.06.04【NOIP提高组】模拟赛B组:
t1 jzoj3762 过河 路径分段,计算出向上移对答案贡献最大的一段路,再使用堆来维护即可 代码: #include<bits/stdc++.h> using namespace st ...
- WebStorm 简单部署服务器进行测试操作
WebStorm 简单部署服务器对外发布接口 第一步: 查看webstorm防火墙是否允许链接,控制面板-->防火墙-->高级设置 入站规则-->webstrom是否允许链接 ,双击 ...
- python基础知识梳理----6set 集合的应用
集合内容简介: set 一: 集合简介 集合set集合是python的一个基本数据类型.一般不是很常用set.中的元素是不重复的.无序的.里里面的元素必须是可hash的tuple,bool),str, ...
- 教你制作自己logo专属的图片
说明:以下教程仅适合对图片分辨率要求不高的情况. 第一步:使用Windows自带的画图工具新建一个250像素*250像素的空白图片. 第二步:使用形状中的三角形,按住Shift键,将三角形拖拉至合适的 ...
- 【性能测试】:关于测试F5负载均衡的问题
在压测过程中,前置分发机的请求分发策略有多种,轮询,随机,DNS均衡,最小连接数等 问题出现的前提:因测试的一个系统对用户安全性校验较强,例如做交易之前,需要验证用户安全信息等 问题出现的现象:当lo ...
- hdu 3709 Balanced Number(平衡数)--数位dp
Balanced Number Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) ...
- Mac下包管理平台homebrew的使用
一.安装 参考:http://www.cnblogs.com/EasonJim/p/6287098.html 二.使用 假设我要安装node,命令如下: 安装软件 brew install node ...