ASP連接數據庫幾種方式

一、access

1.
set dbconnection=Server.CREATEOBJECT("ADODB.CONNECTION")
DBPath = Server.MapPath("customer.mdb")
dbconnection.Open "driver={Microsoft Access Driver (*.mdb)};dbq=" & DBPath
    SQL="select * from auth where id="" & user_id &"""
    SET uplist=dbconnection.EXECUTE(SQL)


2.
    set dbconnection=Server.CreateObject("ADODB.Connection")
    DBPath = Server.MapPath("customer.mdb")
    dbconnection.Open "provider=microsoft.jet.oledb.4.0;data source="&dbpath
    SQL="select * from auth where id="" & user_id &"""
    SET uplist=dbconnection.EXECUTE(SQL)


3.
    DBPath = Server.MapPath("customer.mdb")
    set session("rs")=Server.CreateObject("ADODB.Recordset")
"   rs=Server.CreateObject("ADODB.Recordset")
    connstr="provider=microsoft.jet.oledb.4.0;data source="&dbpath
    SQL="select * from auth where id="" & user_id &"""
    session("rs").Open sql,connstr,1,3


4.
建odbc源xxx
set conn=server.createobject("Adodb.connection")
conn.open "DSN=xxx;UID=;PWD=;Database=customer

二、SQL

ASP與SQL數據庫連接代碼

//第一種寫法:
MM_conn_STRING = "Driver={SQL Server};server=(local);uid=sa;pwd=;database=infs;"
Set conn = Server.Createobject("ADODB.Connection")
conn.open MM_conn_STRING
SET RS=SERVER.CreateObject("ADOBD.recordset")
SQL="SELECT * FROM TABLE ORDER BY ID DESC"
RS.open SQL,CONN,3,3 

//第二種寫法:(DSN連接)

MM_conn_STRING="DSN=BBS;UID=SA;PWD=12345"
Set conn = Server.Createobject("ADODB.Connection")
conn.open MM_conn_STRING
SET RS=SERVER.CreateObject("ADOBD.recordset")
SQL="SELECT * FROM TABLE ORDER BY ID DESC"
RS.open SQL,CONN,3,3  // 3,3是修改、刪除、增加開關!

//第三種寫法:

MM_conn_STRING_own = "Driver={SQL Server};server=(local);uid=sa;pwd=11111;database=infs;"
Set conn = Server.Createobject("ADODB.Connection")
conn.open MM_conn_STRING_own

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章