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

时间:2016-07-11 07:06:30

标签: c# mysql serial-port

是否可以在字符串变量中获取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循环来实现我的目标或任何其他方式逐个有效地访问行的任何建议?非常感谢您的帮助。 在此先感谢

1 个答案:

答案 0 :(得分:2)

您的代码中存在很多问题,启动时,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());
            }
        }
    }
}

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