home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / ASP / database_update.asp < prev    next >
Encoding:
Text File  |  2001-06-08  |  5.3 KB  |  160 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 & 2)
  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 iRecordToUpdate ' 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 update
  58. '  4. Update the record
  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 update
  68.     iRecordToUpdate = 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(iRecordToUpdate) Then
  74.         iRecordToUpdate = CLng(iRecordToUpdate)
  75.     Else
  76.         iRecordToUpdate = 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 update.
  82.     strSQL = "SELECT * FROM scratch WHERE id=" & iRecordToUpdate & ";"
  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.     ' The recordset should only have the one record so:
  90.     If Not objRecordset.EOF Then
  91.         objRecordset.MoveFirst
  92.  
  93. 'Step 4:
  94.     'Only update if we've got a record, o/w we never run this
  95.  
  96.         ' String / Text Data Type
  97.         objRecordset.Fields("text_field") = CStr(WeekdayName(WeekDay(Date())))
  98.  
  99.         ' Integer Data Type
  100.         objRecordset.Fields("integer_field") = CInt(Day(Now()))
  101.  
  102.         ' Date / Time Data Type
  103.         objRecordset.Fields("date_time_field") = Now()
  104.  
  105. 'Step 5:
  106.     'Only update if we've got a record, o/w we never run this
  107.         objRecordset.Update
  108.     
  109.         Response.Write "Record id " & iRecordToUpdate & " updated!"
  110.     End If
  111.  
  112. 'Step 6:
  113.     ' Finally we close the recordset and release the memory used by the
  114.     ' object variable by setting it to Nothing (a VBScript keyword)
  115.     objRecordset.Close
  116.     Set objRecordset = Nothing
  117.  
  118.  
  119. '********************************
  120. ' This is the end of the sample!
  121. '********************************
  122.  
  123. 'Show Table
  124.     ' Feel free to skip this area. (Ignore the man behind the curtain!)
  125.     ' I'm just showing the Table so you have something to look at when
  126.     ' you view the sample.
  127.     Dim objCleanUpRS
  128.  
  129.     strSQL = "SELECT * FROM scratch ORDER BY id;"
  130.  
  131.     Set objCleanUpRS = Server.CreateObject("ADODB.Recordset")
  132.     objCleanUpRS.Open strSQL, DB_CONNECTIONSTRING, adOpenStatic, adLockReadOnly, adCmdText
  133.  
  134.     Response.Write "<TABLE BORDER=""1"" CELLSPACING=""2"" CELLPADDING=""2"">" & vbCrLf
  135.     Response.Write vbTab & "<TR>" & vbCrLf
  136.     Response.Write vbTab & vbTab & "<TD><B>id</B></TD>" & vbCrLf
  137.     Response.Write vbTab & vbTab & "<TD><B>text_field</B></TD>" & vbCrLf
  138.     Response.Write vbTab & vbTab & "<TD><B>integer_field</B></TD>" & vbCrLf
  139.     Response.Write vbTab & vbTab & "<TD><B>date_time_field</B></TD>" & vbCrLf
  140.     Response.Write vbTab & "</TR>" & vbCrLf
  141.  
  142.     If Not objCleanUpRS.EOF Then
  143.         objCleanUpRS.MoveFirst
  144.         'Show data
  145.         Do While Not objCleanUpRS.EOF
  146.             Response.Write vbTab & "<TR>" & vbCrLf
  147.             For I = 0 To objCleanUpRS.Fields.Count - 1
  148.                 Response.Write vbTab & vbTab & "<TD><a href=""db_update.asp?id=" & objCleanUpRS.Fields("id").Value & """>" & objCleanUpRS.Fields(I) & "</a></TD>" & vbCrLf
  149.             Next
  150.             Response.Write vbTab & "</TR>" & vbCrLf
  151.             objCleanUpRS.MoveNext
  152.         Loop
  153.     End If
  154.     Response.Write "</TABLE>" & vbCrLf
  155.  
  156.     objCleanUpRS.Close
  157.     Set objCleanUpRS = Nothing
  158.  
  159. %>
  160.