如何在字符串中存储 MySQL 查询结果

本文关键字:MySQL 查询 结果 存储 字符串 | 更新日期: 2023-09-27 17:56:12

是否可以在字符串变量中获取MySQL查询结果。考虑以下情况,并让我知道是否可以将MySQL查询结果存储到字符串中。我的数据库如下

Article ID       Article Name    Article Quantity
1                Article1         15 Units
2                Article2         20 Units
3                Article3         18 Units

此外,我正在尝试使用 for 循环逐个访问行(不知道我的做法是对还是错)。但是我现在主要关心的是将MySQL查询结果放入字符串中。

private void send_serial_data(string port_name)
{
    MySqlConnection MyConn2 = new MySqlConnection(MyConnection2);
    SerialPort sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One);
    MyConn2.Open();
    sp.Open();
    string variable; //In which I want to store Query
    int j = 0; //To keep track of null rows
    {
        for (int i = 1; i < 20; i++) //I is defined to select new row each time
        {
            String Query = "";
            Query = "SELECT * FROM warehouse_data.display where Article_ID= i";
            MySqlCommand cmd = new MySqlCommand(Query, MyConn2);
            // variable = (string)command.ExecuteScalar();    Tried this to save query but doesn't work.
            int a = cmd.ExecuteNonQuery();
            if (a == 0)
            {
                j++;
                if (j >= 10)
                {
                    MessageBox.Show("Data Transmitted Successfully");
                    break;
                }
            }
            else
            {
                sp.Write(variable); //variable in which I want to store MySQL query
                System.Threading.Thread.Sleep(5000);
            }
        }
    }
}

基本上,我正在尝试将MySQL数据发送到串行端口(一次一行)。关于如何将查询保存到定义的字符串"变量"以及如何使用 for 循环来实现我的目标或任何其他有效逐行访问行的方法的任何建议?您的帮助将不胜感激。提前致谢

如何在字符串中存储 MySQL 查询结果

你的代码中有很多问题,首先,ExecuteNonQuery不返回行。您需要使用ExecuteReader,读取一行,将每个字段转换为字符串,发送然后重复。但在此之前,您需要修复该查询,因为 i 变量不是用于过滤查询的值。但实际上没有必要按 ID 调用 20 次相同的查询过滤,只需使用适当的 WHERE 条件即可

private void send_serial_data(string port_name)
{
    using (MySqlConnection MyConn2 = new MySqlConnection(MyConnection2))
    using (SerialPort sp = new SerialPort(port_name, 9600, Parity.None, 8, StopBits.One))
    {
        MyConn2.Open();
        sp.Open();
        // A query that returns all records with an ID lower than 20
        String Query = @"SELECT * FROM warehouse_data.display where Article_ID < 20";
        using (MySqlCommand cmd = new MySqlCommand(Query, MyConn2))
        using (MySqlDataReader reader = cmd.ExecuteReader())
        {
            // Read one record 
            while (reader.Read())
            {
                StringBuilder sb = new StringBuilder();
                // Accumulate the fields values in the stringbuilder
                // separating each one with a comma
                for (int x = 0; x < reader.FieldCount; x++)
                {
                    if (reader.IsDBNull(x))
                        sb.Append(",")
                    else
                        sb.Append(reader[x].ToString() + ",");
                }
                // trim away the last comma
                sb.Length--;
                // And probably you need something to separate a record
                // from the following one. For example a newline
                sb.Append(Environment.NewLine);
                // send it along the wire
                sp.Write(sb.ToString());
            }
        }
    }
}

还要注意另外两件事,我用逗号分隔每个字段,否则你写入串口的接收方将无法重建从表中读取的字段,每个一次性对象也应该括在适当的 using 语句中