Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 188713 - Last Review: August 11, 2005 - Revision: 4.4
How To Change Information in a Database from ASP
This article was previously published under Q188713
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.
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
%>
APPLIES TO
- Microsoft Active Server Pages 4.0
- Microsoft Visual Studio 6.0 Enterprise Edition
- Microsoft Visual Studio 97 Service Pack 3
- Microsoft Internet Information Server 4.0
- Microsoft Internet Information Services 5.0
- Microsoft Data Access Components 2.5
- Microsoft Data Access Components 2.6
| kbcodesnippet kbdatabase kbhowto KB188713 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate