asp连接数据库

ASP常用数据库连接方法和技巧

<%
   sqlStr="Driver={SQL SERVER};Server=.;uid=sa;Password=123456;DataBase=MJDB"
    set sqlCon = Server.CreateObject("ADODB.Connection")
   set myRs=server.createobject("adodb.recordset")
   mySql="select * from macinfo"
   sqlCon.Open sqlStr
   myRs.open mySql,sqlCon,1,3
   do while not myRs.eof
   Response.write myRs("Remark")&"<br/>"
   myRs.movenext
   loop
   myRs.close
   sqlCon.close   

%>
//第一种写法:
MM_conn_STRING =
"Driver={SQL SERVER};Server=.;UID=sa;Password=;DataBase=KK;"
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

posted @ 2007-06-12 09:31  Microbar  阅读(197)  评论(0编辑  收藏