如何显示 datagridview 看起来像是从 mysql 下载数据
how to show datagridview look like it's downloading data from mysql
当数据从 mysql 加载到数据网格视图时
它同时显示
我想要做的是,当 datagridview 显示五行或七行时,它应该停止获取数据 4 到 5 秒,然后应该再次持续停止 5 秒
就像是
用于向用户显示 datagridview 从 mysql
下载数据
请有人帮助我@tim-schmelter
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MysqlConn = New MySqlConnection()
' Define the SQL to grab data from table.
SQL = "SELECT * FROM dep0 "
'Connection String
MysqlConn.ConnectionString = "Server=sql2.freemysqlhosting.net;User Id=sql294434;Password=mI1*hrrD9*;Database=sql294434"
' Try, Catch, Finally
Try
MysqlConn.Open()
ContactsCommand.Connection = MysqlConn
ContactsCommand.CommandText = SQL
ContactsAdapter.SelectCommand = ContactsCommand
ContactsAdapter.Fill(ContactsData)
DataGridView1.DataSource = ContactsData
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
更新了代码示例。
如果您需要以这种方式检索数据,您可以让 SQL 查询仅检索 5 到 7 行,将该数据作为数据源分配给 DataGridView,然后 运行 一个循环,使用 System.Threading.Thread.Sleep(5000)(告诉它等待 5 秒),每个循环让它再抓取 5 到 7 行,并且每次都将新数据源分配给 DataGridView。
或者您可以让它在计时器上检索数据。这样可以更顺利地更新表单。这是您如何操作的示例。
Private WithEvents myTimer As New Timer
Private Sub myTimer_Tick() Handles myTimer.Tick
Try
GetData()
Catch ex As Exception
'Error handeling
End Try
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnGetData.Click
Try
myTimer.Interval = 5000
myTimer.Start()
Catch ex As Exception
'Error handeling
End Try
End Sub
Private Sub GetData()
Dim MysqlConn As New Data.SqlClient.SqlConnection
Dim ContactsCommand As New Data.SqlClient.SqlCommand
Dim ContactsAdapter As New Data.SqlClient.SqlDataAdapter
Dim ContactsData As New Data.DataTable
'Declare Static variables so they retain their values for the next sub call.
Static TopNumber As Integer = 5
Static NewRowCount As Integer = 0
Static OldRowCount As Integer = -1
Try
If OldRowCount < NewRowCount Then
MysqlConn.ConnectionString = "Server=sql2.freemysqlhosting.net;User Id=sql294434;Password=mI1*hrrD9*;Database=sql294434"
MysqlConn.Open()
ContactsCommand.Connection = MysqlConn
ContactsCommand.CommandText = "SELECT TOP " & TopNumber & " * FROM dep0 "
ContactsAdapter.SelectCommand = ContactsCommand
ContactsAdapter.Fill(ContactsData)
DataGridView1.DataSource = ContactsData
OldRowCount = NewRowCount
NewRowCount = ContactsData.Rows.Count
'Increment the variable to grab 5 more the next time.
TopNumber += 5
Else
myTimer.Stop()
MessageBox.Show("Done!")
End If
Catch ex As Exception
'Error handling
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
但我不建议以这种方式检索和显示数据,因为它会损害程序的性能。你所拥有的将带来更好的表现。
当数据从 mysql 加载到数据网格视图时 它同时显示 我想要做的是,当 datagridview 显示五行或七行时,它应该停止获取数据 4 到 5 秒,然后应该再次持续停止 5 秒 就像是 用于向用户显示 datagridview 从 mysql
下载数据请有人帮助我@tim-schmelter
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MysqlConn = New MySqlConnection()
' Define the SQL to grab data from table.
SQL = "SELECT * FROM dep0 "
'Connection String
MysqlConn.ConnectionString = "Server=sql2.freemysqlhosting.net;User Id=sql294434;Password=mI1*hrrD9*;Database=sql294434"
' Try, Catch, Finally
Try
MysqlConn.Open()
ContactsCommand.Connection = MysqlConn
ContactsCommand.CommandText = SQL
ContactsAdapter.SelectCommand = ContactsCommand
ContactsAdapter.Fill(ContactsData)
DataGridView1.DataSource = ContactsData
Catch myerror As MySqlException
MessageBox.Show("Cannot connect to database: " & myerror.Message)
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
更新了代码示例。
如果您需要以这种方式检索数据,您可以让 SQL 查询仅检索 5 到 7 行,将该数据作为数据源分配给 DataGridView,然后 运行 一个循环,使用 System.Threading.Thread.Sleep(5000)(告诉它等待 5 秒),每个循环让它再抓取 5 到 7 行,并且每次都将新数据源分配给 DataGridView。
或者您可以让它在计时器上检索数据。这样可以更顺利地更新表单。这是您如何操作的示例。
Private WithEvents myTimer As New Timer
Private Sub myTimer_Tick() Handles myTimer.Tick
Try
GetData()
Catch ex As Exception
'Error handeling
End Try
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles btnGetData.Click
Try
myTimer.Interval = 5000
myTimer.Start()
Catch ex As Exception
'Error handeling
End Try
End Sub
Private Sub GetData()
Dim MysqlConn As New Data.SqlClient.SqlConnection
Dim ContactsCommand As New Data.SqlClient.SqlCommand
Dim ContactsAdapter As New Data.SqlClient.SqlDataAdapter
Dim ContactsData As New Data.DataTable
'Declare Static variables so they retain their values for the next sub call.
Static TopNumber As Integer = 5
Static NewRowCount As Integer = 0
Static OldRowCount As Integer = -1
Try
If OldRowCount < NewRowCount Then
MysqlConn.ConnectionString = "Server=sql2.freemysqlhosting.net;User Id=sql294434;Password=mI1*hrrD9*;Database=sql294434"
MysqlConn.Open()
ContactsCommand.Connection = MysqlConn
ContactsCommand.CommandText = "SELECT TOP " & TopNumber & " * FROM dep0 "
ContactsAdapter.SelectCommand = ContactsCommand
ContactsAdapter.Fill(ContactsData)
DataGridView1.DataSource = ContactsData
OldRowCount = NewRowCount
NewRowCount = ContactsData.Rows.Count
'Increment the variable to grab 5 more the next time.
TopNumber += 5
Else
myTimer.Stop()
MessageBox.Show("Done!")
End If
Catch ex As Exception
'Error handling
Finally
MysqlConn.Close()
MysqlConn.Dispose()
End Try
End Sub
但我不建议以这种方式检索和显示数据,因为它会损害程序的性能。你所拥有的将带来更好的表现。