从数据库读取数据时需要制作进度条 [英] Need make progress bar when data is reading from database

查看:30
本文介绍了从数据库读取数据时需要制作进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的进度条有问题.我有一个按钮(SELECT * FROM Table).

I have a problem with progress bar. I have a button (SELECT * FROM Table).

当我点击它时,我想在datagridview中显示数据.没问题,工作正常.

When I click it, I want to display data in datagridview. It's ok, works fine.

但现在我需要制作一个进度条,而数据正在从数据库中读取.

But now I need make a progress bar, while data is reading from DB.

默认进度条可见应该是假的.阅读时 - 进度条可见是真的.

By default progress bar visible should be as false. While reading - progress bar visible is true.

并且当从数据库读取数据时,进度条可见应该再次为假.

And when data is read from DB, progress bar visible should be false again.

我有一个代码,但它工作不正确(1. 进度条有问题等等)也许你可以给我提供另一种变体?

I have a code, but it works incorrect (1. progress bar is buggy and etc.) Maybe you can offer me another variant?

 private void button3_Click(object sender, EventArgs e)
 {
    timer1.Start();         
 }

 private void timer1_Tick(object sender, EventArgs e)
 {
        progressBar1.Visible = true;
        progressBar1.Minimum = 10;
        progressBar1.Maximum = 100;

        progressBar1.Increment(+10);
        if (progressBar1.Value == 90)
        {         
            groupBox3.Enabled = true;
            connect con = new connect();
            DataTable data = connnection.query("SELECT * FROM User WHERE surname LIKE '" + textBox1.Text + "%'");
            dataGridView1.DataSource = tb;

        }
        if(progressBar1.Value == 100)
          progressBar1.Visible = false;
    }

推荐答案

你需要重新思考你的逻辑.

You need to rethink your logic.

当您进行 SQL 调用时,将其视为在您的程序之外运行的代码,这意味着您只能启动它并等待它完成,但您将无法访问内部结构,例如它的进展情况.

When you make that SQL call think of it as code running outside your program, meaning you can only start it up and wait for it to finish, but you won't have access to the internals like how far it is progressing.

作为替代方案,您可以做的是:

What you can do as an alternative is:

  1. 将您的 SQL 调用放入 BackgroundWorker.MSDN 上很棒的示例,而且非常易于使用.
  2. 当您调用 BackgroundWorker 时,这会释放您的 UI 以运行某种请稍候"控件.由于您不知道 SQL 命令何时结束,至少您可以设置某种控件让用户知道程序正在处理".
  3. 一旦 BackgroundWorker 的 RunWorkerCompleted 发生,您可以删除您的 Please Wait 并绑定您的数据.
  1. Put your SQL call in a BackgroundWorker. Great examples on MSDN and is really pretty easy to use.
  2. When you call the BackgroundWorker, that frees up your UI to run some kind of "Please Wait" control. Since you won't have any idea when the SQL command will end, at least you can put up some kind of control to let the user know that the program is "Processing".
  3. Once the BackgroundWorker's RunWorkerCompleted happens, you can remove your Please Wait and bind up your data.

这篇关于从数据库读取数据时需要制作进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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