473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Inline SQL or stored procs for my C# windows app?

I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt

Apr 23 '07 #1
15 2246
The RPC (stored proc) path through SQL Server is a lot faster than the
language ("text SQL") path in most cases.

In addition, there are a number of very sophisticated things you can do
inside the database - transactions, UDFs, triggers, multistatement procs,
Table variables, cursors and so on that are difficult or impossible to do
with text sql even if it is parameterized.
My 2 cents.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Burt" wrote:
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt

Apr 23 '07 #2
Burt,

Well, it kind of is one of those "no right answer" questions. It always
depends on context.

If you have a routine that is going to be called over and over again,
then there really is no reason to not have it in a stored procedure.

Yes, you will have to migrate these if you move to another database, but
honestly, how hard is it to move a stored procedure from one database to
another? If you have dynamic sql, then you are going to run into the same
problem, you will have to change the dynamic sql generator (or strings for
the sql) to make sure you are not using any database-specific features.

Just because you are using dynamic sql doesn't mean that you are not
using database-specific features in the dynamic sql.

Honestly, I would use something like the Data Access Application Block
in the Enterprise Framework that is offered by Microsoft. It will help
mitigate using stored procedures and changing from one database to another
(you won't have to worry about changing how you call stored procedures from
managed code, for example), and you can use the object model exposed by it
to generate dynamic sql that will work across any database that has a
provider for the DAAB, assuming you don't use any database-specific features
in your SQL.

Personally, I think that dynamic sql has a place, but for established
logic which will be called repeatedly, stored procedures are the way to go.
For me, I would use dynamic sql for things like persisting an object model
to the database, but not performing multi-statement logic against the
database.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Burt" <bu*******@yahoo.comwrote in message
news:11**********************@q75g2000hsh.googlegr oups.com...
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt

Apr 23 '07 #3
Burt wrote:
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt
Due to SQL injection becoming a large problem to some databases, i would
suggest sticking with
stored procs. As all you have to do is run the stored procs you can disable
other query methods.
--
Regards JJ (UWA)

Apr 23 '07 #4
JJ,

Stored procedures don't necessarily prevent SQL injection attacks. It
is using the object model for passing parameters to a stored procedure which
prevents SQL injection attacks.

You can use that same object model to create parameterized dynamic sql
which is immune to injection attacks.

Conversely, you can actually execute the stored procedure by stringing
together a statement with the parameters (without using the object model)
and be vulnerable to an injection attack.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"j1mb0jay" <ja**@aber.ac.ukwrote in message
news:11***************@leri.aber.ac.uk...
Burt wrote:
>I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt

Due to SQL injection becoming a large problem to some databases, i would
suggest sticking with
stored procs. As all you have to do is run the stored procs you can
disable other query methods.
--
Regards JJ (UWA)

Apr 23 '07 #5
On Apr 23, 1:54 pm, "j1mb0jay" <j...@aber.ac.ukwrote:
Due to SQL injection becoming a large problem to some databases, i would
suggest sticking with
stored procs. As all you have to do is run the stored procs you can disable
other query methods.
Stored procedures offer no protection against sql injection attacks;
they are just as prone as dynamically built sql. The problem arises
from how you build the statement; if you are concatinating strings to
insert user supplied values, you are vunerable to sql injection
attacks. If you are instead leaving placeholders in the sql string,
and using something which implements IDataParameter to add the user
supplied values, you are immune from attacks.

Apr 23 '07 #6
develop it with inline and then move it to sprocs

for example.. using dreamweaver this strategy is actually quite nice

of course.. dreamweaver includes wizards for building webpages; MS
doesn't give a crap about web dev.. I mean.. Visual Interdev?
FrontPage? SharePoint?

Screw M$

On Apr 23, 10:08 am, Burt <burt_5...@yahoo.comwrote:
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt

