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