显示从 MySQL 填充数据集的进度
我目前正在使用 C# 和 MySQL 数据库后端开发一个应用程序。
我的程序最终可能会从数据库加载大量数据并添加到数据集中以在 DataGridView 中显示。我希望能够显示数据集填充的进度,但不确定如何获取它在数据库中位置的引用。
下面是我目前拥有的代码。
DatabaseWork dbase = new DatabaseWork();
try
{
dbase.openConnection();
MySqlDataAdapter myDA = new MySqlDataAdapter();
myDA.SelectCommand = new MySqlCommand(query, dbase.conn);
DataTable table = new DataTable();
myDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
tblDetails.DataSource = bSource;
//tblGrid.Columns[0].Visible = false;
}
catch (MySqlException ex)
{
dbase.displayError(ex.Message, ex.Number);
}
finally
{
dbase.closeConnection();
}
我知道我必须像后台工作人员一样将这部分代码放入线程中,但如何更改此代码以显示进度。
I am currently developing an application using C# and a MySQL Database Backend.
My program could end up loading a large amount of data from the database and adding into a dataset to be displayed in a DataGridView. I want to be able to show the progress of the filling of the DataSet but not sure how I can get a reference to where it is in the database.
Below is the code that I currently have.
DatabaseWork dbase = new DatabaseWork();
try
{
dbase.openConnection();
MySqlDataAdapter myDA = new MySqlDataAdapter();
myDA.SelectCommand = new MySqlCommand(query, dbase.conn);
DataTable table = new DataTable();
myDA.Fill(table);
BindingSource bSource = new BindingSource();
bSource.DataSource = table;
tblDetails.DataSource = bSource;
//tblGrid.Columns[0].Visible = false;
}
catch (MySqlException ex)
{
dbase.displayError(ex.Message, ex.Number);
}
finally
{
dbase.closeConnection();
}
I know that I will have to put this section of code into a Thread like a Background Worker but how can I change this code to show the progress.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个答案可能来得有点晚,但也许对其他人有帮助。
在许多情况下,显示到目前为止已读取的记录数就足够了。这可以通过处理 DataTable.RowChanged 事件来完成。从测试实现中,我可以验证该事件是否为 DataAdapter.Fill-Method 添加的每一行触发。处理事件时,您可以通过查看 DataTable.Rows.Count-Property 来获取读取的记录数。
我通常做的是在后台线程中读取数据,以更新标签或列表框条目。实际更新 gui 的方法会缓冲标签的更新,以便 gui 更改每秒仅发生一次,以防止闪烁。
希望这有帮助。
萨沙
This answer might come a little late, but maybe it helps others.
In many cases it is sufficient to show the number of records read so far. That could be done by handling the DataTable.RowChanged-event. From a test implementation I could verify, that the event fires for each row added by the DataAdapter.Fill-Method. When handling the event you could get the number records read by looking at the DataTable.Rows.Count-Property
What I usually do is read the data in a background thread, that updates a label or listbox entry. The method that actually updates the gui buffers the updates to the label so that the gui changes occure only once a second to prevent flickering.
Hope this helps.
Sascha
而不是
......您应该能够逐行填充表格:
尽管它不如使用 Fill 方法那么整洁,所以我不确定您是否想这样做。当然,为了能够显示工作完成了多少百分比,您还需要通过“Select count”或类似方法获取表中的行数,正如 phsr 指出的那样。
Instead of
... you should be able to fill the table row by row:
It's not as neat as using the Fill method though, so I'm not sure if you want to do it this way. And of course, in order to be able to show how many percent of the work is done, you will also need to get the number of rows in the table by "Select count" or similar, just as phsr pointed out.
我会用 Stecya 评论将进度条实现为选框进度条。
为了能够更新数据集填充的进度条,您必须事先知道您有多少记录,然后继续执行诸如包装 dataAdapter 之类的操作以了解它已将多少记录放入数据集中,这是我的我不完全确定你能做到。
如果绑定数据源也需要相当长的时间,我会在字幕栏上显示一条状态消息,并用“检索记录”更新它,然后用“渲染记录”的内容进行更新。
I would go with Stecya comment an implement the progress bar as a marquee progress bar.
For you to be able to update a progress bar of the dataset fill, you would have to know how many records you have beforehand, then keep do something like wrapping the dataAdapter to know how many records it has put into the dataset, which I'm not entirely sure you could do.
If binding the dataSource takes a considerable amount of time as well, I would have a status message along with the marquee bar, and update it with "Retrieving records", then something along the lines of "Rendering records".