程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> 如何從數據庫得到一個列表表單

如何從數據庫得到一個列表表單

編輯:ASP技巧

<HEAD>
<TITLE>ASPHole - Fill List Box Example</TITLE>
</HEAD>
<BODY>
<FORM METHOD=POST>
Country: <SELECT NAME="Country">
<%
' Construct path to database
sPath = Request.ServerVariables("Path_Translated")
sPath = Left(sPath,InStrRev(sPath,"\")) & "CountrIEs.mdb"
'
' Open Connection & Recordset
set oSample = Server.CreateObject("ADODB.Connection")
oSample.Open _
"PRovider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=TRUE;" & _
"Data Source=" & sPath, "Admin", ""
'
' Check for default...
mCountry = Trim(Request("Country"))
'
' Create the List
Set oRS=oSample.Execute _
("SELECT ID,COUNTRY " & _
"FROM COUNTRIES " & _
"ORDER BY ID")
DO WHILE NOT oRS.EOF
mSelected = ""
IF mCountry=trim(oRS("Country")) then mSelected=" SELECTED"
%>
<OPTION<%=mSelected%>><%=oRS("Country")%></OPTION>
<%
oRS.MoveNext
Loop
%>
</SELECT><BR>
<INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</Html>

 

Key points of the sample:

sPath is used to construct the path to the database based on the home directory of the script. This means
that the database must be in the same directory as the script., otherwise, set sPath to the absolute path
of the database.

The database is assumed to a be an Access 2000 Database with a table Countries with a character fIEld
Country.

Forms are assumed to point at the same script which created it unless action is specifIEd.

The IF mCountry=... statement is used to insert the Word SELECTED into the OPTION containing the previous
country value, should one have been passed in by a submit.

If you have problems running this script, your database drivers may be out of date. Go to
http://www.microsoft.com/data for the current MDAC RTM.

-END-

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved