logo
  • Jobs
  • About Me
  • Contact
  • Home
« Official VS.NET 2005 Launch Date
Custom Configuration Sections in Beta 2 »

No more stored procedures…YAY!

Posted June 9th, 2005 by Matt Berther

During a recent upgrade to an application Ive been working, all but one of the stored procedures was removed.

What?!?! Indeed, data access can happen without stored procedures, and as referenced in some other weblog posts this may be very advantageous. Go ahead and read this post (stored procedures are bad, m’kay?) now… I’ll wait.

In the case of this particular application, there was always an issue about keeping three different projects in sync (the DAL, the database project, and the installer project).

Any time that I needed to make a change to retrieve additional data from a table in the application’s database, I had to make changes to three projects. First off, I had to update the database project to modify the stored procedure. Once that modification was done, I was able to modify the Data Access Layer to use the new field. Finally, when everything was done, I needed to remember to update the installer project so that the scripts that create and update the database have the correct stored procedures.

Now, as you can see this can cause quite a bit of headache and an update process that can be very prone to error. The way that I found to get around this is to move all but the most complex stored procedures (the ones that were actually performing real work) into the DAL directly.

What this means is that instead of having a stored procedure that does ‘SELECT * FROM Users’, I send that command directly to the SQL database using the SqlCommand object in the .NET framework. The technique is identical to what is used to execute the stored procedures.

One thing that I hear quite a bit is that you open yourself up to Sql injection attacks when using dynamic sql, instead of stored procedures, because you can not use parameters. This is completely wrong. Let me show an small working example:

SqlCommand cmd = new SqlCommand("select * from users where UserName = @UserName");
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = someUserNameVariable;

You see here that you have exactly the same parameter semantics as you do using stored procedures. These semantics allow you to pass values into the parameter object where they will be scrubbed to make sure that no sql injection attacks can take place.

Using dynamic sql in the data access layer, if I want to update the columns that are pulled back, I no longer have to modify a stored procedure in the database project. Also, I dont have to modify the installer project, because all of my sql is right where it belongs… in the data access layer.

3 Comments

This entry was posted on Thursday, June 9th, 2005 at 1:37 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Tim Haines
June 30th, 2005

You’re not using an O/R Mapper?!?!?!

Jon Choy
July 11th, 2005

As my team lead keeps pointing out when I mention this concept:

You must either be using AppRoles for your security, or you must be willing to give away much higher permissions to the tables directly to the users of your application for this technique to work.

Procs as security administration tools to remove the motivation for a middle tier for security chokepoint reasons seem to +1 stored procedures from where I’m sitting.

Coolio
August 13th, 2006

Delphi has been implmenting DataSets for a while thats the way to go for database providers

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010