2014-09-11 72 views
0

我想連接在線MySQL數據庫以同步顯示在VB.NET窗體中。我使用計時器在5秒鐘後自動更新DatagridView,但這會使應用程序變得非常糟糕和緩慢。同步Mysql數據庫與vb.net圖形用戶界面GridDataView

是否有任何方法可以使觸發器或類似的東西更新DataGridView只有當數據庫得到更新?

這是我當前的代碼:

Imports MySql.Data.MySqlClient 

Public Class Main 
    Dim sqlcon As MySqlConnection 
    Dim sqlcom As MySqlCommand 
    Public Function UpdateAppointment() 
     sqlcon = New MySqlConnection 
     sqlcom = New MySqlCommand 
     sqlcon.ConnectionString = "server=******;user id=******;password=*****;database=*****" 
     Dim SDA As New MySqlDataAdapter 
     Dim dbDataset As New DataTable 
     Dim bSource As New BindingSource 

     Dim query As String = " SELECT Name,Program,level,dayT, fromT , toT, why, status from student JOIN appointment ON appointment.userid=student.id join FreeTime on appointment.freetimeid=FreeTime.stuffid where appointment.stuffid='" & LogIn.id & "' " 
     If LogIn.CheckForInternetConnection() Then 
      Try 
       sqlcon.Open() 

       sqlcom = New MySqlCommand(query, sqlcon) 

       SDA.SelectCommand = sqlcom 
       SDA.FillAsync(dbDataset) 
       bSource.DataSource = dbDataset 
       DataGridView1.DataSource = bSource 
       SDA.UpdateAsync(dbDataset) 
       Me.DataGridView1.FirstDisplayedScrollingRowIndex = Me.DataGridView1.RowCount - 1 

       TSlableConnection.Text = "Connected to the Database" 
       TSlableConnection.BackColor = Color.Green 
       sqlcon.Close() 

      Catch ex As MySqlException 
       TSlableConnection.Text = "Cannot connect to database: " & ex.Message 
       TSlableConnection.BackColor = Color.Red 
      Finally 

       sqlcon.Dispose() 
      End Try 
     End If 
     Return DBNull.Value 
    End Function 

    Private Sub ButtonAccept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAccept.Click 

    End Sub 


    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     TextBoxName.Text = LogIn.SName 
     TextBoxTitle.Text = LogIn.Title 
     TextBoxStatus.Text = LogIn.Status 
     TextBoxRoom.Text = LogIn.RoomNum 
     TextBoxDepartment.Text = LogIn.Department 
     TextBoxPhone.Text = LogIn.PhoneNum 
     UpdateAppointment() 
    End Sub 

    Private Sub ToolStripLabel1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 

Handles ToolStripLabel1.Click 
     Try 
      Dim updater As MySqlDataReader 
      Dim Query2 As String = "UPDATE stuff SET Connection=0 WHERE id='" & LogIn.id & "'" 

      LogIn.MysqlCon.Open() 
      LogIn.com2 = New MySqlCommand(Query2, LogIn.MysqlCon) 
      updater = LogIn.com2.ExecuteReader 

      LogIn.MysqlCon.Close() 

     Catch ex As MySqlException 

     Finally 

      LogIn.MysqlCon.Dispose() 
     End Try 
     Me.Hide() 
     LogIn.Show() 

    End Sub 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    Try 
     Dim updater As MySqlDataReader 
     Dim Query2 As String = "UPDATE `stuff` SET `RoomNo`='" & TextBoxRoom.Text & "',`Status`='" & TextBoxStatus.Text & "',`Phone`='" & Val(TextBoxPhone.Text) & "' WHERE id='" & LogIn.id & "'" 

     LogIn.MysqlCon.Open() 
     LogIn.com2 = New MySqlCommand(Query2, LogIn.MysqlCon) 
     updater = LogIn.com2.ExecuteReader 
     MsgBox("The data has been updated..") 
     LogIn.MysqlCon.Close() 

    Catch ex As MySqlException 
     MsgBox("Cannot connect to database: " & ex.Message) 
    Finally 

     LogIn.MysqlCon.Dispose() 
     End Try 
    End Sub 

    Private Sub DataGridView2_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick 

    End Sub 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     UpdateAppointment() 
    End Sub 
End Class 

回答

0

您可以使用此教程,這可能是你的情況更好的後臺工作,background worker tutorial

讓我知道如果你有任何問題,我會樂意效勞。

關於。