Which is better to use Command object or RecordSet object to add records to the database?
Which one should I choose?
Command is used for parameter transfer, especially batch parameter transfer. The Command object mainly passes parameters to SQL statements and StoredProcude.
Rely on the powerful functions of SQL to complete database operations; and the RecordSet object, we can regard it as encapsulating the data object and providing a series of
methods and properties to simplify database programming.
Through the following demonstration of adding a record to the database using two methods, we can see that these two objects use different methods to deal with some problems:
The RecordSet object seems to be easier to understand, but the performance of Command is superior, especially when adding records in batches.
1. Methods using the Command object:
const adCmdText=&H0001
const adInteger=3
const adVarChar=200
Const adParamInput = &H0001
set conn=Server.CreateObject(ADODB.Connection)
set comm=Server.Createobject(ADODB.Command)
conn.open Driver={Microsoft Access Driver};DBQ=& _
Server.Mappath(/source_asp)&/property/chunfeng.mdb;
comm.ActiveConnection=conn
comm.CommandType=adCmdText
comm.CommandText=insert into chunfeng (ID,Name,) & _
&values(?,?,?)
set param=comm.CreateParameter(ID,adInteger,adParamInput,3,4)
comm.Parameters.Append param
set param=comm.CreateParameter(NAME,adVarChar,adParamInput,255, intels)
comm.Parameters.Append param
comm.Execute
conn.close
2. Methods of using the RecordSet object
const adCmdTable=&H0002
set conn=Server.CreateObject(ADODB.Connection)
set rs=Server.Createobject(ADODB.RecordSet)
conn.open Driver={Microsoft Access Driver (*.mdb)};DBQ=& _
Server.Mappath(/source_asp)&/property/chunfeng.mdb;
rs.ActiveConnection=conn
rs.open chunfeng,,,adCmdTable
rs.addnew
rs(ID)=4
rs(Name)=intels
rs.update
rs.close
conn.close