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

时间:2016-06-24 04:49:51

标签: c# sql arrays loops streamwriter

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

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

        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
}

1 个答案:

答案 0 :(得分:1)

因为你没有明确表示需要两个循环我认为一个是缺陷.. 关闭循环中第一个项目的流可能是你不想要的.. 我在循环中移动了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