I have this method to execute stored procedures
public void EjecutarSP(string sp)
{
try
{
SqlCommand cmd = new SqlCommand(sp, ConexionBD.con);
cmd.CommandType = CommandType.StoredProcedure;
ConexionBD.Conectar();
cmd.ExecuteNonQuery();
ConexionBD.Desconectar();
}
catch (Exception ex)
{
throw new Exception(" Error al ejecutar procedimiento almacenado ", ex);
}
}
This method serves me generically when the stored procedures do not receive or return any value, I wanted to know if there is any way to make them generic for when the stored procedure receives and returns parameters, I think that for the return part I can use ExecuteReader that returns a table and then work on it in the application, but for the part of sending parameters I don't know if something generic can be done since not all stored procedures receive the same number of parameters
This method will help you execute Stored Procedures using generic data types:
Now, to be able to make a call to the method, it would be as follows:
Sending to method
You are sending all the desired parameters so that you can finally receive them automatically in the type of the class that you want in a generic way.
If you want you could use the Sql Helper class that already has the overloaded methods you want, or another is to adapt it to the needs you have.
Check out this tutorial , which uses such a class.