I'm using a certain method body to call stored procedures, with the following sample code:

     public void StoredProcedureThatIsBeingcalled(int variable_1, int variable_2, out DataSet ds)
 {
     using (SqlConnection con = new SqlConnection(DatabaseConnectionString))
     {
         ds = new DataSet("DsToGoOut");
         using (SqlCommand cmd = new SqlCommand("StoredProcedureThatIsBeingcalled", DbConn.objConn))
         {
             cmd.CommandType = CommandType.StoredProcedure;


             cmd.Parameters.Add(new SqlParameter("@variable_1", variable_1));
             cmd.Parameters.Add(new SqlParameter("@variable_2", variable_2));
             try
             {
                 con.Open();
                 SqlDataAdapter objDataAdapter = new SqlDataAdapter();
                 objDataAdapter.SelectCommand = cmd;

                 objDataAdapter.Fill(ds);

                 con.Close();
             }
             catch (Exception ex)
             {

                 //sql_log_err
             }

         }
     }
 }

What bugs me I have most of the above code repeating time and time again in my cs file for every different procedure I call.

Obviously I can clear it up and have the one Function being called with the procedure name as a variable, but how do I feed it different number of Parameters (with different Data Types - int,string bool - never anything else) for the different procedures I use ?

I can have few different functions with different number of parameters(0-10), but I feel there is a better way of doing this ?

Related posts

Recent Viewed