gpt4 book ai didi

mysql - 我想使用backgroundworker和datagridview显示mysql数据

转载 作者:行者123 更新时间:2023-11-29 11:52:30 25 4
gpt4 key购买 nike

当我尝试从 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

最佳答案

您仅在 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

关于mysql - 我想使用backgroundworker和datagridview显示mysql数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33809560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com