Update a SQL Server table
How does the code below work? I've never seen an UPDATE done this way. I always do it this way:
http://www.w3schools.com/sql/sql_update.asp
sqlText = "SELECT * FROM pricing WHERE pid=1"
rsx.Open sqlText, cnx, 1, 2
if Not rsx.EOF then
rsx("new_us") = Request.Form("new_us")
rsx("new_us_desc") = Rtrim(Request.Form("new_us_desc"))
rsx("new_ca") = Request.Form("new_ca")
rsx("new_ca_desc") = Rtrim(Request.Form("new_ca_desc"))
rsx("new_int") = Request.Form("new_int")
rsx("new_int_desc") = Rtrim(Request.Form("new_int_desc"))
rsx("conv_us") = Request.Form("conv_us")
rsx("conv_us_desc") = Rtrim(Request.Form("conv_us_desc"))
rsx("conv_ca") = Request.Form("conv_ca")
rsx("conv_ca_desc") = Rtrim(Request.Form("conv_ca_desc"))
rsx("conv_int") = Request.Form("conv_int")
rsx("conv_int_desc") = Rtrim(Request.Form("conv_int_desc"))
rsx("ren_us") = Request.Form("ren_us")
rsx("ren_us_desc") = Rt开发者_高级运维rim(Request.Form("ren_us_desc"))
rsx("ren_ca") = Request.Form("ren_ca")
rsx("ren_ca_desc") = Rtrim(Request.Form("ren_ca_desc"))
rsx("ren_int") = Request.Form("ren_int")
rsx("ren_int_desc") = Rtrim(Request.Form("ren_int_desc"))
rsx.Update
end if
rsx.Close
cnx.Close
Set rsx = Nothing
Set cnx = Nothing
Thanks
Brett
First you execute the query with rsx.Open
. cnx
is the connection to the db, parameter value 1 is CursorType = adOpenKeyset
and parameter value 2 is LockType = adLockPessimistic
.
sqlText = "SELECT * FROM pricing WHERE pid=1"
rsx.Open sqlText, cnx, 1, 2
rsx
is a Recordset
(com object) that holds the returned rows. if Not rsx.EOF then
checks that you actually got at least one row, otherwise the edit will fail.
This rsx("new_us") = Request.Form("new_us")
assign new values to fields in the Recordset
.
Finally rsx.Update
saves the modified fields to the db. The OLE DB Provider used by cnx
builds the update statement that is sent to the database.
So basic work flow is:
- Fetch a row from db
- Modify row
- Send the row back to db
This : http://www.w3schools.com/ado/ado_ref_recordset.asp should help ;)
It is a ADO Recordset (not ADO.NET). Through the Recordset (rsx in this case), you have access to the fields of your query. By changing the value, you can update the database through the Update method.
精彩评论