Llamar a un sp(procedimiento almacenado) c#
Aqui un ejemplo que yo trabajo. Suponiendo que dbhelper es una clase que contiene todos los objetos necesarios para manipular conexiones a SQL, el método q invoca al SP seria algo asi:
public Boleto ConsultaViajeBoleto(RequisicionDetalle requisicion)
{
Boleto boleto = new Boleto();
dbHelper.CommandType = CommandType.StoredProcedure;
dbHelper.ActiveConnection = true;
try
{
dbHelper.AddParams(dbHelper.CreateParam("@Id_Requisicion", requisicion.Id_Requisicion),
dbHelper.CreateParam("@Version", requisicion.Version),
dbHelper.CreateParam("@Id_Boleto", requisicion.Id_ElementoRequisicion));
IDataReader reader = dbHelper.ExecuteDataReader("sp_Req_ConsultaViajeBoleto");
if (reader.Read())
{
boleto.Id_Requisicion = reader.GetInt32(0);
boleto.Version = reader.GetInt32(1);
boleto.Id_Boleto = reader.GetInt32(2);
boleto.Id_Recurso = reader.GetInt32(3);
boleto.Id_TipoTransporte = (TipoTransporte)reader.GetInt32(4);
boleto.Id_TipoViaje = (TipoViaje)reader.GetInt32(5);
boleto.Id_Clase = reader.GetInt32(6);
boleto.Fec_HoraSalida = reader.GetDateTime(7);
boleto.Fec_HoraRegreso = reader.GetDateTime(8);
boleto.LugarOrigen = reader.GetString(9);
boleto.LugarDestino = reader.GetString(10);
boleto.Clave_Conformacion = reader.GetString(11);
boleto.Id_Moneda = reader.GetInt32(12);
boleto.Costo = reader.GetDecimal(13);
boleto.CostoBase = reader.GetDecimal(14);
boleto.Id_MonedaEstimado = reader.GetInt32(15);
boleto.CostoEstimado = reader.GetDecimal(16);
boleto.CostoBaseEstimado = reader.GetDecimal(17);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
dbHelper.ActiveConnection = false;
}
return boleto;
}