home *** CD-ROM | disk | FTP | other *** search
- <%
- '*******************************************************
- '* ASP 101 Sample Code - http://www.asp101.com *
- '* *
- '* This code is made available as a service to our *
- '* visitors and is provided strictly for the *
- '* purpose of illustration. *
- '* *
- '* Please direct all inquiries to webmaster@asp101.com *
- '*******************************************************
- %>
-
- <%
- Dim DataConn, cmdDC, rsDC
- Dim Item
- Dim iFieldCount, iLoopVar
- Dim strLTorGT, iCriteria, strSortBy, strOrder
-
- ' Retrieve QueryString Variables and convert them to a usable form
- strLTorGT = Request.QueryString("LTorGT")
- Select Case strLTorGT
- Case "LT"
- strLTorGT = "<"
- Case "GT"
- strLTorGT = ">"
- Case Else
- strLTorGT = "<>"
- End Select
-
- iCriteria = Request.QueryString("criteria")
- If IsNumeric(iCriteria) Then
- iCriteria = CLng(iCriteria)
- Else
- iCriteria = 0
- End If
-
- strSortBy = Request.QueryString("sortby")
- If strSortBy = "" Then strSortBy = "last_name"
-
- strOrder = Request.QueryString("order")
- ' Finally we've got all our info, now to the cool stuff
-
-
- ' Create and establish data connection
- Set DataConn = Server.CreateObject("ADODB.Connection")
- DataConn.ConnectionTimeout = 15
- DataConn.CommandTimeout = 30
-
- 'Access connection code
- 'DataConn.Open "DBQ=" & Server.MapPath("database.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;", "username", "password"
-
- 'Our SQL Server code - use above line to use sample on your server
- DataConn.Open Application("SQLConnString"), Application("SQLUsername"), Application("SQLPassword")
-
-
- ' Create and link command object to data connection then set attributes and SQL query
- Set cmdDC = Server.CreateObject("ADODB.Command")
- cmdDC.ActiveConnection = DataConn
- cmdDC.CommandText = "SELECT * FROM sample WHERE (sales " & strLTorGT & " " & iCriteria & ") ORDER BY " & strSortBy & strOrder & ";"
- cmdDC.CommandType = 1
-
- ' Create recordset and retrieve values using command object
- Set rsDC = Server.CreateObject("ADODB.Recordset")
- ' Opening record set with a forward-only cursor (the 0) and in read-only mode (the 1)
- rsDC.Open cmdDC, , 0, 1
- %>
-
- <B>Results of <%= cmdDC.CommandText %></B><BR>
-
- <BR>
- <TABLE BORDER=1>
- <THEAD>
- <% For Each Item in rsDC.Fields %>
- <TH><%= Item.Name %></TH>
- <% Next %>
- </THEAD>
- <%
- ' Loop through recordset and display results
- If Not rsDC.EOF Then rsDC.MoveFirst
-
- ' Get the number of fields and subtract one so our loops start at 0
- iFieldCount = rsDC.Fields.Count - 1
-
- ' Continue till we get to the end, and while in each <TR> loop through fields
- Do While Not rsDC.EOF
- %> <TR>
- <% For iLoopVar = 0 to iFieldCount %>
- <TD><%= rsDC.Fields(iLoopVar) %></TD>
- <% Next %>
- </TR>
- <%
- rsDC.MoveNext
- Loop
- %>
- </TABLE>
-
- <%
- ' Close Data Access Objects and free DB variables
- rsDC.Close
- Set rsDC = Nothing
- ' can't do a "cmdDC.Close" !
- Set cmdDC = Nothing
- DataConn.Close
- Set DataConn = Nothing
- %>
-
- <BR>
-
- <B>Build your own query:</B>
- <FORM ACTION="database.asp" METHOD="get">
- Sales:
- <INPUT TYPE="radio" NAME="LTorGT" VALUE="LT" CHECKED>Less Than</INPUT>
- <INPUT TYPE="radio" NAME="LTorGT" VALUE="GT">Greater Than</INPUT>
- <INPUT TYPE="text" NAME="criteria" VALUE="4500" SIZE=10></INPUT>
- <BR>
- Sort By:
- <SELECT NAME="sortby">
- <OPTION VALUE="id">ID
- <OPTION VALUE="last_name">Last Name
- <OPTION VALUE="first_name">First Name
- <OPTION VALUE="sales">Sales
- </SELECT>
- <SELECT NAME="order">
- <OPTION VALUE="">Ascending
- <OPTION VALUE=" DESC">Descending
- </SELECT>
- <BR>
- <INPUT TYPE="submit" VALUE="Run Query"></INPUT>
- </FORM>
-