【问题标题】:C# Loop through DataGridView - MySqlDataReader stops after 2 Rows?C# 循环通过 DataGridView - MySqlDataReader 在 2 行后停止?
【发布时间】:2014-11-06 19:59:12
【问题描述】:

我很难使用 C# ATM...

关于这段讨厌的代码:

foreach (DataGridViewRow r in dgv_selectedOrders.Rows)
            {
                MessageBox.Show(r.Cells[0].Value.ToString());
                order_id = r.Cells[0].Value.ToString();
                query = "SELECT count(orders.id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = " + order_id + ");";
                command = new MySqlCommand(query, conn);
                MySqlDataReader sda = command.ExecuteReader();
                while (sda.Read())
                    dgv_selectedOrders[11,r.Index].Value = sda.GetInt32(0).ToString();*/

            }

这个循环只执行了两次,然后就停止了……

有趣的是,当我取消注释除“MessageBox”之外的所有内容时,它会很好地遍历每一行并显示我的 DataGridView 的每个“order_id”。

应该做的是:

遍历现有的 DataGridView 并从每一行获取“Order_id”。 然后我想计算客户已经下了多少订单,并将结果附加到 DataGridView 的第 11 列中...

有什么想法吗?

正如我所说,它在第二行之后停止......

【问题讨论】:

  • 如果先生成字符串,然后在消息框中显示会怎样?你逐行调试过吗?
  • 嘿伙计。是的,我做到了。问题似乎是 MySqlDataReader 行。它停在那里。查询也很好。嗯...
  • 但要完全回答您的问题:SELECT count(orders.id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = 111) 第二个是:SELECT count(orders. id) AS total_orders FROM orders WHERE customer_id = (SELECT customer_id FROM orders WHERE id = 111) ... 两个查询都很好。但是还有 20 行要走,但它只是停止了......疯了......

标签: c# datagridview mysqldatareader


【解决方案1】:

好吧,伙计们,

所以解决方案非常简单。

先关闭 MySqlDataReader -.-...大声笑

sda.Close();

在while循环之后。

干杯,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    相关资源
    最近更新 更多