gpt4 book ai didi

mysql - 如何计算过期日期 3个月前就过期了

转载 作者:行者123 更新时间:2023-11-29 09:52:37 25 4
gpt4 key购买 nike

我需要帮助如何获取过期日期前 3 个月的提醒。我用的是mysql。

Try
Call connection()
cmd.CommandText = "select * from medicine where expiry_date < date_sub(now(), interval 3 month)"
dr = cmd.ExecuteReader
count = 0
While dr.Read
count = count + 1
End While
If count = 1 Then
pop.Image = Image.FromFile("E:\asasda.png")
pop.TitleText = "Notification Alert!!!"
pop.ContentText = "Medicine at Risk"
pop.AnimationDuration = 3000
pop.Popup()
Else
pop.Image = Image.FromFile("E:\asasda.png")
pop.TitleText = "Notification Alert!!!"
pop.ContentText = "No items for risk"
pop.AnimationDuration = 3000
pop.Popup()
End If
Catch ex As Exception

End Try

最佳答案

我评论了我们的Call Connection()。最好将您的连接保留在本地,这样您就可以确保它们已关闭并被处置。
即使出现错误,Using...End using block 也将完成此操作。我也没有看到你在哪里将连接与你的命令关联起来。在这种情况下,call 关键字不是必需的。我假设 Connection() 返回一个连接,但您没有提供变量来保存连接。

Select 语句和连接直接传递给命令的构造函数。

您已消耗了在 While 循环中返回的所有数据。如果您只需要 Count,则请求 Count 并使用 .ExecuteScalar

我不明白 If 的意义,因为 if 部分与 else 部分相同。

空的 Catch 只会吞掉错误。坏主意。

Private Sub OPCode()
Dim CountReturned As Integer
Try
'Call Connection()
Using cn As New MySqlConnection("Your connection string")
Using cmd As New MySqlCommand("select Count(*) from medicine where expiry_date < date_sub(now(), interval 3 month);", cn)
cn.Open()
CountReturned = CInt(cmd.ExecuteScalar)
End Using
End Using
If CountReturned = 1 Then
pop.Image = Image.FromFile("E:\asasda.png")
pop.TitleText = "Notification Alert!!!"
pop.ContentText = "Medicine at Risk"
pop.AnimationDuration = 3000
pop.Popup()
Else
pop.Image = Image.FromFile("E:\asasda.png")
pop.TitleText = "Notification Alert!!!"
pop.ContentText = "No items for risk"
pop.AnimationDuration = 3000
pop.Popup()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

如果您无法使 MySql data_sub 正常工作,请使用 vb 和参数。

Using cmd As New MySqlCommand("select Count(*) from medicine where expiry_date < @Minus3Months;", cn)
cmd.Parameters.Add("@Minus3Months", MySqlDbType.DateTime).Value = Now.AddMonths(-3)

关于mysql - 如何计算过期日期 3个月前就过期了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54538230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com