DotNetFirebird.org DotNetFirebird
Using Firebird SQL in .NET.
Sunday, January 09, 2005

Using Stored Procedures for Data Access

When I started using Firebird I was calling raw SQL from the code and only when there was no other choice I was calling a stored procedure. When there was a too complicated SELECT command I saved it as a view.

Now I am more and more relying on stored procedures. The Firebird ADO.NET provider makes this easier by providing a Microsoft-SQL-style stored procedure calling:

FbCommand cmd = new FbCommand("SP_INSERTDOCUMENT", new FbConnection(this.ConnectionString));
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@guid", guid);
cmd.Parameters.Add("@parent", parent);
...

It makes the .NET code much easier to read. It has also other advantages:

  • Now I have a clean database interface (I am trying to do every DB call through a SP).
  • I can easily do later optimizations inside a selecting SP. This was not possible with raw SELECTs or views.
I also had some problems with this approach:
  • When changing a table field type I need to change the variables in all related stored procedures (this already caused me some headaches - after making the VARCHAR field bigger the SP still wasn't working properly because I forgot to change it there).
  • Stored procedures can't use the type domains.

Comments:

It is observed through my performance benchmarks that store procedures take more CPU and time to execute as compared to adhoc SQL queries. I am using simple Create, read, update and delete queries and stored procedures. I am using embedded firebird database in my application and .net firebird wrapper to make such calls. Is this behavior expected or is it something missing in my approach.
I believe in other database systems the behavior you observed is expected. I've read several other places that SQL optimization (done internally by the database) usually outperforms handwritten code.
Blog comments are closed.



Previous

Archives