1

I am using ADO/Visual C++ to access SQL Server database engine. I find both the connection and command object has Execute method, as below:

https://msdn.microsoft.com/en-us/library/ms675023%28v=vs.85%29.aspx (ADO Connection) https://msdn.microsoft.com/en-us/library/ms681559%28v=vs.85%29.aspx (ADO Command)

Both will execute the SQL query and return a recordset.

In that case, what is the difference between them and why MS will provide two functions with same functionalities?

1 Answer 1

1

I consider it to be about flexibility and the speed of object creation (fewer lines of code for you to type). If you need to send a query to SQL with less overhead, use a Connection. If you want to create a Command and leverage parameters (strongly encouraged), or other features of the Command, then great - use that. There's more overhead, but the features on offer are better.

Just use the Command - it's much better.

2
  • 1
    I think it's important to point out that the overhead is miniscule, and given the dangers of sql injection, it's a good idea in most cases to use a command with parameters. If you're executing sql with no parameters, and that sql comes from a trusted source, then using the connection.execute results in less lines of code. Less code is a good thing, so in that case is probably the better choice.
    – Aheho
    Commented Jan 1, 2016 at 13:50
  • Yup. The speed is almost whitely design-time speed.
    – Rob Farley
    Commented Jan 1, 2016 at 17:44

Not the answer you're looking for? Browse other questions tagged or ask your own question.