通过ADO连接各种数据库的字符串翠集
海南省信息服务中心 温大文
在网络编程过程中,ADO(Activex Data Object)编程往往是不可少的工作,特别是开发电子商务网站。既然讲到ADO数据对象,那么就顺便简单地介绍一下ADO数据对象及其功能。ADO数据对象共有七种独立的对象,他们分别是连接对象(Connection)、记录集对象(RecordSet)、域对象(Field)、命令对象(Command)、参数对象(Parameter)、属性对象(Property)和错误对象(Error)。功能请参见附录1。
ADO数据对象用来连接数据库的方式有两种,分别是ODBC和OLE DB方式,下面就分别介绍用这两种方式连接数据库的例子。
第一种:ODBC DSN-Less Connections
ODBC Driver for Access
ODBC Driver for dBASE
ODBC Driver for Excel
ODBC Driver for MySQL
ODBC Driver for Oracle
ODBC Driver for Paradox
ODBC Driver for SQL Server
ODBC Driver for Sybase
ODBC Driver for Sybase SQL Anywhere
ODBC Driver for Text
ODBC Driver for Teradata
ODBC Driver for Visual FoxPro
第二种:OLE DB Data Provider Connections
OLE DB Provider for Active Directory Service
OLE DB Provider for DB2
OLD DB Provider for Internet Publishing
OLE DB Provider for Index Server
OLE DB Provider for Microsoft Jet
OLE DB Provider for ODBC Databases
OLE DB Provider for Oracle (From Microsoft)
OLE DB Provider for Oracle (From Oracle)
OLE DB Provider for Simple Provider
OLE DB Provider for SQL Server
一、下面我就先讲利用ODBC DSN来访问数据库,要完成这项工作,必须先通过“控制面板”—“管理工具”—“ODBC数据源”来建立一个数据源名,例如叫做TestData_Resoure。
1) 通过系统数据源(System DSN)的连接
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="DSN=TestData_Resoure;" & _
"Uid=AdminAccount;" & _
"Pwd=PassWord;
oConn.Open strConn
%>
2) 通过文件数据源(File DSN)的连接
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="FILEDSN=c:\somepath\mydb.dsn;" & _
"Uid=AdminAccount;" & _
"Pwd=PassWord;"
oConn.Open strConn
%>
3) 通过连接池(DSN-Less)的连接(ODBC Driver for AS/400)
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
strConn= "Driver={Client Access ODBC Driver (32-bit)};" & _
"System=myAS400;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
oConn.Open strConn
%>
二、利用数据库驱动程序直接访问数据库的连接字符串。
1) ODBC Driver for Access
● 标准的也是比较常用的连接方法
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=”&Server.MapPath(“Testdb.mdb”); & _
"Uid=AdminAccount;" & _
"Pwd=Password;"
oConn.Open strConn
%>
● 假如是一个工作组的系统数据库,那么连接字符串如下
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=c:\datapath\Testdb.mdb;" & _
"SystemDB=c:\datapath\Testdb.mdw;", _
"admin", ""
oConn.Open strConn
%>
● 假如数据库(MDB)是网络上共享的,那么连接字符串如下
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={Microsoft Access Driver (*.mdb)};" & _
"Dbq=\\myServer\myShare\myPath\Testdb.mdb;"
oConn.Open strConn
%>
2) ODBC Driver for dBASE
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={Microsoft dBASE Driver (*.dbf)};" & _
"DriverID=277;" & _
"Dbq=c:\FilePath;"
oConn.Open strConn
%>
说明:这里要注意的一点就是,在SQL查询语句中要特别指定数据库文件名,例如:
oRs.Open "Select * From Testdb.dbf", oConn, , ,adCmdText
3) ODBC Driver for Excel
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={Microsoft Excel Driver (*.xls)};" & _
"DriverId=790;" & _
"Dbq=c:\filepath\myExecl.xls;"
oConn.Open strConn
%>
4) ODBC Driver for MySQL (通过 MyODBC驱动程序)
● 连接到本地数据库(local database)
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={mySQL};" & _
"Server=ServerName;" & _
"Option=16834;" & _
"Database=mydb;"
oConn.Open strConn
%>
● 连接远程数据库(remote databas)
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={mySQL};Server=db1.database.com;Port=3306;" & _
"Option=131072;Stmt=;Database=mydb;Uid=myUsername;Pwd=myPassword;"
oConn.Open strConn
%>
5) ODBC Driver for Oracle
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={Microsoft ODBC for Oracle};" & _
"Server=OracleServer.world;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
oConn.Open strConn
%>
6) ODBC Driver for Paradox
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={Microsoft Paradox Driver (*.db)};" & _
"DriverID=538;" & _
"Fil=Paradox 5.X;" & _
"DefaultDir=c:\dbpath\;" & _
"Dbq=c:\dbpath\;" & _
"CollatingSequence=ASCII;"
oConn.Open strConn
%>
7) ODBC Driver for SQL Server
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn="Driver={SQL Server};" & _
"Server=MyServerName;" & _
"Database=myDatabaseName;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
oConn.Open strConn
%>
8) ODBC Driver for Sybase (通过Sybase System 11 ODBC Driver驱动程序)
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={SYBASE SYSTEM 11};" & _
"Srvr=myServerName;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
oConn.Open strConn
%>
9) ODBC Driver for Sybase SQL Anywhere
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "ODBC; Driver=Sybase SQL Anywhere 5.0;" & _
"DefaultDir=c:\dbpath\;" & _
"Dbf=c:\sqlany50\mydb.db;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
"Dsn="""";"
oConn.Open strConn
%>
10) ODBC Driver for Teradata
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Provider=Teradata;" & _
"DBCName=MyDbcName;" & _
"Database=MyDatabaseName;" & _
"Uid=myUsername;" & _
"Pwd=myPassword;"
oConn.Open strConn
%>
11) ODBC Driver for Text
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={Microsoft Text Driver (*.txt; *.csv)};" & _
"Dbq=c:\somepath\;" & _
"Extensions=asc,csv,tab,txt;" & _
"Persist Security Info=False"
oConn.Open strConn
%>
12) ODBC Driver for Visual FoxPro
●使用数据库容器(database container)连接方式
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={Microsoft Visual FoxPro Driver};" & _
"SourceType=DBC;" & _
"SourceDB=c:\somepath\mySourceDb.dbc;" & _
"Exclusive=No;"
oConn.Open strConn
%>
● 不使用数据库容器(database container)连接方式(即Free Table Directory方式)
<%
<%
Dim oConn,strConn
Set oConn=Server.CreateObject(“ADODB.Connection”)
StrConn= "Driver={Microsoft Visual FoxPro Driver};" & _
"SourceType=DBF;" & _
"SourceDB=c:\somepath\mySourceDbFolder;" & _
"Exclusive=No;"
oConn.Open strConn
%>
(未完待续)
kdsfjk