Calling a stored procedure using the DAAB

by Matthew Noonan 8. February 2007 23:50

It seems to me there are more than a few on the Enterprise Library forums (http://www.codeplex.com/entlib/Thread/List.aspx) about how to call a stored procedure using the Data Access Application Block (DAAB). And every once in a while the question of how to get a return value from a stored procedure surfaces.

Since it's in my nature to be a problem solver, I have pushed out a couple of templates to the MyGeneration template library that will generate the correct DAAB code for you, one for VB and one for C#. The templates handle the following situations:

  • Load the business entity -or- return a DataSet
  • Execute only
  • Return a DataReader
  • Return a RETURN_VALUE

The templates do not gaurantee perfect code for every scenario, but at least provide you with a great starting point so you don't have to code the whole thing from scratch. It will automatically detect parameters and their input/output status, a return value if you request it, and add the correct parameters to the procedure and method signature. Checking the Static (or Shared in VB) option will generate proper DAAB code, unchecking it will generate the correct calls for an EO business entity.

It's a great timesaver when you are reverse-engineering an application with existing stored procedures that you have to call (I have been using it to add EasyObjects to the TimeTracker project from www.asp.net). I also use it to generate quick code examples when people ask "How do I call this stored procedure?" on the EntLib forum. People are sometimes shocked at the answer, either they missed something simple in their code, or they can't believe how easy it is.

To me, this is yet another example of why you should be using code generation as a part of your development methodology. It gives you and your development staff (no matter how large) a consistent and repeatable process for creating data access code, while at the same time increasing productivity and reducing errors. And hey, as long as you're checking out code generation, maybe give EasyObjects a spin, too?  ;-)

Next on my list is a template for usage with the ObjectDataSource in ASP.NET.

Invoke a stored procedure - C#
http://www.mygenerationsoftware.com/TemplateLibrary/Template/?id=523a2083-1ca5-4dc9-a0c8-769b452c71e0

Invoke a stored procedure - VB.NET
http://www.mygenerationsoftware.com/TemplateLibrary/Template/?id=13696b88-9b79-402b-87c7-f87f42a7cfbb

Comments

4/14/2007 4:00:00 AM #

For use this template i have to do the generation for each store procedure?

Victor United States

4/15/2007 4:00:00 AM #

Yes, but I believe you can select multiple stored procedures in the list.

Matthew Noonan United States

5/3/2007 4:00:00 AM #

Ouch, that's a tough one. There used to be, back in the EntLib 1.1 days, a user-supplied patch for the EntLib that allowed you to specify the Oracle cursor name, or even add multiple cursors to the same call (which is also not supported in the EntLib).

Unfortunately, I have no idea of the status of the patch, if it was ported to EntLib 2.0, etc. You can find the original patch on the GotDotNet site, so maybe you can adapt the code for your own purposes.

Good luck!

Matthew Noonan United States

5/3/2007 4:00:00 AM #

I'm trying to call an Oracle stored procedure that returns a cursor. I didn't know how to bind the parameter to cursor type, as there was no option to specify cursor in DbType. Then, I found from the codeproject.com, that there's no need to add parameter for cursor and the DAAB implicitly reads data from cursors, but the cursor name has to be cur_OUT and specified as IN OUT. For eg.
TYPE my_cursor IS REF CURSOR; -- declaration
PROCEDURE GetData(ID varchar2(15), curOUT IN OUT mycursor)....

This works ok if I'm going to write a new stored procedure. But in our system, we already have a bunch of stored procedures that has different names for the cursors and we cannot change this, as many other applications call this stored procedure and it's impossible for us change all these applications. (These app's doesn't use Ent Lib). Is there any workaround for this?

Thanks,
Hari.

Hari United States

Comments are closed