Friday, December 14, 2007

How to execute batch updates with ADO?

How to execute batch updates with ADO?

Normally, when we process a task, we need to update to the database. For example:

<%

rs.AddNew

rs.fields("MemberName") = "C.K."

rs.fields("Location") = "SG"

rs.Update

%>

But, can we perform few tasks, and update only one time?

The ASP codes are written and described as below:

<%

const adLockBatchOptimistic=4 'Use only when doing the batch processing.

Set conn = Server.CreateObject("Adodb.Connection")

conn.Open "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("db1.mdb")

Set rs = Server.CreateObject("Adodb.Recordset")

rs.open "Member",conn,1,adLockBatchOptimistic 'Open a Recordset that can perform batch processing

rs.AddNew

rs.Fields("MemberName")="Alvin"

rs.Fields("Location")="UK"

rs.AddNew 'Add another record again

rs.Fields("MemberName")="Eric"

rs.Fields("Location")="UK"

rs.UpdateBatch 'Save all new data.

rs.Close

Set rs = Nothing

%>


If you want to cancel the process, you can use the CancelBatch to cancel it.

No comments: