Adding and Reading Out Parameters with Enterprise Library

I had writing a code that was doing the job well but, after looking around the Enterprise Library community i found an easier way to do that.

Instead of adding the parameter like this:

object[] paramArray = new object[4];
        paramArray[0] = orgCode;
        paramArray[1] = ProgramCD;
        paramArray[2] = ElementCD;
        paramArray[3] = ComponentCD;

        Database db = DatabaseFactory.CreateDatabase();
        DbCommand dbCmd = db.GetStoredProcCommand(@"Test_ProjectExists", paramArray);
        db.AddOutParameter(dbCmd, @"ProjectID", DbType.Int32, 100);
        db.ExecuteNonQuery(dbCmd);

        ProjectID = Convert.ToInt32((db.GetParameterValue(dbCmd, @"ProjectID")));

You can simply write it like this:

object[] paramArray = new object[5];
int projId = 0;
paramArray[0] = orgCode;
paramArray[1] = ProgramCD;
paramArray[2] = ElementCD;
paramArray[3] = ComponentCD;
paramArray[4] = projId;

Database db = DatabaseFactory.CreateDatabase();
DbCommand dbCmd = db.GetStoredProcCommand(@"Test_ProjectExists", paramArray);
//db.AddOutParameter(dbCmd, @"ProjectID", DbType.Int32, 100);
db.ExecuteNonQuery(dbCmd);

ProjectID = Convert.ToInt32((db.GetParameterValue(dbCmd, @"ProjectID")));
Advertisements

2 thoughts on “Adding and Reading Out Parameters with Enterprise Library

  1. Although this post is a couple of years old, I wanted to send you a quick note that it came in handy for me. Thanks for taking the time to post it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s