Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
How to create a webapi that returns sqldatareader data in c#?

I am using asp.net core. I need to display the data in the select dropdown in react from web api. WebApi GET method that returns SQL DataReader. Reader returns one row with prodid, prodname, and proddescr columns. Please help what the best way to write a get web api that uses SQL DataReader for filling the select dropdown in react.

[HttpGet("{ProductID}")]
public JsonResult GetProductInfo(int ProductID)
{
    var response = GetProductInfo(ProductID);

    return new JsonResult(response);
}

public string GetProductInfo(int Product_ID)
{
    SqlConnection objConnect = new SqlConnection(_connectionString);

    SqlCommand objCommand = new SqlCommand("usp_GetProdInfo", objConnect);

    objCommand.CommandType = CommandType.StoredProcedure;

    objCommand.Parameters.Add(new SqlParameter("@Product_ID", SqlDbType.Int, 4));

    objCommand.Parameters["@Product_ID"].Value = intProduct_ID;

    string json = string.Empty;

    List<object> objects = new List<object>();     

    objConnect.Open();

    SqlDataReader reader = objCommand.ExecuteReader();      
    while (reader.Read())
    {
        IDictionary<string, object> record = new Dictionary<string, object>();
        
        for (int i = 0; i < reader.FieldCount; i++)
        {
            record.Add(reader.GetName(i), reader[i]);
        }

        objects.Add(record);
    }

    json =JsonConvert.SerializeObject(objects);

    reader.Close();

    objConnect.Close();

    return json;
}
about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

you don't need to serialize data, Net can do it for you. And it is a very bad programming style to use objects instead of real classes. So return List instead of List

public ActionResult<List<Product>> GetProductInfo(int ProductID)
       {

         var response = GetProductInfoList(ProductID);

         if (response != null) return Ok(result)
         else retun BadRequest();

     }

     public List<Product> GetProductInfoList(int Product_ID)
     {
                List<Product> products = new List<Product>();    
                ...fix the code to get typed List<Product> from reader
                return products;
    }
about 4 years ago · Santiago Gelvez Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda