home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / database_search.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  3.6 KB  |  106 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. ' Declare our variables... always good practice!
  15. Dim strURL     ' The URL of this page so the form will work
  16.                ' no matter what this file is named.
  17.  
  18. Dim cnnSearch  ' ADO connection
  19. Dim rstSearch  ' ADO recordset
  20. Dim strDBPath  ' path to our Access database (*.mdb) file
  21.  
  22. Dim strSQL     ' The SQL Query we build on the fly
  23. Dim strSearch  ' The text being looked for
  24.  
  25. ' Retreive the URL of this page from Server Variables
  26. strURL = Request.ServerVariables("URL")
  27.  
  28. ' Retreive the term being searched for.  I'm doing it on
  29. ' the QS since that allows people to bookmark results.
  30. ' You could just as easily have used the form collection.
  31. strSearch = Request.QueryString("search")
  32.  
  33. ' Since I'm doing this all in one page I need to see if anyone
  34. ' has searched for something.  If they have we hit the DB.
  35. ' O/W I just show the search form and quit.
  36.  
  37. %>
  38. <p>Search our sample db by first or last name.  (% returns all)</p>
  39. <form action="<%= strURL %>" method="get">
  40. <input name="search" value="<%= strSearch %>" />
  41. <input type="submit" />
  42. </form>
  43. <p>[Try 'am' or 'er' for an example]</p>
  44. <%
  45. If strSearch <> "" Then
  46.     ' MapPath of virtual database file path to a physical path.
  47.     ' If you want you could hard code a physical path here.
  48.     strDBPath = Server.MapPath("database.mdb")
  49.  
  50.  
  51.     ' Create an ADO Connection to connect to the sample database.
  52.     ' We're using OLE DB but you could just as easily use ODBC or a DSN.
  53.     Set cnnSearch = Server.CreateObject("ADODB.Connection")
  54.  
  55.     ' This line is for the Access sample database:
  56.     'cnnSearch.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & ";"
  57.  
  58.     ' We're actually using SQL Server so we use this line instead:
  59.     cnnSearch.Open Application("SQLConnString")
  60.  
  61.     ' Build our query based on the input.
  62.     strSQL = "SELECT last_name, first_name, sales " _
  63.         & "FROM sample " _
  64.         & "WHERE last_name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
  65.         & "OR first_name LIKE '%" & Replace(strSearch, "'", "''") & "%' " _
  66.         & "ORDER BY last_name;"
  67.  
  68.     ' Execute our query using the connection object.  It automatically
  69.     ' creates and returns a recordset which we store in our variable.
  70.     Set rstSearch = cnnSearch.Execute(strSQL)
  71.  
  72.     ' Display a table of the data in the recordset.  We loop through the
  73.     ' recordset displaying the fields from the table and using MoveNext
  74.     ' to increment to the next record.  We stop when we reach EOF.
  75.     ' For fun I'm combining some fields and showwing you can do more then
  76.     ' just spit out the data in the form it is in in the table.
  77.     %>
  78.     <table border="1">
  79.     <tr>
  80.     <th>Name</th>
  81.     <th>Sales</th>
  82.     </tr>
  83.     <%
  84.     Do While Not rstSearch.EOF
  85.         %>
  86.         <tr>
  87.         <td><%= rstSearch.Fields("first_name").Value %> <%= rstSearch.Fields("last_name").Value %></td>
  88.         <td><%= rstSearch.Fields("sales").Value %></td>
  89.         </tr>
  90.         <%
  91.  
  92.         rstSearch.MoveNext
  93.     Loop
  94.     %>
  95.     </table>
  96.     <%
  97.     ' Close our recordset and connection and dispose of the objects
  98.     rstSearch.Close
  99.     Set rstSearch = Nothing
  100.     cnnSearch.Close
  101.     Set cnnSearch = Nothing
  102. End If
  103.  
  104. ' That's all folks!  See it's really not all that hard.
  105. %>
  106.