There are actually several ways to update a Database table with ASP - two that come to mind quickly are<br>
1. using the Execute method on the Connection object and passing an UPDATE sql statement to the DB or<br>
2. using the Update method on the Recordset object passing new field values.<br>
<br>
You asked for the best but you didn't specify your criteria... Best (easiest) is using the Connection object's execute method, best (fastest) is probably using the Recordset object. That's just what I've read, I haven't benchmarked it.<br>
<br>
Anyway, I've provided a code example below that uses the first method I describe, Connection.Execute.<br>
<br>
This requires the nwind.mdb to be copied into your C:\temp folder, or modify the Connection string (dsn variable) to point to the location of your Nwind.mdb.<br>
<br>
<br>
<br>
<%@ language="VBScript"%><br>
<% Option Explicit %><br>
<br>
<%<br>
<br>
Sub UpdateDatabase(sql, dsn)<br>
On Error Resume Next<br>
Dim oCn<br>
Set oCn = Server.Createobject("ADODB.Connection")<br>
oCn.Open dsn<br>
oCn.Execute sql<br>
oCn.Close<br>
Set oCn = Nothing<br>
If Err.number <> 0 Then<br>
Response.Write "<br>" & Err.Number & " " & Err.Description & "<br>" & sql & "<br>"<br>
End If<br>
End Sub<br>
<br>
%><br>
<br>
<html><br>
<head><br>
<title></title><br>
</head><br>
<body><br>
<br>
<%<br>
Dim dsn, sql<br>
dsn="DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\temp\nwind.mdb"<br>
sql = "update customers set city = 'Berlin2' where city = 'Berlin'"<br>
UpdateDatabase sql, dsn<br>
%><br>
<br>
</body><br>
</html>
hello all<br>
i have a question<br>
if the client wants to update a selective rows freom the sql server database how should we go about it. that too if we have to show a record by record to the client and ask him to update it. please suggest me the code also.<br>
i will be very thankful.<br>
each record should have a unique ID. Use a self-submitting Form to get the User's request. the ASP code (using ADO and MSDE) is this<br>
<br>
If Request.Form("btnSubmit" = "Submit" Then<br>
Dim objConn, objRs, StrSql, IdFromForm<br>
IdFromForm = Request.Form("RequestedID"<br>
<br>
StrSql = "select * from dbase where ID like '" &_<br>
IdFromForm & "'"<br>
<br>
Set objConn = Server.CreateObject("ADODB.Connection"<br>
Set objRs = Server.CreateObject("ADODB.Recordset"<br>
<br>
objConn.Open "Provider=SQLOLEDB;User ID=sa; " &_<br>
"Initial File Name=C:\data\base\source.mdf"<br>
objRs.Open StrSql, objConn, 3, 3<br>
If Not objRs.EOF Then<br>
'Here the updating to the fields go<br>
<br>
End If<br>
objRs.update<br>
objRs.close<br>
objConn.close<br>
End If<br>
<br>
If you have multiple rows to update, use a loop and make sure your cursor is dynamic.<br>
<br>
The most efficent way to show record by record update is to parse all of the updates into an Array that is relative to the RS(I really don't know how you want to display Record by Record updating). and then update the recordset. Something like this.<br>
<br>
If Not objRs.BOF Then<br>
objRs.MoveFirst<br>
End If<br>
numrows = ubound(DataArray, 2)<br>
For i = 0 To numrows<br>
uniqueId = DataArray(5,i) 'Whatever the ID is<br>
If CInt(uniqueId) = CInt(objRs.Fields("ID") Then<br>
objRs.Fields("FirstField" = DataArray(1,i)<br>
objRs.Fields("SecondField" = DataArray(2,i)<br>
'////.....etc<br>
End If<br>
objRs.MoveNext<br>
Next<br>
objRs.Update<br>
<br>
<br>
<br>
Hope This Helped,<br>
Jeremy Lowery MCP<br>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.