保存以在晚上8点或特定时间访问数据库

时间:2018-10-07 04:58:31

标签: database vb.net ms-access

我需要在晚上8点的特定时间保存到数据库,我似乎无法保存它。.

Public Sub updateDatabase(ByVal data As String) // update database using this function
    With txtIn //textbox 

            'Dim con As OleDbConnection = New OleDbConnection("D:\POLI\SEM 5\PROJECT\Monitoring PH and Temperature\Monitoring PH and Temperature\PHTempdb.mdb")
            'Dim sql2 As String = String.Empty

            Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\POLI\SEM 5\PROJECT\Monitoring PH and Temperature\Monitoring PH and Temperature\PHTempdb.mdb") //database 

            'Dim dateTime.Now As String = Date 
            Dim date1 As Date = DateTime.Now

            Dim str As String = date1.ToString("yyyy/MM/dd")
            Dim insert As String = "INSERT INTO report (PHVALUE_TEMPERATURE, DATE_TIME) VALUES ('" & data & "', '" & DateTime.Now & "');"

            Dim cmd As New OleDbCommand(insert, conn)
        conn.Open()

        If (DateTime.Now.Hour = 20 & DateTime.Now.Minute = 00) Then //save at 8 pm daily

            cmd.ExecuteNonQuery() // execute 
        End If

        System.Threading.Thread.Sleep(5000)
        '.Clear()
        'cmd.ExecuteNonQuery()
        conn.Close()
        ' End If
        '.Clear()
    End With
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
    Dim str As String = sp.ReadExisting()
    Invoke(myDelegate, str)
    updateDatabase(str)


End Sub

1 个答案:

答案 0 :(得分:0)

如果您可以通过简单地运行一个过程以编程方式正常地保存数据库,则可以add a Timer Control to your form并将其设置为从现在起以毫秒为宜。

给出排定的日期时间和current datetime,您可以使用DateDiff计算从“现在”开始的秒数,然后乘以with cte as ( select stuff(path, 1, len(v.element) + 1, '') as path, v.element, 1 as lev from table1 cross apply (values (left(path, charindex('_', path + '_') - 1))) v(element) union all select stuff(cte.path, 1, len(v.element) + 1, ''), left(path, charindex('_', path + '_') - 1) as element, 1 + lev from cte cross apply (values (left(path, charindex('_', path + '_') - 1))) v(element) where path <> '' ) select * from cte where lev = 3; 得到毫秒。

您可以找到有关使用计时器控件herehere的详细说明。


注释

  • 在计时器事件调用的过程开始时,将1000属性设置为Enabled,以阻止该过程再次运行。 (将时间间隔设置为False不会不会导致计时器停止。)
  • 由于将计时器间隔设置为相对于当前时间 ,因此不能“重复使用”,但是如果您要自动为第二天,您可以简单地重新计算毫秒数,并在过程执行结束时重置计时器。
  • 0的最大值为2,147,483,647毫秒,因此,您将来可以使用此方法设置时间表的最长时间约为 25天