ASP串連資料庫的5種方法

來源:互聯網
上載者:User

第一種 - 這種方法用在ACCESS中最多
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" _
& Server.MapPath("aspfree.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn

第二種-這種方法用在SQL SERVER中多
strconn = "Driver={SQL Server};Description=sqldemo;SERVER=127.0.0.1;" _
&"UID=LoginID;Password=;DATABASE=Database_Name"
set conn = server.createobject("adodb.connection")
conn.open strconn

第三種
strconn="Driver={Microsoft Access Driver(*.mdb)};" _
&"DBQ=F:\Inetpub\wwwroot\somedir\db1.mdb;DefaultDir=" _
&"f:\Inetpub\wwwroot\somedir;uid=LoginID;" _
&"pwd=Password;DriverId=25;FIL=MSAccess;"
set conn = server.createobject("adodb.connection")
conn.open strconn

第四種運用系統資料來源
The following uses a Data Source Name: Example
set conn = server.createobject("adodb.connection")
conn.open "Example"

第五種運用ODBC資料來源,前提是你必須在控制台的ODBC中設定資料來源
set rs = server.createobject("adodb.recordset")
rs.open "tblname", "DSNName", 3, 3  第一種 - 這種方法用在ACCESS中最多
strconn = "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" _
& Server.MapPath("aspfree.mdb")
set conn = server.createobject("adodb.connection")
conn.open strconn

下面是講解資料庫連接的一個網站,相當好
 connectiong  :http://tempuri.org/tempuri.html

ASP串連資料庫的全能代碼

'解決了ACCESS資料庫路徑的問題!
'採用DBType=0或DBType=1來區分AC庫還是MSSQL庫
'具體採用AC庫時應注意的事項,請看程式說明

-----------------------------------------------
Dim DBType,Conn,StrConn
DBType=0        '0為Access資料庫,1為MSSQL資料庫

If(DBType=0) Then
'****************ACCESS資料庫******************
Dim DbFolderName,DbFolder_Path,SiteFolder
DbFolderName="ArticleData"   '資料庫所在檔案夾名稱
DbFolder_Path = Server.MapPath(DbFolderName)   '資料庫所在路徑
SiteFolder="Article"                    '系統所在根目錄名稱

   If Session("RootDir") = "" Then
        Session("RootDir") = Mid(DbFolder_Path, 1, InStr(1,DbFolder_Path,SiteFolder,1) -1) & SiteFolder
   End if
   Set Conn = Server.CreateObject("Adodb.Connection")
   StrConn = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Session("RootDir") & "\"& DbFolderName & "\Data.mdb"  '串連到資料庫
   Conn.Open StrConn
'**************************************************
ElseIf(DBType=1) Then
'*****************MSSQL SERVER資料庫******
   Dim DBUserID,DBPassWord,DBName,DBIP
   '修改以下資訊以適合你的網站
   DBUserID="sa"  '資料庫登陸名
   DBPassWord=""  '資料庫密碼
   DBName="dbname" '資料庫名稱
   DBIP="local" '資料庫所在地址,如果是本機資料庫則為:(local)

   Set Conn=Server.CreateObject("Adodb.Connection")
   StrConn = "PROVIDER=SQLOLEDB.1;Data Source="&DBIP&";Initial Catalog="&DBName&";Persist Security Info=True;User ID="&DBUserID&";Password="&DBPassWord&";Connect Timeout=30"
   Conn.Open StrConn
'******************************************************
Else
'***********************資料庫設定錯誤*****************
   Response.Write"資料庫設定錯誤,請聯絡管理員!"
   Response.End
End If
'Response.Write StrConn

'解決了ACCESS資料庫路徑的問題!
'採用DBType=0或DBType=1來區分AC庫還是MSSQL庫
'具體採用AC庫時應注意的事項,請看程式說明

-----------------------------------------------
Dim DBType,Conn,StrConn
DBType=0        '0為Access資料庫,1為MSSQL資料庫

If(DBType=0) Then
'****************ACCESS資料庫******************
Dim DbFolderName,DbFolder_Path,SiteFolder
DbFolderName="ArticleData"   '資料庫所在檔案夾名稱
DbFolder_Path = Server.MapPath(DbFolderName)   '資料庫所在路徑
SiteFolder="Article"                    '系統所在根目錄名稱

   If Session("RootDir") = "" Then
        Session("RootDir") = Mid(DbFolder_Path, 1, InStr(1,DbFolder_Path,SiteFolder,1) -1) & SiteFolder
   End if
   Set Conn = Server.CreateObject("Adodb.Connection")
   StrConn = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Session("RootDir") & "\"& DbFolderName & "\Data.mdb"  '串連到資料庫
   Conn.Open StrConn
'**************************************************
ElseIf(DBType=1) Then
'*****************MSSQL SERVER資料庫******
   Dim DBUserID,DBPassWord,DBName,DBIP
   '修改以下資訊以適合你的網站
   DBUserID="sa"  '資料庫登陸名
   DBPassWord=""  '資料庫密碼
   DBName="dbname" '資料庫名稱
   DBIP="local" '資料庫所在地址,如果是本機資料庫則為:(local)

   Set Conn=Server.CreateObject("Adodb.Connection")
   StrConn = "PROVIDER=SQLOLEDB.1;Data Source="&DBIP&";Initial Catalog="&DBName&";Persist Security Info=True;User ID="&DBUserID&";Password="&DBPassWord&";Connect Timeout=30"
   Conn.Open StrConn
'******************************************************
Else
'***********************資料庫設定錯誤*****************
   Response.Write"資料庫設定錯誤,請聯絡管理員!"
   Response.End
End If
'Response.Write StrConn

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.