当此值为今天的日期时,如何在Label上显示特定值的计数

时间:2014-01-29 16:54:48

标签: sql vb.net label

我有一个带有Appointment表的SQL Server数据库,其中包含NameDate列。如果Label列等于“今天日期”,我想显示每条记录的计数(在Date上)。我还想在另一个标签上显示当月的约会数量。我怎样才能做到这一点?我正在使用VB.NET。

1 个答案:

答案 0 :(得分:2)

这样的事情应该有效:

Public Function GetAppointmentsCount(startDate As Date, endDate As Date) As Integer
    Using connection As New SqlConnection("")
        connection.Open()
        Using command As SqlCommand = connection.CreateCommand()
            command.CommandText = "select count([Date]) from Appointment where [Date] >= @StartDate and [Date] <= @EndDate"
            command.Parameters.AddWithValue("StartDate", startDate)
            command.Parameters.AddWithValue("EndDate", endDate)
            Return CInt(command.ExecuteScalar())
        End Using
    End Using
End Function

然后你可以这样称呼它:

Dim startOfDay As Date = Date.Today
Dim endOfDay As Date = startOfDay.AddDays(1).AddTicks(-1)
Dim dayCount As Integer = GetAppointmentsCount(startOfDay, endOfDay)

Dim startOfMonth As Date = New Date(Date.Today.Year, Date.Today.Month, 1)
Dim endOfMonth As Date = startOfMonth.AddMonths(1).AddTicks(-1)
Dim monthCount As Integer = GetAppointmentsCount(startOfMonth, endOfMonth)

lblDayCount.Text = dayCount.ToString() & " appointment(s) today"
lblMonthCount.Text = monthCount.ToString() & " appointment(s) this month"