Dim readers As MySqlDataReader
cn.Open()
Dim query As String
query = "Select * from tblmeds where expdate='" & DateTime.Now & "'"
command = New MySqlCommand(query, cn)
readers = command.ExecuteReader
Dim count As Integer
count = 0
While readers.Read
count = count + 1
End While
cn.Close()
If count = 0 Then
MsgBox("no expiration")
Else
MsgBox("medicine at risk")
End If
this is just the code i made but it doesnt work because it doesnt go to while instead is goes straight for if count=0. also how can i say to my program "if the expiration date on the database is near month i set to" sorry for my bad english
You are checking if any of your items has an expiry date exactly equals to the current value of DateTime.Now. This property contains also the Time part and thus it is highly improbable that you have an item with exactly the same value.
I think you would use instead 'expdate lesser than now' thus your code should be
Dim readers As MySqlDataReader
cn.Open()
Dim query As String
query = "Select * from tblmeds where expdate<=@exp"
command = New MySqlCommand(query, cn)
command.Parameters.Add("@exp", MySqlDbType.DateTime).Value = DateTime.Now;
readers = command.ExecuteReader
Dim count As Integer
count = 0
While readers.Read
count = count + 1
End While
cn.Close()
If count = 0 Then
MsgBox("no expiration")
Else
MsgBox("medicine at risk")
End If
Instead if you want to check for expiry date equals to today date then the expression to use is DateTime.Today
instead of DateTime.Now.
Notice also that is a very good practice to never concatenate strings to form sql queries. Use always a parameter approach to avoid parsing and conversion errors.
为什么不使用时间戳记差异:
query = "Select * from tblmeds where TIMESTAMPDIFF(MONTH,`expdate`,CURRENT_TIMESTAMP())< 1"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.