Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Change Information in a Database from ASP


View products that this article applies to.

This article was previously published under Q188713

↑ Back to the top


Summary

There are two different methods for performing updates and inserts. One way is to create a recordset and then insert/update its records. The other way is to use the Execute method to issue a SQL statement which inserts/updates the records.

↑ Back to the top


More information

The following code illustrates opening a recordset and then adding and altering its records. The current location in the recordset determines which record will be updated.
 <%
   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Open Session("DSN=MyDSN")
   Set rs = Server.CreateObject("ADODB.Recordset")
   rs.Open "MyTable", conn, 1, 3, 2  ' Make sure the LockType
                                     ' allows for insertions and updates
   ' Insert a record
   rs.AddNew
   rs("Field1") = Value1
   rs.Update

   ' Update the current record
   rs("Field1") = Value2
   rs.Update

   rs.Close
   %>
				
The next code illustrates inserting and updating records through the Execute method. The "Where" clause is used to specify which records will be updated. Notice that you do not work directly with a recordset.
   <%
   Set conn = Server.CreateObject("ADODB.Connection")
   conn.Open "DSN=MyDSN"

   ' Insert a record
   conn.Execute("INSERT INTO MyTable(Field1,Field2) VALUES (Value1,
     Value2))

   ' Update a record
   conn.Execute("UPDATE MyTable SET Field1 = Value1 WHERE Field1 = Value2")

   conn.Close
   %>
				

↑ Back to the top


Keywords: KB188713, kbhowto, kbdatabase, kbcodesnippet

↑ Back to the top

Article Info
Article ID : 188713
Revision : 8
Created on : 8/11/2005
Published on : 8/11/2005
Exists online : False
Views : 554