我需要在晚上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
答案 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;
得到毫秒。
1000
属性设置为Enabled
,以阻止该过程再次运行。 (将时间间隔设置为False
不会不会导致计时器停止。)0
的最大值为2,147,483,647毫秒,因此,您将来可以使用此方法设置时间表的最长时间约为 25天。