In what situations is the SqlCommand preparation method useful?
- If the underlying database supports it and
- If you are going to call the same command (with different parameter values) multiple times. This way, you can save some time because the database can reuse the same query plan.
Typically modern database caching commands and query plans, but assuming your queries or commands are prepared, you can still save some time.
source to share
We're talking about SqlCommand, which implies Microsoft SQL Server. As far as I know, SQL Server has always supported it.
It is not clear, however, if he does anything useful. I haven't found anyone yet who really knows. Many people argue that it is nothing useful and there are no discernible performance differences. I would like to see evidence anyway.
On another database (Firebird using FbCommand object) I know this can cause problems if you're not careful. If you prepare an expression but never use it, the transaction will remain open forever. We had to remove all the pre-made files from our Firebird code. We had a habit of cooking anything that could be done multiple times.
source to share