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