home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / database.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  3.7 KB  |  130 lines

  1. <%
  2. '*******************************************************
  3. '*     ASP 101 Sample Code - http://www.asp101.com     *
  4. '*                                                     *
  5. '*   This code is made available as a service to our   *
  6. '*      visitors and is provided strictly for the      *
  7. '*               purpose of illustration.              *
  8. '*                                                     *
  9. '* Please direct all inquiries to webmaster@asp101.com *
  10. '*******************************************************
  11. %>
  12.  
  13. <%
  14. Dim DataConn, cmdDC, rsDC
  15. Dim Item
  16. Dim iFieldCount, iLoopVar
  17. Dim strLTorGT, iCriteria, strSortBy, strOrder
  18.  
  19. ' Retrieve QueryString Variables and convert them to a usable form
  20. strLTorGT = Request.QueryString("LTorGT")
  21. Select Case strLTorGT
  22.     Case "LT"
  23.         strLTorGT = "<"
  24.     Case "GT"
  25.         strLTorGT = ">"
  26.     Case Else
  27.         strLTorGT = "<>"
  28. End Select
  29.  
  30. iCriteria = Request.QueryString("criteria")
  31. If IsNumeric(iCriteria) Then
  32.     iCriteria = CLng(iCriteria)
  33. Else
  34.     iCriteria = 0
  35. End If
  36.  
  37. strSortBy = Request.QueryString("sortby")
  38. If strSortBy = "" Then strSortBy = "last_name"
  39.  
  40. strOrder = Request.QueryString("order")
  41. ' Finally we've got all our info, now to the cool stuff
  42.  
  43.  
  44. ' Create and establish data connection
  45. Set DataConn = Server.CreateObject("ADODB.Connection")
  46. DataConn.ConnectionTimeout = 15
  47. DataConn.CommandTimeout = 30
  48.  
  49. 'Access connection code
  50. 'DataConn.Open "DBQ=" & Server.MapPath("database.mdb") & ";Driver={Microsoft Access Driver (*.mdb)};DriverId=25;MaxBufferSize=8192;Threads=20;", "username", "password"
  51.  
  52. 'Our SQL Server code - use above line to use sample on your server
  53. DataConn.Open Application("SQLConnString"), Application("SQLUsername"), Application("SQLPassword")
  54.  
  55.  
  56. ' Create and link command object to data connection then set attributes and SQL query
  57. Set cmdDC = Server.CreateObject("ADODB.Command")
  58. cmdDC.ActiveConnection = DataConn
  59. cmdDC.CommandText = "SELECT * FROM sample WHERE (sales " & strLTorGT & " " & iCriteria & ") ORDER BY " & strSortBy & strOrder & ";"
  60. cmdDC.CommandType = 1
  61.  
  62. ' Create recordset and retrieve values using command object
  63. Set rsDC = Server.CreateObject("ADODB.Recordset")
  64. ' Opening record set with a forward-only cursor (the 0) and in read-only mode (the 1)
  65. rsDC.Open cmdDC, , 0, 1
  66. %>
  67.  
  68. <B>Results of <%= cmdDC.CommandText %></B><BR>
  69.  
  70. <BR>
  71. <TABLE BORDER=1>
  72.     <THEAD>
  73. <% For Each Item in rsDC.Fields %>
  74.         <TH><%= Item.Name %></TH>
  75. <% Next %>
  76.     </THEAD>
  77. <%
  78. ' Loop through recordset and display results
  79. If Not rsDC.EOF Then rsDC.MoveFirst
  80.  
  81. ' Get the number of fields and subtract one so our loops start at 0
  82. iFieldCount = rsDC.Fields.Count - 1
  83.  
  84. ' Continue till we get to the end, and while in each <TR> loop through fields
  85. Do While Not rsDC.EOF
  86. %>    <TR>
  87.         <% For iLoopVar = 0 to iFieldCount %>
  88.         <TD><%= rsDC.Fields(iLoopVar) %></TD>
  89.         <% Next %>
  90.     </TR>
  91. <%
  92. rsDC.MoveNext
  93. Loop
  94. %>
  95. </TABLE>
  96.  
  97. <%
  98. ' Close Data Access Objects and free DB variables
  99.     rsDC.Close
  100.     Set rsDC =  Nothing
  101.     ' can't do a "cmdDC.Close" !
  102.     Set cmdDC = Nothing
  103.     DataConn.Close
  104.     Set DataConn = Nothing
  105. %>
  106.  
  107. <BR>
  108.  
  109. <B>Build your own query:</B>
  110. <FORM ACTION="database.asp" METHOD="get">
  111.     Sales:
  112.     <INPUT TYPE="radio" NAME="LTorGT" VALUE="LT" CHECKED>Less Than</INPUT>
  113.     <INPUT TYPE="radio" NAME="LTorGT" VALUE="GT">Greater Than</INPUT>
  114.     <INPUT TYPE="text" NAME="criteria" VALUE="4500" SIZE=10></INPUT>
  115.     <BR>
  116.     Sort By:
  117.     <SELECT NAME="sortby">
  118.     <OPTION VALUE="id">ID
  119.     <OPTION VALUE="last_name">Last Name
  120.     <OPTION VALUE="first_name">First Name
  121.     <OPTION VALUE="sales">Sales
  122.     </SELECT>
  123.     <SELECT NAME="order">
  124.     <OPTION VALUE="">Ascending
  125.     <OPTION VALUE=" DESC">Descending
  126.     </SELECT>
  127.     <BR>
  128.     <INPUT TYPE="submit" VALUE="Run Query"></INPUT>
  129. </FORM>
  130.