显示进度条在执行一个SQL查询 [英] Displaying a progressbar while executing an SQL Query

查看:2184
本文介绍了显示进度条在执行一个SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要通知用户数据正在从SQL数据库
阅读,我决定创建一个进度条形式,但它不工作 - 也许是因为需要一个线程。我希望以编程方式创建表单

I want to inform the user while data is being read from an SQL database and I decided to create a form with a progressbar but it doesn't work - maybe because a thread is needed. I want to create the form programmatically

        ProgressBar pb = new ProgressBar();

        pb.MarqueeAnimationSpeed = 30;
        pb.Style = ProgressBarStyle.Marquee;
        pb.Dock = DockStyle.Fill;

        progressForm.ClientSize = new Size(200, 50);
        progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
        progressForm.StartPosition = FormStartPosition.CenterScreen;
        progressForm.Controls.Add(pb);
        progressForm.ControlBox = false;
        progressForm.TopMost = true;

        progressForm.Show();  
        //do data processes here (all queries and executes)
        progressForm.close();



如何修改上面的代码来实现我的既定目标?

How do I modify the code above to achieve my stated goals?

编辑:顺便说一下,我想在我的项目中使用该进度表中每一个数据的功能。例如:fillGrid,runQuery ..

edit: Btw, I want to use this progressbar form in every data functions in my project. For example: fillGrid, runQuery..

@Will非常感谢你对你的答案。我的意思是我该如何使用类的一个功能,例如我的gridFill功能是在连接类:

@Will thank you very much for your answers. I meant how can I use a function of class for example my gridFill function is in that connection class:

 class ConnectionClass
    {
       public static SqlConnection connection = new SqlConnection();

    public string sorgu;
    public static string server;
    public static string userId;
    public static string catalog;
    public static string password;
    public static string accessMethod;
    public DataSet ds = new DataSet();
    Form progressForm = new Form();       

    public bool Open()
    {
        try
        {
            if (connection.State != ConnectionState.Open)
            {

                connection.ConnectionString = "Data Source = " + server + ";" +
                                              "Initial Catalog=" + catalog + ";" +
                                              "User ID=" + userId + ";" +
                                              "Password=" + password + ";" +
                                              "Connect Timeout=0";

                connection.Open();
                return true;
            }
            else
            {
                return true;
            }


        }
        catch (Exception ex)
        {
            MessageBox.Show("System message:" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

    }

    public DataTable Dt(string query)
    {
        DataTable dt = new DataTable();
        if (Open())
        {
            SqlDataAdapter da = new SqlDataAdapter(query, connection);
            try
            {   
                //progressForm.Showdialog()  is this possible???
                da.Fill(dt);
                //progressForm.close(); ??
            }
            catch (Exception ex)
            {
                MessageBox.Show("Sistem Mesajı:" + ex.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }         
        return dt;
    }

    public bool Run(string query, string hataMsj)
    {
        Form activeForm = Form.ActiveForm;
        query = " SET DATEFORMAT DMY " + query;

        SqlCommand sc = new SqlCommand(query, connection);
        try
        {
            Open();
            sc.ExecuteNonQuery();
            return true;
        }           
        catch (Exception )
        {
            return false;
        }
    }

    public void fillComboBox(string sorgu, ComboBox cb, string text, string value)
    {
        DataTable dt = Dt(sorgu);

        cb.DisplayMember = text;
        cb.ValueMember = value;
        cb.DataSource = dt;
        if (cb.Items.Count > 0)
        {
            cb.SelectedIndex = 0;
        }

    }

    public int fillGridView(string sorgu, DataGridView dgv)
    {
        DataTable dtGrvw = Dt(sorgu);
        dgv.DataSource = dtGrvw;
        return 1;
    }       
    }

和例如查询从另一个窗体(类)

and example queries from another form(class)

   ConnectionClass cc = new ConnectionClass();

    query= "  INSERT INTO tblPersonel (" +
                                          " [sqlUserName] " +
                                          ",[personelNo] " +
                                          ",[ad] " +
                                          ",[soyad] " +
                                          ",[departmanId] " +
                                          ",[emailadres] " +
                                          ",[tcKimlikNo],[kangurubu],[dokumaciNo])VALUES" +
                                          "('" + tbSqlUserName.Text +
                                          "','" + tbPersonelNo.Text +
                                          "','" + tbAd.Text +
                                          "','" + tbSoyad.Text +
                                          "','" + cbDepartman.SelectedValue.ToString() +
                                          "','" + tbMail.Text +
                                          "','" + tbKimlikno.Text + 
                                          "','" + tbKangrubu.Text + 
                                          "','" + tbDokumaciNo.Text + "' ) ";
                    if (cc.Run(query, "Unexpected error on insert new person"))
                    {
                        fillGrid();
                        this.Close();

                    }

    public void fillGrid()
    {
        query= " select * from View_Personel order by personelNo desc";
        cc.fillGridView(query, gridviewPersonel);
    }

和我不能想象我如何使用它在bw_DoWork事件。因为我的函数的参数。(查询,GridView的),当我把它从另一个类,我可以带参数...

and I cant imagine how can I use it in bw_DoWork event. because my function has parameters.(query, gridview) when I call it from another class I can use it with parameters...

P.S使用它。 :这个方法对我来说是相当不错的但它没有工作。我不明白的问题。

p.s. : this Method is pretty good for me but it didnt worked. I didnt understand the problem

推荐答案

使用BackgroundWorker的类来填补你的DataGrid中。

Use the BackgroundWorker class to fill your DataGrid.

     Form progressForm;

     public void func() {
        BackgroundWorker bw = new BackgroundWorker ();
        bw.DoWork += new DoWorkEventHandler (bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler (bw_RunWorkerCompleted);

        progressForm = new Form ();

        ProgressBar pb = new ProgressBar ();

        pb.MarqueeAnimationSpeed = 30;
        pb.Style = ProgressBarStyle.Marquee;
        pb.Dock = DockStyle.Fill;

        progressForm.ClientSize = new Size (200, 50);
        progressForm.FormBorderStyle = FormBorderStyle.FixedDialog;
        progressForm.StartPosition = FormStartPosition.CenterScreen;
        progressForm.Controls.Add (pb);
        progressForm.ControlBox = false;
        progressForm.TopMost = true;

        progressForm.Show ();

        string queryString = "SELECT ...."; // fill query string here
        var params = new KeyValuePair<GridControl, string>(sorgu, queryString);
        bw.RunWorkerAsync (params);
    }

    void bw_DoWork (object sender, DoWorkEventArgs e) {
        KeyValuePair<GridControl, string> params = e.Argument as KeyValuePair<GridControl, string>;
        ConnectionClass cc = new Connection Class();
        cc.fillGrid(params.Value, params.Key);
    }

    void bw_RunWorkerCompleted (object sender, RunWorkerCompletedEventArgs e) {
        progressForm.Close (); //
    }



是可能的一个参数发送到BackgroundWorker的。如果你需要一个以上的参数,你可以发送包含你需要的任何对象的元组

It is possible to send a parameter to the BackgroundWorker. If you need more than one parameter, you can send a Tuple which contains any objects you need.

编辑:如果你在3.5中,你可以使用一个KeyValuePair代替。代码更新为。

If you're on 3.5, you can use a KeyValuePair instead. Code is updated for that.

这篇关于显示进度条在执行一个SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