I've got a data layer I'm working on that calls into a database three times with three different stored procedures. I initially created three different functions to retrieve three dimensionalities of results. The first returns a single value, the second an entire row, and the third a table. They also take in different parameters. The first takes two varchars, the second two ints, and the last three varchars. If I try to get fancy and merge it all down as shown below, am I going to have problems?
public void CallStoredProcedure(string[] astrParams, string strConnectionString, string strStoredProcedure)
{
int nParams = 0;
SqlParameter[] asqlParams;
asqlParams = SqlHelperParameterCache.GetSpParameterSet(strConnectionString, strStoredProcedure);
foreach (string strParam in astrParams)
{
asqlParams[nParams].Value = strParam;
nParams++;
}
}
Alternately, can I use an array of mixed data types without know what is in there, and can I assign different types into the same array, replacing elements?
object[] aParams;
string strName = "Joe";
long lngHeight = 180;
object[0] = strName;
object[1] = lngHeight;
CallStoredProcedure(aParams, strConnectionString, "StoredProcedure1")
long lngWeight = 3;
string strItemName = "Bookend";
object[0] = lngWeight;
object[1] = strItemName;
CallStoredProcedure(aParams, strConnectionString, "StoredProcedure2")
And then change the code inside that function to:
foreach (object oParam in astrParams)
{
asqlParams[nParams].Value = oParam;
nParams++;
}
Would I need to use ToString() in either or both of these cases? And if so, does that essentially turn them into the same thing? Like I said, right now I've got three functions that take in all the parameters correctly typed, but I'm trying to get rid of duplicate code if possible.
Parameter.AddWithValue