C#中的数据库加载进度

本文关键字:加载 数据库 | 更新日期: 2023-09-27 18:27:00

我正在开发一个应用程序,该应用程序将OLE数据库加载到DataGridView中。尽管数据库文件是本地存储的,但应用程序加载数据库需要一些时间,所以我只得到一个"加载"光标。我想要更多:我想创建一个进度条,在数据库加载时显示,在数据库完全加载时隐藏。

我在谷歌上搜索过,但没有找到我要找的东西。我能做什么?

(我在Visual Studio中工作,所以请记住,数据集的整个代码都是由IDE自动编写的)

C#中的数据库加载进度

您可能正在寻找与ProgressBar一起使用的BackgroundWorker将两者都写在你的表格上,并使用以下代码:

public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    Shown += new EventHandler(Form1_Shown);
    // To report progress from the background worker we need to set this property
    backgroundWorker1.WorkerReportsProgress = true;
    // This event will be raised on the worker thread when the worker starts
    backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
    // This event will be raised when we call ReportProgress
    backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
}
void Form1_Shown(object sender, EventArgs e)
{
    // Start the background worker
    backgroundWorker1.RunWorkerAsync();
}
// On worker thread so do our thing!
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    // Your background task goes here
    for (int i = 0; i <= 100; i++)
    {
        // Report progress to 'UI' thread
        backgroundWorker1.ReportProgress(i);
        // Simulate long task
        System.Threading.Thread.Sleep(100);
    }
}
// Back on the 'UI' thread so we can update the progress bar
void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // The progress percentage is a property of e
    progressBar1.Value = e.ProgressPercentage;
}

}

这只是如何使用它的一个例子。您需要修改:

backgroundWorker1.ReportProgress(i);

因此,它实际上是在报告OLE数据库的进展情况。