如何计算到期前三个月的到期日期

时间:2019-02-05 15:51:37

标签: mysql vb.net

我需要帮助,以了解如何在到期日前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

2 个答案:

答案 0 :(得分:1)

我评论了我们的Call Connection()。最好将您的连接保持在本地,以确保它们已关闭并已废弃。
即使存在错误,Using...End Using块也可以完成此操作。另外,我也看不到将连接与命令关联的位置。在这种情况下,不需要call关键字。我假设Connection()返回了一个连接,但是您没有提供变量来保存该连接。

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

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

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

一个空的捕获只会吞下错误。好主意。

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)

答案 1 :(得分:0)

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

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