Ado Update in ASP
In Classic ASP, using ADO, you can update records in a database either by executing an SQL UPDATE statement or by modifying fields in an editable Recordset and calling .Update.
Two common ways to do an ADO Update in ASP:
1. Using SQL UPDATE statement with Connection.Execute
<%Dim conn, sqlSet conn = Server.CreateObject("ADODB.Connection")conn.Open "Provider=SQLOLEDB;Data Source=SERVER;Initial Catalog=DB;User ID=USER;Password=PASS;"sql = "UPDATE Users SET Email = 'newemail@example.com' WHERE UserID = 5"conn.Execute sqlconn.CloseSet conn = Nothing%>2. Using Recordset to update fields
<%Dim conn, rs, sqlSet conn = Server.CreateObject("ADODB.Connection")conn.Open "Provider=SQLOLEDB;Data Source=SERVER;Initial Catalog=DB;User ID=USER;Password=PASS;"sql = "SELECT * FROM Users WHERE UserID = 5"Set rs = Server.CreateObject("ADODB.Recordset")rs.Open sql, conn, 1, 3 ' adOpenKeyset, adLockOptimisticIf Not rs.EOF Then rs("Email") = "newemail@example.com" rs.UpdateEnd Ifrs.CloseSet rs = Nothingconn.CloseSet conn = Nothing%>Notes:
When updating via Recordset, use a cursor that supports updates (e.g.,
adOpenKeyset) and a lock type that allows changes (adLockOptimisticoradLockPessimistic).conn.Executeis simpler and efficient for direct SQL updates.Always sanitize inputs to prevent SQL Injection if using SQL strings.
Need help with parameterized updates or using stored procedures for updating?