In our project's current state, we have at least one method used to call each stored procedure that we have created on the database server. Since these methods are pretty lengthy, involving creating each SqlParameter and then passing them to the database connection, I was considering creating a method that would generalize the process for our needs.
This is what I've come up with so far:
public static void UpdateTableTest(string procedureName, params object[] paramList)
{
if (sessionID == -1)
GetActiveSession();
SqlConnection scn = new SqlConnection(GetConnectionString());
try
{
scn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = scn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = procedureName;
SqlCommandBuilder.DeriveParameters(cmd);
for (int i = 0; i < paramList.Length; i++)
{
cmd.Parameters[i+1].Value = paramList[i];
}
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw ex;
}
finally
{
scn.Close();
}
}
Aside from problems that I will run in to for different query types, and for query types that I will need to return some data from, is there anything inherently wrong with this method of executing stored procedures? Should I stick with an individual method for each stored procedure?
SqlConnectionandSqlCommandintousingblocks.