在ASP中清除代码
作者:未知 来源:青苹果电脑工作室 添加时间:2006-5-21 10:31:14(response.write)去掉上下文转换。在本例中关键是要为移植准备代码。在实际工作时,
你很有可能要将代码进一步分离。
< % @ LANGUAGE=VBScript % >
< %
'Example of Inline code
Option Explicit
'Declare variables
Dim oConn 'as ADODB.Connection
Dim oRS 'as ADODB.Recordset
Dim x 'as Integer
Private Sub Main()
Dim ConnectionString 'as String
Dim SQLQuery 'as String
ConnectionString = "DSN=Northwind;"
SQLQuery = "Select * from Products"
Call CreateProductRecordset(ConnectionString, SQLQuery)
End Sub
Private Function CreateProductRecordset(ConnectionString, SQLQuery) 'as Recordset
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open ConnectionString
Set oRS = Server.CreateObject("ADODB.Recordset")
'Use with ASP 2.0+
With oRS
.ActiveConnection = oConn
.Source = SQLQuery
.Open
End With
End Function
Call Main()
% >
< HTML >
< HEAD >
< meta http-equiv="Content-Type" content="text/html; charset=windows-1252" >
< TITLE >New Page 1< /TITLE >
< /HEAD >
< BODY >
< H1 >Products< /H1 >
< table cellspacing="2" cellpadding="5" >
< tr bgcolor="#FF6666" >
< th >Product Name< /th >
< th >Quantity Per Unitr< /th >
< th >Price< /th >
< /tr >
< %
Do until oRS.EOF
If x = 1 then
x = 0
Response.Write "< tr bgcolor=""#ffcccc"" >" & vbcrlf
else
Response.Write "< tr >" & vbcrlf
x = 1
end if
Response.Write "< td >" & oRS("ProductName") & "< /td >" & vbcrlf
Response.Write "< td >" & oRS("QuantityPerUnit") & "< /td >" & vbcrlf
Response.Write "< td >" & oRS("UnitPrice") & "< /td >" & vbcrlf
Response.Write "< /tr >" & vbcrlf
oRS.MoveNext
Loop
% >
< /TABLE >
< /BODY >
< /HTML >
< %
'Destroy objects
Set oRS = Nothing
Set oConn = Nothing
% >
站内搜索