我正在使用 vb 處理一個專案,我的一個表單必須將當前活動的提醒(尚未到達截止日期的提醒)顯示到 datagridview 中,我還有另一個 datagridview 用于超過截止日期的提醒。日期和時間與提醒資訊一起保存到我的訪問資料庫中,我想從資料庫中讀取日期和時間并將其與系統日期和時間進行比較,然后顯示提醒資訊。
這就是我的表格的樣子;頂部的 datagridview 用于當前提醒,底部的用于過去/過期提醒:

這是我的表單代碼以及我嘗試過的代碼:
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
uj5u.com熱心網友回復:
一次性資料庫物件,例如Connection應該在Using塊中使用它們的方法中本地宣告。
您似乎不知道 Sql 字串的各個部分是什么意思。該Select部分列出了您要檢索的欄位。*此子句中的星號 ( ) 表示選擇所有欄位。這里我們使用一個Where子句來過濾記錄。僅ReminderDate回傳欄位大于或等于引數的記錄。這僅在資料作為DateTime.
該While回圈不斷改寫的值ReminderDateTime在每次迭代所以只由閱讀器回傳的最后一個值將保持不變。此外,您試圖將 a 強制String轉換為宣告為 a 的變數Date。不會作業。
假設代碼可能超出If CurrentDateTime = ReminderDateTime Then您的范圍,您將使用關閉的連接。Commands 不能在關閉的連接上執行。
您似乎也不知道類物件是如何作業的。Dim SQLCmd As New OleDbCommand在這里,您宣告了Command. 你沒有連接,沒有CommandText所以它不可能被執行。
看看下面的代碼,直到它開始變得有意義。查看Using塊的作用。查看Loada的方法,DataTable看看它做了什么。查看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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/361240.html
下一篇:滑動擴展影片旁邊的專案
