比较MS访问数据库的时间与当前系统时间

时间:2014-02-16 07:30:03

标签: vb.net ms-access

美好的一天!我是使用MS访问和visual basic的初学者。目前,我很难将数据库中的时间与当前系统时间进行比较。我有一个数据库,其中有一个时间表,计划将与当前的系统时间进行比较。如果十五分钟过去了,计算机屏幕上会显示有人尚未登录的消息。以下是我的代码:

Public Class Form1

    Dim cnn As New OleDb.OleDbConnection
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim date1 As Date
        Dim date2 As Date = Date.Now
        Dim cmd As New OleDb.OleDbCommand
        If Not cnn.State = ConnectionState.Open Then
            cnn.Open()
        End If
        Dim myDate As New OleDb.OleDbDataAdapter("SELECT DT FROM Test", cnn)
        cnn.Close()
    End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

        cnn = New OleDb.OleDbConnection
        cnn.ConnectionString = "Provider=Microsoft.ACE.Oledb.12.0; Data Source=" & Application.StartupPath & "\Database121.accdb"
    End Sub
End Class

任何回复都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

Microsoft.ACE.OLEDB.12.0连接允许我们在Access SQL查询中使用DateDiff()Now()函数,因此您可以使用以下内容(在C#中,但VB.NET将是非常相似):

using (var cmd = new OleDbCommand())
{
    cmd.Connection = con;
    cmd.CommandText = 
            @"SELECT DateDiff(""n"",[LastLogin],Now()) AS mins " +
            @"FROM [Clients] WHERE [ID]=1";
    int mins = Convert.ToInt32(cmd.ExecuteScalar());
    Console.WriteLine(String.Format("Last login was {0} minute(s) ago.", mins));
}

有关DateDiff()功能的详细信息,请参阅Office在线文档

中的以下页面

DateDiff Function