Home .net data reader in background worker
Reply: 0

.net data reader in background worker

user1940 Published in March 23, 2018, 9:03 am

I am trying to query a DB in a background worker in order for my form not to be frozen while the query is being processed. However since I moved to this solution, the data reader only retrieves the first result of the query instead of 400.000+ ...

Public Class Form5
    Public con As OdbcConnection = Form1.con
    Public cmd As New OdbcCommand
    Public dr As OdbcDataReader

    Dim bw As BackgroundWorker = New BackgroundWorker
    Public Delegate Sub PictureVisibilityDelegate(ByVal visibility As 
    Dim ChangePictureVisibility As PictureVisibilityDelegate

    Private Sub Form5_Load(sender As Object, e As EventArgs) Handles 
        AddHandler bw.DoWork, AddressOf bw_DoWork
        AddHandler bw.RunWorkerCompleted, AddressOf bw_RunWorkerCompleted
        ChangePictureVisibility = AddressOf ChangeVisibility


    End Sub

    Public Sub ChangeVisibility(ByVal visibility As Boolean)
        PictureLoader.Visible = visibility
    End Sub

    Private Sub bw_DoWork(sender As Object, e As DoWorkEventArgs)
        Me.Invoke(ChangePictureVisibility, True)

        Dim SQL As String = "SELECT xxx, xxx, xxx from xxx.xxx where xxx.xxx      
        = """ & dt1.Rows(Form1.line).Item(4) & """"
        cmd = New OdbcCommand(SQL, con)

        dr = cmd.ExecuteReader

        While dr.Read()
           DataGridView1.Rows.Add(New String() 
           {dr(0).ToString,dr(1).ToString & " %", dr(2).ToString})
        End While

     End Sub

    Private Sub bw_RunWorkerCompleted(ByVal sender As Object, ByVal e As                 
        Me.Invoke(ChangePictureVisibility, False)
    End Sub

End Class

I really can't wrap my head around that and couldn't find any similar problem on the net, so any comment would be really appreciated...

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.546243 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO