如何在C#Windows窗体中单击按钮时暂停和恢复命令

如何在C#Windows窗体中单击按钮时暂停和恢复命令,c#,visual-studio-2010,C#,Visual Studio 2010,我想在程序运行while命令一次后暂停程序,并等待按钮4\u单击继续进行一次迭代。每次单击按钮4都应运行while循环一次 代码: 如果要在每次单击按钮后处理单个客户记录,则不需要while循环 相反,您要做的是存储记录,关闭数据库连接,并在每次单击按钮4时处理一条记录 你也不能以你想要的方式终止一个while循环,这完全违背了他们的目的 试着这样做: private void button2_click() { /* Open your database connection, ret

我想在程序运行
while
命令一次后暂停程序,并等待
按钮4\u单击
继续进行一次迭代。每次单击按钮4都应运行
while
循环一次

代码:


如果要在每次单击按钮后处理单个客户记录,则不需要
while
循环

相反,您要做的是存储记录,关闭数据库连接,并在每次单击按钮4时处理一条记录

你也不能以你想要的方式终止一个while循环,这完全违背了他们的目的

试着这样做:

private void button2_click()
{
    /* Open your database connection, retrieve your results, store them in 'read' as previously */
    recordList = new List<YourObject>(); //Define this at the class level, not the method level, so it is accessible to other methods
    while(read.Read())
    {
        recordList.Add(new YourObject(read.GetValue(0).ToString(), read.GetValue(1).ToString(), read.GetValue(2).ToString());
    }
    /* Close your connections */
}

private void button4_click()
{
    //You should probably check to make sure recordList[0] isn't null first
    this.listView1.Items.Add(recordList[0].A);
    this.listView1.Items.Add(recordList[0].B);
    this.listView1.Items.Add(recordList[0].C);
    recordList.removeAt[0];
}

如果要在每次单击按钮后处理单个客户记录,则不需要
while
循环

相反,您要做的是存储记录,关闭数据库连接,并在每次单击按钮4时处理一条记录

你也不能以你想要的方式终止一个while循环,这完全违背了他们的目的

试着这样做:

private void button2_click()
{
    /* Open your database connection, retrieve your results, store them in 'read' as previously */
    recordList = new List<YourObject>(); //Define this at the class level, not the method level, so it is accessible to other methods
    while(read.Read())
    {
        recordList.Add(new YourObject(read.GetValue(0).ToString(), read.GetValue(1).ToString(), read.GetValue(2).ToString());
    }
    /* Close your connections */
}

private void button4_click()
{
    //You should probably check to make sure recordList[0] isn't null first
    this.listView1.Items.Add(recordList[0].A);
    this.listView1.Items.Add(recordList[0].B);
    this.listView1.Items.Add(recordList[0].C);
    recordList.removeAt[0];
}

我对你的问题进行了编辑,以澄清你的意思并解决一些次要的语法问题。如果您认为我没有改进您的问题,请随时回复。最好在conn、cmd和read上使用。如果抛出异常(例如,当查询超时),您的关闭调用将被跳过。我编辑了您的问题,以澄清您的意思并修复一些次要的语法问题。如果您认为我没有改进您的问题,请随时回复。最好在conn、cmd和read上使用。如果抛出异常(例如,当查询超时),将跳过关闭调用。
private class YourObject
{
    string A,B,C;

    public YourObject( string a, string b, string c)
    {
        A = a;
        B = b;
        C = c;
    }
}