Click here to Skip to main content
16,022,752 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This may be a stupid question.
But please answer me.

Which is the best way to use and manipulate SQL queries in code..??

i have tried 3 methods and still don't know which one is a more professional approach.

1. By writing select, insert, update, delete queries when ever needed inside the C# code.

2. By creating a DataSet Item in the project and writing corresponding Select, Update, Insert, Delete Queries in the DataSet DataTableAdapter.

3. By Using Stored Procedure.
Posted

Well, using Stored Procedure is always recommended. Stored procedures are gold when you have several applications that depend on the same database. It let's you define and maintain query logic once, rather than several places.
 
Share this answer
 
Comments
Prasad_Kulkarni 21-Aug-12 8:52am    
MY 5!
Manas Bhardwaj 21-Aug-12 9:06am    
thx!
ridoy 21-Aug-12 9:00am    
+5
Manas Bhardwaj 21-Aug-12 9:06am    
thx!
__TR__ 21-Aug-12 11:59am    
My 5!
Prefer Stored procedure!!!
Here are some advantages for Stored procedures:

In terms of maintainability:

One could use parametrized queries for simple/trivial operations, like retrieving data, or inserting data in table. Stored procedures are easier to maintain than inline queries if you have complicated (processing) code. Who would like to maintain program source code mixed with complicated SQL code in the same file. Also, almost all of the parametrized queries that are complex, can be found splitted in multiple string concats. Believe it or not, I've seen a SQL query concatenated in over 60 lines length.

In terms of speed:

First, you could think about the time it takes to compile the query and, also, the time it takes to create an execution plan (Both of them are sometimes longer than the execution time of the query). The stored procedures are compiled only once, unless specified, and the execution plan is stored, so a restart of the server wouldn't loose it. Also, for queries, the execution plans aren't created right away, but after a few calls of the same query (depending on the databse engine).

In terms of connection:

If you have a slow connection to the sql server, and large queries, you would probably be better off using stored procedures, as they generate less network traffic, and usually less roundtrips between the server and the client.

Also, there are also lot's of other advantages/disadvantages of using one over another, but the bid would be for stored procedures in most of the cases.

Refer Here

1.Stored-Procedures-Optimization-Tips[^]
2.stored-procedure-optimization-tips[^]
 
Share this answer
 
v3
Comments
Prasad_Kulkarni 21-Aug-12 8:53am    
MY 5!
ridoy 21-Aug-12 9:01am    
good answer..+5
[no name] 21-Aug-12 9:01am    
thanq
Manas Bhardwaj 21-Aug-12 9:06am    
+5
__TR__ 21-Aug-12 12:00pm    
My 5!
Adding my two cents here. I also think that using stored procedures is advicable in many situations as others have already stated.

However, if your queries are more dynamic in nature then it may not make sense to pass the elements of a composed query (or even the whole statement) to a stored procedure. This would introduce a problem where the dynamic logic is separated to two different layers, the application and the database. In this kind of situation I feel that creating the queries in your application and executing them against the database is more preferred way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900