locked
Query on Mysql database with asp.net core RRS feed

  • Question

  • User-1261553524 posted

    I need to query the database.

    This query is working correctly and return 1 result, but I need to loop to read all data in the table.

    I don't know how to put "do" or "while" in this query

    This is the code:

    [HttpPost]
    public IActionResult SendMessage(string send_to, string message, string phone)
    {
        var email = HttpContext.Session.GetString("email");
        MySqlDataReader reader;           
        try
        {
            cmdMySQL.Connection = conMySQL;
            conMySQL.Open();
            cmdMySQL.CommandText = "SELECT * FROM tbl_mensagens WHERE enviado = '" + 0 + "' AND email_usuario = '" + email + "' ";
            reader = cmdMySQL.ExecuteReader();       
            while (reader.Read())
            {
                var codmsgx ="";
                phone = "";
                message = "";        
                codmsgx = reader[0].ToString();
                phone = reader[4].ToString();
                message = reader[5].ToString();
                EnviarMensagem(send_to, message, phone, email, codmsgx);
                AtualizarBd(send_to, message, phone, email, codmsgx);
            }
            reader.Close();           
            return View();
        }
        catch (Exception ex) { Debug.WriteLine(ex); return View(); }
        finally
        {
            conMySQL.Close();
        }
    }

    Tuesday, July 30, 2019 10:00 PM

All replies