home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / database_count.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  4.3 KB  |  121 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. ' BEGIN CONSTANT DEFINITION
  15.  
  16.     ' The following command includes the ADODB VBScript constants file.
  17.     ' If you can't find your copy you can download a copy from:
  18.     ' http://www.asp101.com/samples/download/adovbs.inc
  19.     ' It may not be the most recent copy so use it at your own risk.
  20.  
  21.     %>
  22.     <!-- #INCLUDE FILE="./download/adovbs.inc" -->
  23.     <%
  24.  
  25.     ' DB Configuration variables
  26.     ' After this, strictly used as if it were a Const.
  27.     Dim DB_CONNSTRING
  28.     DB_CONNSTRING = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.Mappath("./db_scratch.mdb") & ";"
  29.  
  30.     ' Now we override the above setting so the sample uses our SQL server.
  31.     ' Comment out the following line to use the sample Access DB.
  32.     DB_CONNSTRING = Application("SQLConnString") & _
  33.         "UID=" & Application("SQLUsername") & ";" & _
  34.         "PWD=" & Application("SQLPassword") & ";"
  35.  
  36. ' END CONSTANT DEFINITION
  37. %>
  38.  
  39. <%
  40. Dim rsCount ' The recordset object
  41.  
  42. ' Create an instance of an ADO Recordset
  43. Set rsCount = Server.CreateObject("ADODB.Recordset")
  44.  
  45. ' Open RS
  46. ' I'm actually doing this directly (without a connection object) to keep
  47. ' the code short and to the point.  I'm opening the RS with a static
  48. ' cursor, read only, and telling it that "scratch" is a table name and
  49. ' not a SQL command.  If I don't specify how to open the rs, I'd get the
  50. ' default cursor type which doesn't support .RecordCount!
  51. rsCount.Open "scratch", DB_CONNSTRING, adOpenStatic, adLockReadOnly, adCmdTable
  52.  
  53. ' Show RecordCount
  54. ' I dress it up and pop it into the middle of a sentence, but you can
  55. ' do whatever you want with it.
  56. Response.Write "This table currently has <B><FONT COLOR=""#FF0000"">"
  57. Response.Write rsCount.RecordCount ' This is the line that does it!
  58. Response.Write "</FONT></B> records in it!<BR>" & vbCrLf
  59.  
  60.  
  61. '======================================================================
  62. ' BEGIN TABLE DISPLAY
  63. ' Now I'm going to display the table if they requested it just so you
  64. ' have something to look at!  This really doesn't pertain to the topic
  65. ' of this sample so I'm going to keep the code short but feel free to
  66. ' look it over and if you do please notice the pretty HTML it outputs!
  67. ' Ugly HTML output is a pet peeve of mine!  ;)
  68. If LCase(Request.QueryString("showtable")) = "true" Then
  69.     Dim Field   ' Field Looper for display
  70.     Dim bColor  ' Use for showing alternating colors
  71.     bColor = False
  72.     
  73.     ' Spacers and intro
  74.     Response.Write "<BR>" & vbCrLf & "They are:<BR>" & vbCrLf & "<BR>" & vbCrLf
  75.  
  76.     ' Start the table
  77.     Response.Write "<TABLE BORDER=""1"">" & vbCrLf
  78.  
  79.     ' Write Titles
  80.     Response.Write vbTab & "<TR>" & vbCrLf
  81.     For Each Field in rsCount.Fields
  82.         Response.Write vbTab & vbTab & "<TD BGCOLOR=""#CCCCCC""><B>" & Field.Name & "</B></TD>" & vbCrLf
  83.     Next 'Field
  84.     Response.Write vbTab & "</TR>" & vbCrLf
  85.  
  86.     ' Loop through records outputting data
  87.     Do While Not rsCount.EOF
  88.         Response.Write vbTab & "<TR>" & vbCrLf
  89.         For Each Field in rsCount.Fields
  90.             Response.Write vbTab & vbTab & "<TD BGCOLOR="""
  91.             
  92.             ' Decide what color to output
  93.             If bColor Then
  94.                 Response.Write "#CCCCFF"  ' Light blueish
  95.             Else
  96.                 Response.Write "#FFFFFF"  ' White
  97.             End If
  98.             
  99.             Response.Write """>" & Field.Value & "</TD>" & vbCrLf
  100.         Next 'Field
  101.         Response.Write vbTab & "</TR>" & vbCrLf
  102.  
  103.         ' Toggle our colors
  104.         bColor = Not bColor
  105.         rsCount.MoveNext
  106.     Loop
  107.  
  108.     ' End the table
  109.     Response.Write "</TABLE>" & vbCrLf
  110.     Response.Write "<BR><A HREF=""db_count.asp"">Hide the table</A>" & vbCrLf
  111. Else
  112.     Response.Write "<BR><A HREF=""db_count.asp?showtable=true"">Show the table</A>" & vbCrLf
  113. End If
  114. ' END TABLE DISPLAY - Now back to our regularly scheduled code!
  115. '======================================================================
  116.  
  117. ' Close and dispose of recordset object
  118. rsCount.Close
  119. Set rsCount = Nothing
  120. %>
  121.