Connection.Execute and Command.Execute

Command.Execute is more versatile, gives you more options, is probably more efficient. It allows you to setup parameters for your sql statements, stored procedures, create Prepared statements etc. You can retrieve information from stored procedure Output and Return Value parameters. It is more work to implement but in my mind worth the effort. It is not limited to stored procedures.
Personally, I never use Connection.Execute.
 
The main reason I use Command.Execute is because I hate writing (and reading) this type of code.

strSQL = "Insert Into SomeTable (Field1, Field2, Field3) Values( " _
            & SomeLongVariable & ", '" & SomeStringVariable & "','" _
            & Format$(SomeDateVariable,"yyyymmdd") & "'"

Connection.Execute strsql 


I prefer to write this type of code. Again, more work but in my opinion, its worth it.

strSQL = "Insert Into SomeTable (Field1, Field2, Field3) Values(?,?,?)"

With Command
.CommandText = strsql
.CommandType = adCmdText
.Parameters.Append .CreateParameter("Field1", adInteger,adParamInput)
.Parameters.Append .CreateParameter("Field2", advarchar,adParamInput,20)
.Parameters.Append .CreateParameter("Field3", adDBTimeStamp,adParamInput)
End With

'Then when its time to execute.

With Command
Set .ActiveConnection = SomeConnection
.Parameters(0).Value = SomeLongVariable
.Parameters(1).Value = SomeStringVariable
.Parameters(2).Value = SomeDateVariable
.Execute lngRowsAffected, , adExecuteNoRecords
End With


There r some points by which one can say that command.execute is faster than connection.execute

1.Command.Prepared = true prepared the temp. store proc, query plan for the specified query or SP. Hence due to this the further execution speeds up the exection time.
2.Connection object is only one we suppose to use to open the connection. hence any execution fired will be in pipeline/ or any execution have to wait for rest all execution gets completed.

Might be these points are not exactly true.... If u r having the thorough knowledge of this and if u r very sure then plz tell me which one is faster and why?

No comments: