0

我在 c# 中设计了一个查询执行器,它允许您选择多个文件(.sql)并在从列表中选择的多个数据库上运行这些查询。因为这个过程有时需要很长时间,我想给用户一些信息。在我的代码中,我首先遍历每个数据库,对于每个数据库,我遍历所有文件(.sql)并执行它们。在我的表单中,我有两个标签,我想显示当前数据库,另一个显示正在运行的文件。这已经完成了,问题是标签没有更新,表单被冻结,我知道我必须使用线程,但不知道如何。这是我的代码:

for (int i = 0; i < listBox1.CheckedItems.Count; i++)
            {
                String bd = listBox1.CheckedItems[i].ToString();

                this.textBD.Text = bd; //UPDATE CURRENT DATABASE LABEL

                bd = bd.Replace("CL25DEMAYO.", "");

                string ConnStrng = "Data Source=CL25DEMAYO;Initial Catalog=" + bd + "; User Id=sa;Password=190205;";

                try
                {
                    this.SqlConn = new SqlConnection(ConnStrng);
                    this.SqlConn.Open();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }



                for (int e = 0; e < listBoxArchivos.Items.Count; e++)
                {
                    String archi = listBoxArchivos.Items[e].ToString();

                    this.textScript.Text = archi; //UPDATE CURRENT FILE LABEL

                    FileInfo file = new FileInfo(archi);
                    string script = file.OpenText().ReadToEnd();

                    SqlCommand command = new SqlCommand(script, SqlConn);

                    command.ExecuteNonQuery();
                }

                SqlConn.Close();
            }

如果有人可以帮助我,将不胜感激。

4

1 回答 1

0

最后,我使用BackgroundWorker解决了,并通过invoke()方法更新标签。

于 2013-06-27T15:53:34.003 回答