Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C消息框一行多个显示_C#_Database_Winforms_Foreach_Messagebox - Fatal编程技术网

C# C消息框一行多个显示

C# C消息框一行多个显示,c#,database,winforms,foreach,messagebox,C#,Database,Winforms,Foreach,Messagebox,我正在为数据库的列表视图编写代码,我想保存所有的列,它保存了,但每行都显示一个messagebox。有人能帮我解决这个问题吗?当按钮点击时,我只想显示一个消息框。 这是我的代码 foreach (ListViewItem li in listView1.Items) { string condense = "datasource=localhost;port=3306;username=root;password=''"; string milk = "insert into c

我正在为数据库的列表视图编写代码,我想保存所有的列,它保存了,但每行都显示一个messagebox。有人能帮我解决这个问题吗?当按钮点击时,我只想显示一个消息框。 这是我的代码

foreach (ListViewItem li in listView1.Items)
{
    string condense = "datasource=localhost;port=3306;username=root;password=''";
    string milk = "insert into cashier.sales(Cashier,Orders,Quantity,Size,Price,Date) values ('" + this.cashier.Text + "','" + li.SubItems[0].Text + "','" + li.SubItems[1].Text + "','" + li.SubItems[2].Text + "','" + li.SubItems[3].Text + "','" + this.dateTimePicker1.Value + "');";
    MySqlConnection conDatabase = new MySqlConnection(condense);
    MySqlCommand cmdDatabase = new MySqlCommand(milk, conDatabase);
    MySqlDataReader myReader;

    if (string.IsNullOrEmpty(cashier.Text))
    {
        MessageBox.Show("Please fill the Notes. ", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    }
    else
    {
        conDatabase.Open();
        myReader = cmdDatabase.ExecuteReader();

        MessageBox.Show("Order has been added successfully  ", "Order!", MessageBoxButtons.OK, MessageBoxIcon.Information);

         total.Text = "";
         amount.Text = "";
         change.Text = "";
         while (myReader.Read())
         {
         }
     }
}          
将MessageBox移到循环之外

foreach (ListViewItem li in listView1.Items)
    {

        string condense = "datasource=localhost;port=3306;username=root;password=''";
        string milk = "insert into cashier.sales(Cashier,Orders,Quantity,Size,Price,Date) values ('" + this.cashier.Text + "','" + li.SubItems[0].Text + "','" + li.SubItems[1].Text + "','" + li.SubItems[2].Text + "','" + li.SubItems[3].Text + "','" + this.dateTimePicker1.Value + "');";
        MySqlConnection conDatabase = new MySqlConnection(condense);
        MySqlCommand cmdDatabase = new MySqlCommand(milk, conDatabase);
        MySqlDataReader myReader;

        if (string.IsNullOrEmpty(cashier.Text))
        {
            MessageBox.Show("Please fill the Notes. ", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            conDatabase.Open();
            myReader = cmdDatabase.ExecuteReader();

            total.Text = "";
            amount.Text = "";
            change.Text = "";
            while (myReader.Read())
            {

            }
        }
    }

MessageBox.Show("Orders has been added successfully  ", "Order!", MessageBoxButtons.OK, MessageBoxIcon.Information);

把你的messagebox.show放在foreach循环之外。现在你说每行显示消息框…omg。非常感谢。简单的逻辑是,消息框显示代码在循环中。你需要把它放在外面。