home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / database_del.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  6.7 KB  |  197 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. <%' Defining some constants to make my life easier! (Same as Sample 1)
  14. ' Begin Constant Definition
  15.     
  16.     ' DB Configuration constants
  17.     ' Fake const so we can use the MapPath to make it relative.
  18.     ' After this, strictly used as if it were a Const.
  19.     Dim DB_CONNECTIONSTRING
  20.  
  21.     ' ODBC
  22.     'DB_CONNECTIONSTRING = "DRIVER={Microsoft Access Driver (*.mdb)};" _
  23.     '    & "DBQ=" & Server.Mappath("./db_scratch.mdb") & ";"
  24.  
  25.     ' OLE DB
  26.     DB_CONNECTIONSTRING = "Provider=Microsoft.Jet.OLEDB.4.0;" _
  27.         & "Data Source=" & Server.Mappath("db_dsn.mdb") & ";"
  28.  
  29.     ' We don't use these, but we could if we neeeded to.
  30.     'Const DB_USERNAME = "username"
  31.     'Const DB_PASSWORD = "password"
  32.  
  33.     'Now we override the above settings to use our SQL server.
  34.     'Delete the following line to use the sample Access DB.
  35.     DB_CONNECTIONSTRING = Application("SQLConnString")
  36.  
  37.     ' ADODB Constants
  38.     ' You can find these in the adovbs.inc file
  39.     ' Do a search for it and it should turn up somewhere on the server
  40.     ' If you can't find it you can download our copy from here:
  41.     '     http://www.asp101.com/samples/download/adovbs.inc
  42.     ' It may not be the most recent copy so use it at your own risk.
  43. %>
  44.     <!-- #INCLUDE FILE="adovbs.inc" -->
  45. <%
  46. ' End Constant Definition
  47. %>
  48.  
  49. <%
  50. Dim I               ' Standard looping var
  51. Dim iRecordToDelete ' Id of deleted record
  52. Dim strSQL          ' String variable for building our query
  53.  
  54. 'We're going to keep this as simple as we can.
  55. '  1. Create a Recordset object
  56. '  2. Connect the Recordset to the table
  57. '  3. Find the record to delete
  58. '  4. Delete It!
  59. '  5. Update the table
  60. '  6. Close the Recordset
  61.  
  62. 'Step 1:
  63.     Dim objRecordset
  64.     Set objRecordset = Server.CreateObject("ADODB.Recordset")
  65.  
  66. 'Step 2:
  67.     ' Get the Id of the record to delete 
  68.     iRecordToDelete = Request.QueryString("id")
  69.     
  70.     ' If the record ID passed in isn't a number, we set it to
  71.     ' one so we don't cause SQL query errors.  I use 0 since I
  72.     ' know there's no record in the DB with an id of 0
  73.     If IsNumeric(iRecordToDelete) Then
  74.         iRecordToDelete = CLng(iRecordToDelete)
  75.     Else
  76.         iRecordToDelete = 0
  77.     End If
  78.     
  79.     ' I'm prebuilding our SQL query so it's easier to print
  80.     ' out in case we need to debug later.  I'm using a query
  81.     ' that will return just the record we want to delete.
  82.     strSQL = "SELECT * FROM scratch WHERE id=" & iRecordToDelete & ";"
  83.  
  84.     ' The syntax for the open command is
  85.     ' recordset.Open Source, ActiveConnection, CursorType, LockType, Options 
  86.     objRecordset.Open strSQL, DB_CONNECTIONSTRING, adOpenKeyset, adLockPessimistic, adCmdText
  87.  
  88. 'Step 3:        
  89.     ' Not really much to do here!
  90.     ' We're looking to delete the only record from the current recordset.
  91.     ' The first one is the one to delete.
  92.  
  93.     ' Note: If the data was a little more important (or of any value at all
  94.     ' to us!), we'd probably check some other criteria or at least check to see
  95.     ' if it's the oldest record in the recordset.  Since it's not and we really
  96.     ' don't care, here goes!
  97.  
  98. 'Step 4:
  99.     'Make sure we actually have a record to delete.
  100.     If Not objRecordset.EOF Then
  101.         objRecordset.MoveFirst
  102.         objRecordset.Delete adAffectCurrent
  103.  
  104.         ' You can also delete groups of records which satisfy the filter
  105.         ' property setting if you're doing this in batch mode.  For this
  106.         ' situation we're just killing the one record so we don't bother.
  107.     End If
  108.  
  109. 'Step 5:
  110.     ' We don't need to do the update unless we batch it like mentioned above!
  111.     'objRecordset.UpdateBatch
  112.     
  113.     ' Show a message saying we deleted a record
  114.     If iRecordToDelete <> 0 Then
  115.         Response.Write "Record id " & iRecordToDelete & " deleted!"
  116.     End If
  117.  
  118. 'Step 6:
  119.     ' Finally we close the recordset and release the memory used by the
  120.     ' object variable by setting it to Nothing (a VBScript keyword)
  121.     objRecordset.Close
  122.     Set objRecordset = Nothing
  123.  
  124.  
  125.  
  126. '********************************
  127. ' This is the end of the sample!
  128. '********************************
  129.  
  130. 'Show Table
  131.     ' Feel free to skip this area. (Ignore the man behind the curtain!)
  132.     ' I'm just showing the Table so you have something to look at when
  133.     ' you view the sample.
  134.     Dim objCleanUpRS
  135.     Dim iRecordCount
  136.  
  137.     strSQL = "SELECT * FROM scratch ORDER BY id;"
  138.  
  139.     Set objCleanUpRS = Server.CreateObject("ADODB.Recordset")
  140.     objCleanUpRS.Open strSQL, DB_CONNECTIONSTRING, adOpenStatic, adLockReadOnly, adCmdText
  141.  
  142.     Response.Write "<TABLE BORDER=""1"" CELLSPACING=""2"" CELLPADDING=""2"">" & vbCrLf
  143.     Response.Write vbTab & "<TR>" & vbCrLf
  144.     Response.Write vbTab & vbTab & "<TD><B>id</B></TD>" & vbCrLf
  145.     Response.Write vbTab & vbTab & "<TD><B>text_field</B></TD>" & vbCrLf
  146.     Response.Write vbTab & vbTab & "<TD><B>integer_field</B></TD>" & vbCrLf
  147.     Response.Write vbTab & vbTab & "<TD><B>date_time_field</B></TD>" & vbCrLf
  148.     Response.Write vbTab & "</TR>" & vbCrLf
  149.  
  150.     If Not objCleanUpRS.EOF Then
  151.         objCleanUpRS.MoveFirst
  152.         'Show data
  153.         Do While Not objCleanUpRS.EOF
  154.             Response.Write vbTab & "<TR>" & vbCrLf
  155.             For I = 0 To objCleanUpRS.Fields.Count - 1
  156.                 Response.Write vbTab & vbTab & "<TD><a href=""db_del.asp?id=" & objCleanUpRS.Fields("id").Value & """>" & objCleanUpRS.Fields(I) & "</a></TD>" & vbCrLf
  157.             Next
  158.             Response.Write vbTab & "</TR>" & vbCrLf
  159.             objCleanUpRS.MoveNext
  160.         Loop
  161.     End If
  162.     Response.Write "</TABLE>" & vbCrLf
  163.  
  164.     ' Get recordcount so we know if we need to clean up.
  165.     iRecordCount = objCleanUpRS.RecordCount
  166.  
  167.     objCleanUpRS.Close
  168.     Set objCleanUpRS = Nothing
  169.  
  170.  
  171. ' Now this is REALLY behind the curtain!
  172.  
  173. ' Normally I'd cut you off right here and do the rest behind the scenes; however,
  174. ' since this has to do with the DB you were just writing to, I'll give you a
  175. ' treat and let you see some of our administative / housekeeping code!
  176.  
  177. ' Now we clean up!
  178. ' Basically, to keep things manageable, I'm checking the DB to keep it over
  179. ' 2 records.
  180. If iRecordCount <= 2 Then
  181.     Set objCleanUpRS = Server.CreateObject("ADODB.Recordset")
  182.  
  183.     objCleanUpRS.Open strSQL, DB_CONNECTIONSTRING, adOpenStatic, adLockPessimistic, adCmdText
  184.  
  185.     For I = 1 to 2
  186.         objCleanUpRS.AddNew
  187.         objCleanUpRS.Fields("text_field") = CStr(WeekdayName(WeekDay(Date())))
  188.         objCleanUpRS.Fields("integer_field")   = CInt(Day(Now()))
  189.         objCleanUpRS.Fields("date_time_field") = Now()
  190.     Next
  191.     objCleanUpRS.Update
  192.  
  193.     objCleanUpRS.Close
  194.     Set objCleanUpRS = Nothing
  195. End If
  196. %>
  197.