Apr 23 '07 #7
I agree with Peter. Another consideration is good app design/layering. Since
the format of the query is dependent upon the structure of the database, a
Stored Procedure can behave like a "function" that is called from the
database. If designed well, you can make changes to the underlying structure
of the database, leave the SP parameters the same, and the change will be
transparent to the application, thereby preventing you from having to
rebuild the application.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Peter Bromberg [C# MVP]" <pb*******@yahoo.yabbadabbadoo.comwrote in
message news:E8**********************************@microsof t.com...
The RPC (stored proc) path through SQL Server is a lot faster than the
language ("text SQL") path in most cases.

In addition, there are a number of very sophisticated things you can do
inside the database - transactions, UDFs, triggers, multistatement procs,
Table variables, cursors and so on that are difficult or impossible to do
with text sql even if it is parameterized.
My 2 cents.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Burt" wrote:
>I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt


Apr 23 '07 #8
Thanks all. Nicholas you wrote:
Honestly, I would use something like the Data Access Application Block
in the Enterprise Framework that is offered by Microsoft. It will help
mitigate using stored procedures and changing from one database to another
(you won't have to worry about changing how you call stored procedures from
managed code, for example), and you can use the object model exposed by it
to generate dynamic sql that will work across any database that has a
provider for the DAAB, assuming you don't use any database-specific features
in your SQL.
My next question was actually about the DAAB. At my current company
we're using it. At my last job, we just had a shared class called
"Data Access" which all programmers used. It was one level, and just
had some basic "GetDataReader", "GetDataSet", etc methods. The DAAB
sure does seem like a lot of overhead. Occasionally it will throw an
error which I'll have to Step through through a few Microsoft code
files to figure out. But I see there are benefits, like the one
above.

Burt

Apr 23 '07 #9
On Apr 23, 1:08 pm, Burt <burt_5...@yahoo.comwrote:
I'm a stored proc guy, but a lot of people at my company use inline
sql in their apps, sometimes putting the sql in a text file, sometimes
hardcoding it. They don't see much benefit from procs, and say anyway
they're are db specific- what if we change from SQL Server to Oracle
one day?

What say you experts? Is this one of those "no right answer"
questions?

Thanks,

Burt
I'll tell you what's been working for me for years: dynamically
generating SQL from my object model. Which implies first of all that I
do have an object model. I use two functions (static to boot because I
don't want to create objects more than necessary) :

- Read (which generates SELECT)
- Write (which generates either INSERT, UPDATE or DELETE).

I get 90% of my app working this way; the other 10% is custom sql or
stored procedures that I write after a lot of performance profiling.
If performance is ok, no sprocs - and complete database independence.
Also, I found it pretty hard to inject SQL because all of it is
generated. And I get most of the benefits of stored procedures since
of course I use parameterized prepared statements. I only update
columns that I actually changed in the objects, which increases
concurrency (two users are able to change different columns of the
same row at the same time). I don't need to pass any parameters to
stored procedures. My systems have never been so clean and
uncluttered. Life is good.

I would stay away from the DAAB but the microsoft buffs may disagree
(reasonable people really disagree over this very topic). Also, if you
sold your soul to a database vendor, well, maybe better stick with
it.

Otavio

Apr 23 '07 #10
1. The inline SQL statement, hard coded, makes maintenance in the log
run a little tedious.

2. Stored procs have a distinct advantage because they are executed in
the DB and hence, is out of process from the currently executing code.
Plus, it is easily maintainable as you would not need to touch the code
but change it in the DB.

3. No matter what the DB if you use stored procs all you need to do is
to retype the stored proc in the other database. The call to the stored
proc remains the same with only the connection string to be changed.
Even this can be separated from code as you could put it in the config
file (suitable encrypted in 2.0)!

with regards,
J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Apr 24 '07 #11
On Apr 24, 1:03 am, Ravichandran J.V. <jvravichand...@yahoo.com>
wrote:
1. The inline SQL statement, hard coded, makes maintenance in the log
run a little tedious.
It depends on how you build things. Calling a proc can also make
maintenance more difficult as well.
2. Stored procs have a distinct advantage because they are executed in
the DB and hence, is out of process from the currently executing code.
All Sql is executed by the database engine, dynamic or sp call. This
is just plain wrong.
Plus, it is easily maintainable as you would not need to touch the code
but change it in the DB.
Again, not necessarly true either. Procedures can change what they
return, or what arguments they take, and you'll be back changing the
code.
3. No matter what the DB if you use stored procs all you need to do is
to retype the stored proc in the other database. The call to the stored
proc remains the same with only the connection string to be changed.
Even this can be separated from code as you could put it in the config
file (suitable encrypted in 2.0)!
What if the next new db doesn't support stored procedures at all? And
again, this isn't always true. Sql Server uses @varName for parameter
names. Other Dbs simply use ? for place holders, and you must add
parameters in the proper order.

Apr 24 '07 #12
Ravichandran J.V. <jv************@yahoo.comwrote:
1. The inline SQL statement, hard coded, makes maintenance in the log
run a little tedious.
Otavio said that 90% of his SQL is auto-generated, not hard-coded. In
my experiences with Hibernate in Java, there was even less hard-coded
SQL than that.
2. Stored procs have a distinct advantage because they are executed in
the DB and hence, is out of process from the currently executing code.
As Andy pointed out, the SQL itself is always executed in the DB.
Where's the advantage?
Plus, it is easily maintainable as you would not need to touch the code
but change it in the DB.
Why do you believe it's easier to change DB code than server-side code?
(I'm not talking about clients talking directly to the DB, but
something like a web service.)
3. No matter what the DB if you use stored procs all you need to do is
to retype the stored proc in the other database. The call to the stored
proc remains the same with only the connection string to be changed.
Even this can be separated from code as you could put it in the config
file (suitable encrypted in 2.0)!
Um, you're kidding, right? Stored procs vary *massively* between
different databases. Ever tried putting complex SQL stored procs on an
Oracle database?

If you're talking about using the same kind of database, just pointing
at a different box, I don't see that there's any benefit in using
stored procs over dynamically generated SQL that's relevant to what you
put in the above paragraph. Whether you generate the SQL or call a
stored proc, you've still just got to change the connection string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 24 '07 #13
Peter Bromberg [C# MVP] wrote:
The RPC (stored proc) path through SQL Server is a lot faster than the
language ("text SQL") path in most cases.
Yep
In addition, there are a number of very sophisticated things you can do
inside the database - transactions, UDFs, triggers, multistatement procs,
Table variables, cursors and so on that are difficult or impossible to do
with text sql even if it is parameterized.
Neither transactions, UDFs or triggers is tied to SP's.

Arne
May 6 '07 #14
Nicholas Paldino [.NET/C# MVP] wrote:
If you have a routine that is going to be called over and over again,
then there really is no reason to not have it in a stored procedure.
That depends a lot on portability requirements, application design etc..
Yes, you will have to migrate these if you move to another database, but
honestly, how hard is it to move a stored procedure from one database to
another?
It can be very hard. Some databases does not have SP's. Other databases
use SP's a lot different from the Microsoft/Sybase way.
If you have dynamic sql, then you are going to run into the same
problem, you will have to change the dynamic sql generator (or strings for
the sql) to make sure you are not using any database-specific features.
Not necesarrily.
Just because you are using dynamic sql doesn't mean that you are not
using database-specific features in the dynamic sql.
It is by no way give, but it is possible to avoid those.
Honestly, I would use something like the Data Access Application Block
in the Enterprise Framework that is offered by Microsoft. It will help
mitigate using stored procedures and changing from one database to another
(you won't have to worry about changing how you call stored procedures from
managed code, for example), and you can use the object model exposed by it
to generate dynamic sql that will work across any database that has a
provider for the DAAB, assuming you don't use any database-specific features
in your SQL.
There are other ways of implementing data access. But a lot of them are
worse than DAAB, so if in doubt DAAB is a good starting point.

Arne

May 6 '07 #15
Security is simpler to implement if your db doesn't allow any dynamic SQL.
Then all db access is via SP, you can assign permissions to your desired SQL
roles, and any access not specifically allowed can be prevented.
Paul Shapiro

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
Peter Bromberg [C# MVP] wrote:
>The RPC (stored proc) path through SQL Server is a lot faster than the
language ("text SQL") path in most cases.

Yep
>In addition, there are a number of very sophisticated things you can do
inside the database - transactions, UDFs, triggers, multistatement procs,
Table variables, cursors and so on that are difficult or impossible to do
with text sql even if it is parameterized.

Neither transactions, UDFs or triggers is tied to SP's.

Arne

May 6 '07 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LineVoltageHalogen | last post by:
Greetings All, currentley there is a heated discussion in my place of work over which method is better/more efficient for simple selects. Background: 1.) Simple Application that uses sql server...
6
by: Dave | last post by:
1) I know that we can define an external proc to be Fenced or NotFenced on "CREATE PROCEDURE" command. I don't see the FENCED / NOT FENCED option on "Create Procedure" for SQL stored procs. Is...
5
by: Rhino | last post by:
This question relates to DB2 Version 6 on OS/390. Can a (COBOL) stored procedure on this platform do file I/O, i.e. write to a sequential file? I am trying to debug a stored procedure. As far...
7
by: trint | last post by:
My boss wants me to use stored procedures, but I thought somehow that using c# that ADO.Net was better. Any help is appreciated. Thanks, Trint
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
4
by: Wendy Elizabeth | last post by:
I have the following questions about VB.NET interfacing with sql server 2000: 1. I have heard that VB.NET can run with inline SQL. Can you show me how to use inline sql to access a sql server 2000...
1
by: kentk | last post by:
Is there a difference in how SQL Server 7 and SQL 2000 processes SQL passed from a program by an ADO command object. Reason I ask is I rewrote a couple applications a couple years ago were the SQL...
4
by: PJackson | last post by:
I have been given the task of taking a 3,200 line COBOL stored procedure and duplicating the same functionality in UDB 7.2 on the Windows platform with a procedural SQL stored procedure. I have...
8
by: Frank Calahan | last post by:
I've been looking at LINQ and it seems very nice to be able to make queries in code, but I use stored procs for efficiency. If LINQ to SQL only works with SQL Server and stored procs are more...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.