如何计算3个月前的到期日期将到期

问题描述 投票:0回答:2

我需要帮助如何在到期日期前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
mysql vb.net
2个回答
1
投票

我评论了我们的Call Connection()。最好将您的连接保持在本地,这样您就可以确保它们已关闭并处理掉。 即使有错误,Using...End Using块也会实现这一点。此外,我没有看到您与命令的连接关联的位置。在这种情况下,不需要call关键字。我假设Connection()返回一个连接但你没有提供一个变量来保持连接。

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

您已经使用了在While循环中返回的所有数据。如果您只需要伯爵,那么请求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)

0
投票
cmd.CommandText = "select * from medicine where expiry_date < @threeMonthsAgo"
cmd.parameters.add("@threeMonthsAgo", variableWithYourDate)

您可以使用参数从VB传递值。

© www.soinside.com 2019 - 2024. All rights reserved.