[轉] 通過ADO連線各種資料庫的字串翠集

evancss發表於2004-12-27

提供人暱稱:鵬程.Net 作者:溫大文

--------------------------------------------------------------------------------
 
在網路程式設計過程中,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

%>
 

相關文章