如何从数据库中读取日期和时间并将其与系统日期和时间进行比较

How to read date and time from database and compare it to the system date and time

我正在使用 vb 进行一个项目,我的一个表单必须将当前活动的提醒(尚未达到截止日期的提醒)显示到数据网格视图中,我还有另一个数据网格视图用于提醒已经过了最后期限。日期和时间连同提醒信息被保存到我的访问数据库中,我想从数据库中读取日期和时间并将其与系统日期和时间进行比较,然后显示提醒信息。

这是我的表单的样子;顶部的数据网格视图用于当前提醒,底部的数据网格视图用于 past/out 个日期提醒:

这是我的表单代码和我尝试过的代码:

Imports System.Data.OleDb

Public Class frmReminderInfo

    Private Sub frmReminderInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DisplayCurrentReminders()
    End Sub

    Private Sub DisplayCurrentReminders()
        Dim ReminderDateTime As Date
        Dim CurrentDateTime As Date
        CurrentDateTime = Date.Now
        CurrentDateTime = FormatDateTime(Date.Now, DateFormat.GeneralDate)
        ReminderDateTime = FormatDateTime(ReminderDateTime, DateFormat.GeneralDate)
        If DbConnect() Then
            DgvCurrentReminders.Rows.Clear()
            Dim SQLCmd As New OleDbCommand
            With SQLCmd
                .Connection = cn
                .CommandText = "SELECT ReminderDate FROM TblReminder "

                Dim rs As OleDbDataReader = .ExecuteReader()
                While rs.Read
                    ReminderDateTime = (rs(0).ToString)
                End While
            End With
        End If
        cn.Close()
        If CurrentDateTime = ReminderDateTime Then
            Dim SQLCmd As New OleDbCommand
            With SQLCmd
                Dim rs As OleDbDataReader = .ExecuteReader()
                While rs.Read
                    Dim NewStockRow As New DataGridViewRow()
                    NewStockRow.CreateCells(DgvCurrentReminders)
                    NewStockRow.SetValues({rs("ReminderID"), rs("CustomerName"), rs("DeviceInfo"), rs("RepairPrice"), rs("ReminderDate")})
                    NewStockRow.Tag = rs("ReminderID")
                    DgvCurrentReminders.Rows.Add(NewStockRow)
                End While
                rs.Close() 
            End With
        End If
        cn.Close()

    End Sub
End Class

Connection 这样的一次性数据库对象应该在 Using 块中使用它们的方法中本地声明。

您似乎对 Sql 字符串的各个部分的含义一无所知。 Select 部分列出了您要检索的字段。此子句中的星号 (*) 表示 select 所有字段。这里我们使用 Where 子句来过滤记录。仅返回字段 ReminderDate 大于或等于参数的记录。这仅在数据已作为 DateTime.

正确插入时才有效

While 循环在每次迭代中不断覆盖 ReminderDateTime 的值,因此只保留 reader 返回的最后一个值。此外,您正试图将 String 强制转换为声明为 Date 的变量。不行。

假设代码可以超出 If CurrentDateTime = ReminderDateTime Then 您将使用关闭的连接。 Commands 无法在关闭的连接上执行。

您似乎也不知道 class 对象是如何工作的。 Dim SQLCmd As New OleDbCommand 此处声明了 Command 的新实例。您没有连接也没有 CommandText 所以它不可能被执行。

看看下面的代码,直到它开始有意义。查看 Using 块的作用。查找 DataTableLoad 方法以了解它的作用。查看 DataSource 属性 提供的内容。

Private Sub frmReminderInfo_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt = GetCurrentReminders()
    DgvCurrentReminders.DataSource = dt
End Sub

Private Function GetCurrentReminders() As DataTable
    Dim dt As New DataTable
    Using cn As New OleDbConnection("Your connection string"),
            SQLCmd As New OleDbCommand("SELECT * FROM TblReminder Where ReminderDate >= @Date", cn)
        SQLCmd.Parameters.Add("@Date", OleDbType.Date).Value = Now
        cn.Open()
        Using reader = SQLCmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    Return dt
End Function