views:

210

answers:

3

Apologies if this is a duplicate question, but is there a real difference between executing a SQL statement in Linq2Sql compared to executing a Stored Procedure?

What are the benefits? (if any)

+5  A: 

If you put your code into a stored procedure, you've decoupled it from your C# app. You can fix any errors or extend its functionality without having to recompile and redistribute your app.

Also, using stored procedure can be a security measure: you can grant your users execute permission on the stored procs, but no update or insert privileges on the underlying tables. This can make your database more secure and more manageable.

Purely from a functional perspective, I would say (almost) anything you can do in a stored procedure can also be done with inline SQL being executed from your Linq-to-SQL DataContext - but as I mentioned before - in that case, you have all your SQL code in your main app - which might be a good or a bad thing, depending on how your app and your customers work and function.

marc_s
While it is true using a stored procedure de-couples the data logic from your app-code, you can write Linq-to-SQL in a separate data-tier assembly that is compiled separately and accessed across an interface. True, you still have to re-compile/deploy the data access assembly, but it does de-couple the data access logic.
j0rd4n
+5  A: 

If raw speed is the primary concern, then use a DataReader.

Otherwise, LINQ to SQL is a solid alternative. It offers tremendous benefits in terms of strong naming and type safety. It's a major productivity boost. As you learn how to write good LINQ queries, and in particular, learn to use compiled queries where appropriate, you can get very decent performance.

A well-designed LINQ-based program won't be a memory hog, either. Yes, the data context caches query results, but the data context is designed to be a very-short-lived object. If you keep a data context around longer than a single transaction, you're doing it wrong. And if you dispose of them at then end of transactions, then the memory loss is gone.

LINQ to SQL provides about the best protection you can get against SQL injection. Stored procedures are supposed to be the safest, but this ignores the fact that too many people put dynamic SQL inside stored procedures, and this negates all the safety of parameterized calls. LINQ to SQL parameterizes everything, and while it's very friendly to stored procedures, it won't run stored procedures that utilize dynamic SQL.

Cylon Cat
+1 for "If you keep a data context around longer than a single transaction, you're doing it wrong"
Steven
And + many for comparing security
Steven
+3  A: 

One incredible advantage of LINQ to SQL over stored procedures is that you can build up your query dynamically.

Many complex queries built in stored procedures are complicated primarily because they are built to handle many possible cases. For example, a stored procedure that implements a filter with many possible parameters, some of which are optional, needs to be built to accommodate the various permutations and can get quite ugly. You can, of course, build the same query in LINQ to SQL as one large query, but the beauty of LINQ to SQL is that you don't need to.

Using flow control logic and chaining of queries, you can build up a simpler query that only uses those parameters that are active in the search, omitting the checks or alternative logic needed to address the cases where optional parameters aren't provided. Using Dynamic LINQ and/or PredicateBuilders, these queries can also be arbitrarily complex -- yet still simpler than that implemented by the stored procedure.

To accomplish the same thing with stored procedures, you'd have to write and maintain many different stored procedures each doing similar, but not the same work. Then, you'd have to have the same flow logic to choose between these procedures. This rapidly becomes untenable as the number of parameters increases.

Another advantage of LINQ, IMO, is that it lets the developer write the queries using more natural idioms (in my case, C#). While I can write SQL, it's more natural for me to write C# code. I'm better and faster at it.

tvanfosson