简体   繁体   English

使用asp.net核心在Mysql数据库上查询

[英]Query on Mysql database with asp.net core

I need to query the database. 我需要查询数据库。 This query is working correctly, 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 我不知道如何在此查询中添加“做”或“在”时

[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();
    }
}

You're only getting one row because you're calling reader.Read() . 您只得到一行,因为您正在调用reader.Read() Each time you call Read(), the reader advances to the next row and returns true; 每次调用Read()时,阅读器都会前进到下一行并返回true;否则,返回true。 or, when the reader advances past the last row, it returns false. 或者,当阅读器前进到最后一行时,它返回false。

Assume that you have a view model: 假设您有一个视图模型:

public class MensagensViewModel
{
    public string Codmsgx { get; set; }
    public string Phone { get; set; }
    public string Message { get; set; }
}

To get the List<MensagensViewModel> data from tbl_mensagens table, you need to manually save the accessed data into mensagens shown as below code: 要从tbl_mensagens表中获取List<MensagensViewModel>数据,您需要手动将访问的数据保存到mensagens中,如下代码所示:

try
    {
        cmdMySQL.Connection = conMySQL;             
        cmdMySQL.CommandText = "SELECT * FROM tbl_mensagens WHERE enviado = '" + 0 + "' AND email_usuario = '" + email + "' ";
        conMySQL.Open();
        reader = cmdMySQL.ExecuteReader();

        List<MensagensViewModel> mensagens = new List<MensagensViewModel>();//To save all retrieved data
        while (reader.Read())
        {
            var mensagen = new MensagensViewModel()
                {
                    Codmsgx = reader[0].ToString(),
                    Phone = reader[4].ToString(),
                    Message = reader[5].ToString()
                };

            mensagens.Add(mensagen);
            //other logic                 

        }
        reader.Close();
        return View();
    }
    catch (Exception ex) { Debug.WriteLine(ex); return View(); }
    finally
    {
        conMySQL.Close();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM