Is it a best practice to use SP (store Proc) for every single SQL call in .NET applications?
Is it encouraged for performance reasons and to reduce surface area for SQL injection attacks (in web applications)?
Is it a best practice to use SP (store Proc) for every single SQL call in .NET applications?
Is it encouraged for performance reasons and to reduce surface area for SQL injection attacks (in web applications)?
No.
If you send your queries to SQL Server as parameterized queries, SQL Server will cache the execution plan AND will sanitize your parameter inputs properly to avoid SQL injection attacks.
It's just one way of doing things. Upsides include keeping all your SQL code in one place, procs being verified for syntax at creation time, and being able to set permissions on procs, which usually represent some kind of "action" and are well suited to a conceptual security model.
Downsides include massive numbers of procs for any medium or larger application, and all the housekeeping that comes with that.
My employer's product uses procs for everything, and I must say with the right practices in place it's quite bearable.
I prefer stored procs over inline SQL, because this way the SQL is one consolidated place; however, I prefer using a tool like nHibernate which will auto generate the SQL for me, then you have no SQL to worry about!
Stored procedures have a few advantages over parameterized queries:
When used exclusively, you can turn off CREATE, INSERT, SELECT, UPDATE, ALTER, DROP, DELETE, etc access for your application accounts, and this way add a small amount of security.
They provide a consistent, manageable interface when you have multiple applications using the same database.
Using procedures allows a DBA to manage and tune queries even after an application is deployed.
Deploying small changes and bug fixes is much simpler.
They also have a few disadvantages:
The number of procedures can quickly grow to the point where maintaining them is difficult, and current tools don't provide a simple method for adequate documentation.
Parameterized queries put the database code next to the place where it's used. Stored procedures keep it far separated, making finding related code more difficult.
Stored procedures are harder to version.
You'll need to weigh those costs/benefits for your system.
There is one more advantage - when it comes to tuning, especially per customer, it can be easily done with SP (by adding hints or even rewriting the code). With embedded SQL it is practically impossible.