如何循环StreamWriter从ChecklistBox中写入字符串

本文关键字:ChecklistBox 字符串 StreamWriter 何循环 循环 | 更新日期: 2023-09-27 17:49:18

大家好!我需要帮助。我想循环我的StreamWriter从checkListBox在记事本中写入每个字符串,并使其作为批处理文件执行SQL脚本。

情况:每个checkLISTbox是一个SQL,然后我需要通过StreamWriter逐个保存它,然后使用SQLCMD在批处理文件上执行它。但我需要再循环一次。让我们说一个问题2或2.5秒(这就是为什么我使用Thread.Sleep(2000))之前运行下一个批处理文件,其中包含另一行文本,即下一个复选框

        StreamWriter SaveFile = new StreamWriter(file_name);
        try
        {   for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++)
            {
                foreach (Object item in checkedListBox1.CheckedItems)
                {
                    SaveFile.WriteLine(":r '"" + textBox1.Text + "''" + item + "'"");
                    SaveFile.Close();
                    //Create a batchfile for execution of consolidated script 
                    string execute = "''Script_Runner.bat";
                    execute = textBox1.Text + execute;
                    StreamWriter SaveFile2 = new StreamWriter(execute);
                    SaveFile2.WriteLine("sqlcmd -S localhost -E -i " + textBox1.Text + "''" + "test1.sql");
                    SaveFile2.Close();
                    //running the batchfile
                    string ScriptRun = "''Script_Runner.bat";
                    ScriptRun = textBox1.Text + ScriptRun;
                    Process.Start(ScriptRun);
                }
                this.Refresh();
                Thread.Sleep(2000);
                i++;
            }
        }
        catch
        {
            //error message every script
        }                                              
    }
    catch
    {
    //error message 
    }
    richTextBox2.Text += "'nData Patch Completed.";                  
}
catch (Exception)
{
   // error message upon clicking execute
}

如何循环StreamWriter从ChecklistBox中写入字符串

由于您没有明确表示需要两个循环,我认为一个是缺陷。在循环的第一项上关闭流可能是您不想要的。我将Streamwriter-instantiation移到了循环中。也许对你有用——试试吧。

            try
            {   
                    foreach (Object item in checkedListBox1.CheckedItems)
                    {
                        StreamWriter SaveFile = new StreamWriter(file_name);
                        SaveFile.WriteLine(":r '"" + textBox1.Text + "''" + item + "'"");
                        SaveFile.Close();
                        //Create a batchfile for execution of consolidated script 
                        string execute = "''Script_Runner.bat";
                        execute = textBox1.Text + execute;
                        StreamWriter SaveFile2 = new StreamWriter(execute);
                        SaveFile2.WriteLine("sqlcmd -S localhost -E -i " + textBox1.Text + "''" + "test1.sql");
                        SaveFile2.Close();
                        //running the batchfile
                        string ScriptRun = "''Script_Runner.bat";
                        ScriptRun = textBox1.Text + ScriptRun;
                        Process.Start(ScriptRun);
                        Thread.Sleep(2000);
                    }
                    this.Refresh();
            }
            catch
            {
                //error message every script
            }                                              
        }
        catch
        {
        //error message 
        }                   
                richTextBox2.Text += "'nData Patch Completed.";                  
    }
    catch (Exception)
    {
       // error message upon clicking execute