首页 > 解决方案 > 如何在 VB NET 函数中正确地在某个时间间隔(定时器)刷新 SQL Server 中的数据

问题描述

我有一些代码用于每 5 秒检查一次来自 sql server 的数据。它实际上很好,但我看到内存使用量增长缓慢?什么会导致这种情况?

这是简单的代码:Public LastID as integer=0 Public NewID as integer=0

Private Sub Timer1_Tick(sender As Object, e As EventArgs) 处理 TimerSken.Tick

    Dim com As New SqlCommand("Select * from Products  where datediff(second,FinishedDate, GETDATE())<7 order by ID desc", Connection)
    Dim ad As New SqlDataAdapter(com)
    Dim tableTimer As New DataTable()
    ad.Fill(tableTimer)

    If tableTimer.Rows.Count = 0 Then
        Return
    End If

    NewID = tableTimer.Rows.Item(0)("ID")

    If LastID <> NewID Then

        Dim Tittle As String = tableTimer.Rows.Item(0)("Operator").ToString & ", " & tableTimer.Rows.Item(0)("FinishedDate").ToString
        Dim text As String = tableTimer.Rows.Item(0)("Customer").ToString & ", " & tableTimer.Rows.Item(0)("WorkID").ToString & vbNewLine & tableTimer.Rows.Item(0)("Product").ToString & ", " & tableTimer.Rows.Item(0)("Operation").ToString
        LastID = NewID

        Notification.Show(Me, info.Caption = Tittle, info.Text = text)
    End If

End Sub

我把所有这些都放在计时器里可以吗?

标签: sql-servervb.nettimer

解决方案


推荐阅读