« CSS Text Wrap: 可以让网页的文字以任意线形方式布局http-https的转换和相对URLs »

Access使用DSN方式和不用DSN连接数据库

前些天买了EasyCGI(美国的IDC)的一个windows虚拟主机,在安装时遇到了一些麻烦,美国的虚拟主机对目录的访问权限控制的比较严格,还有数据库的连接方式也比较符合规范,不像国内的IDC,随便上网粘个代码就可以连上数据库了,最后几经折腾,设置好了数据库所在目录的写权限,按照他们给的连接数据库的示范代码,终于搞定了,下面我把他们连接数据库的几种方式拿出来分享一下,使用他们的例子,我想不管在哪个国家的IDC都可以通行了。

下面是不使用DSN的方式:

<%
Dim objConn, dbPath, rsRecord, strSQL, tableName

Set objConn = Server.CreateObject("ADODB.Connection")

dbPath = "/db/test.mdb"     'The virtual path to your database file
tableName = "testit1"     'The table in the database to display

objConn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath(dbPath)
Set rsRecord = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM " & tableName
rsRecord.Open strSQL, objConn

Response.Write "<TABLE BORDER=1>"
'Write out Field Names
Response.Write "<TR>"
For i=0 to rsRecord.fields.count-1
     Response.Write "<TH>"+rsRecord(i).Name+"</TH>"
Next

Response.Write "</TR>"
'Write out Data
Do while not rsRecord.eof
     Response.Write "<TR>"
     For i=0 to rsRecord.fields.count-1
          Response.Write "<TD>"
          Response.Write rsRecord(i)
          Response.Write "</TD>"
     Next
     Response.Write "</TR>"
     rsRecord.movenext
Loop

rsRecord.Close
objConn.Close
Set rsRecord = Nothing
Set objConn = Nothing

%>

 下面是使用DSN连接Access:

<%

Dim objConn, rsRecord, strSQL, tableName, dsnName, dsnUser, dsnPass

Set objConn = Server.CreateObject("ADODB.Connection")

dsnName = "testdsn"     'The name of the DSN
dsnUser = "testuser"     'The username for the DSN
dsnPass = "testPass"     'The password for the DSN
tableName = "testit1"     'The table in the database to display

objConn.Open "DSN="&dsnName, dsnUser, dsnPass
Set rsRecord = Server.CreateObject("ADODB.Recordset")
strSQL = "SELECT * FROM " & tableName
rsRecord.Open strSQL, objConn

Response.Write "<TABLE BORDER=1>"

'Write out Field Names
Response.Write "<TR>"
For i=0 to rsRecord.fields.count-1
     Response.Write "<TH>"+rsRecord(i).Name+"</TH>"
Next
Response.Write "</TR>"

'Write out Data
Do while not rsRecord.eof
     Response.Write "<TR>"
     For i=0 to rsRecord.fields.count-1
          Response.Write "<TD>"
          Response.Write rsRecord(i)
          Response.Write "</TD>"
     Next
     Response.Write "</TR>"
     rsRecord.movenext
Loop

rsRecord.Close
objConn.Close
Set rsRecord = Nothing
Set objConn = Nothing
%>

原创文章如转载,请注明:转载自悠悠博客 [ http://www.ajaxstu.com/ ]

相关文章:

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。