.net查询数据时,显示数据装载进度的方法

一般我们在大数据量查询的情况下,由于消耗的时间比较多,所以为了给用户更好的使用体验,我们往往会在查询的时候加载一个进度条,以便让用户能准确的知道程序的运行状态。而在使用DBDATAADAPTER的FILL方法是很难完成这个任务,那么通过什么方式可以实现呢?这让我想到了DataReader。利用它再辅助一些办法是可以实现查询数据的时候显示进度条的。

以下是测试代码

添加三个控件 button datagridview processbar 分别取名button1 DGV PB

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Datatable As New DataTable Dim Command As New OleDbCommand Dim i As Integer Dim Row As DataRow Dim Reader As OleDbDataReader Dim Con As OleDbConnection = New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=data.mdb") Command.Connection = Con Command.CommandText = "select count(*) from plu" Con.Open() If Command.ExecuteScalar IsNot Nothing Then Dim count As Integer = CInt(Command.ExecuteScalar) If count > 0 Then PB.Visible = True Command.CommandText = "select * from plu" Reader = Command.ExecuteReader() For k As Integer = 0 To Reader.FieldCount - 1 Datatable.Columns.Add(Reader.GetName(k)) Next While Reader.Read i += 1 PB.Value = (i / count) * 100 Row = Datatable.NewRow For k As Integer = 0 To Reader.FieldCount - 1 Row(k) = Reader.GetValue(k) Next Datatable.Rows.Add(Row) End While If PB.Value = 100 Then PB.Visible = False End If Reader.Close() Datatable.AcceptChanges() DGV.DataSource = Datatable End If End If Con.Close() End Sub

经过本机测试ACCESS数据库20万条数据时间消耗大概在10秒,如果为了主界面能更好的响应,可以利用多线程,让不界面不存在假死的状况。

你可能感兴趣的:(.net查询数据时,显示数据装载进度的方法)