如何显示datagridview看起来像是从mysql下载数据

时间:2015-11-04 17:39:42

标签: mysql vb.net visual-studio-2010 vb.net-2010

当数据从mysql加载到datagridview时 它在同一时间显示出来 我想要做的是当datagridview显示五行或七行时应该停止提取数据4到5秒然后再连续停止5秒 就像是 用于向用户显示datagridview从mysql下载数据

请某人帮助我@ tim-schmelter

Private Sub Button3_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理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

1 个答案:

答案 0 :(得分:0)

使用代码示例更新。

如果需要以这种方式检索数据,则可以让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

但我不建议以这种方式检索和显示数据,因为它会损害程序的性能。你所拥有的将带来更好的表现。