【问题标题】:i want to show mysql data using backgroudworker and dtagridview我想使用 backgroundworker 和 datagridview 显示 mysql 数据
【发布时间】:2015-11-19 16:39:16
【问题描述】:

我在尝试从 mysql 数据库中获取数据时遇到问题

我总是得到最后一条记录。

如何让datagridview上的所有记录同步显示

这是我的代码

谁能帮帮我

Public Class Form1

    Private RowCount As Integer = 0


    Private Sub bgw_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
        ListBox1.Items.Add(e.UserState)
        ProgressBar1.Value = e.ProgressPercentage
        Label1.Text = "Processing row.. " + e.ProgressPercentage.ToString() 
        DataGridView1.Rows.Add(New Object() {f1, f2})
         Me.ProgressBar1.Maximum = RowCount 

    End Sub
    Dim ListText As String
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles go.Click
        go.Enabled = False
        cancel.Enabled = True
        ListBox1.Items.Clear()
        ProgressBar1.Value = 0
        BackgroundWorker1.WorkerReportsProgress = True
        BackgroundWorker1.WorkerSupportsCancellation = True
        BackgroundWorker1.RunWorkerAsync()
        Me.Cursor = Cursors.WaitCursor
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cancel.Click
        BackgroundWorker1.CancelAsync()
    End Sub

    Private Sub bgw_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        go.Enabled = True
        cancel.Enabled = False
        Me.Cursor = Cursors.Arrow

        DataGridView1.Refresh()
        DataGridView1.CurrentCell = DataGridView1(0, DataGridView1.Rows.Count - 1)
    End Sub
    Dim f1 As String
    Dim f2 As String
    Dim ds As New DataSet
    Dim command As MySqlCommand
    Dim reader As MySqlDataReader
    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
         GetRecordCount()
        Dim connetionString As String
        Dim connection As MySqlConnection
        Dim i As Integer = 0
        Dim sql As String
        connetionString = "Server=db4free.net;User Id=mouhcindinaoui;Password=$$$$$$;Database=mouhcindinaoui"

        sql = "SELECT id,nom FROM dep02 "
        connection = New MySqlConnection(connetionString)
        Try

            connection.Open()
            command = New MySqlCommand(sql, connection)
            reader = command.ExecuteReader()
            For Value As Integer = 0 To rowcount

                If reader.HasRows Then
                    Do While reader.Read()

                        f1 = reader.GetString("id")
                        f2 = reader.GetString("nom")

                    Loop
                End If
                ListText = String.Concat("Sequence #", Value)
                BackgroundWorker1.ReportProgress(Value, ListText)
                Thread.Sleep(10)

            Next
            reader.Close()
            command.Dispose()
            connection.Close()
        Catch ex As Exception
            '   MsgBox("Can not open connection ! ")
        End Try
    End Sub
    Private Sub GetRecordCount()
        Dim connetionString As String
        Dim connection As MySqlConnection
        Dim command As MySqlCommand
        Dim sql As String
        connetionString = "Server=db4free.net;User Id=mouhcindinaoui;Password=dinaouimouhcin1991;Database=mouhcindinaoui"

        sql = "Select count(*) from dep02"
        connection = New MySqlConnection(connetionString)
        command = New MySqlCommand(sql, connection)
        connection.Open()
        RowCount = CInt(command.ExecuteScalar())

    End Sub

End Class

【问题讨论】:

  • 你真的不需要所有这些:你可以很容易地填充数据表并将其绑定到 DGV。您可以从 ReportProgress 传递其他数据,但这不是它的用途

标签: mysql vb.net datagridview backgroundworker


【解决方案1】:

您仅在 DataReader 上的循环结束时调用 ReportProgress。当然,此时用于设置网格中项目的两个变量 f1 和 f2 包含 DataReader 读取的最后一条记录的值。

您需要在 DataReader 循环内移动调用

    For Value As Integer = 0 To rowcount

        If reader.HasRows Then
            Do While reader.Read()

                f1 = reader.GetString("id")
                f2 = reader.GetString("nom")
                ListText = String.Concat("Sequence #", Value)
                BackgroundWorker1.ReportProgress(Value, ListText)
            Loop
        End If
        Thread.Sleep(10)
    Next

然而,对 GetRecordCount 的调用和对 rowcount 变量的外部循环是完全没用的(完全错误)。 DataReader 上的 while 循环不需要它,您可以将其全部删除,用局部变量的简单增量替换所有内容,以保持当前记录在 datareader 循环内的进步

Dim recNum = 1
If reader.HasRows Then
    Do While reader.Read()

        f1 = reader.GetString("id")
        f2 = reader.GetString("nom")

        ListText = String.Concat("Sequence #", recNum)
        BackgroundWorker1.ReportProgress(Value, ListText)
        recNum = recNum + 1
    Loop
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-26
    • 1970-01-01
    • 2022-01-24
    • 2012-03-08
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 2011-07-31
    相关资源
    最近更新 更多