我的应用允许用户进行会议约会。在将约会保存到数据库之前,用户从Calendar控件中选择日期。选择日期后,页面应从数据库中获取当天的约会列表,并检查每个记录的约会的时间。任何已经预订的时间段都将从DropDownList中删除,用户将从中选择一个方便的时间。
问题在于我的时间比较。下面的大部分代码都是为了区分日期/时间值以获得我可以比较的东西。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
PopulateTime()
End If
End Sub
Protected Sub PopulateTime()
Dim db As New Database
Dim sql As String = "select apptinterval from centres where centreid = @id"
Dim args As New List(Of SqlParameter)
args.Add(New SqlParameter("@id", Session("CentreID")))
Dim dr As SqlDataReader = db.GetReader(sql, args.ToArray)
Dim interval As Integer = 0
If dr.Read Then
interval = dr("apptinterval")
End If
Dim time = TimeSpan.FromHours(8)
While time < TimeSpan.FromHours(17)
time += TimeSpan.FromMinutes(interval)
ddlAppointmentTime.Items.Add(New ListItem(time.ToString, time.ToString))
End While
End Sub
Protected Sub calAppointmentDate_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles calAppointmentDate.SelectionChanged
Dim seldate As String = Left(calAppointmentDate.SelectedDate, 10)
Dim dateargs() As String = seldate.ToString.Split("/")
Dim newdate As String = dateargs(0) & "-" & dateargs(1) & "-" & dateargs(2)
GetTimes(newdate)
End Sub
Protected Sub GetTimes(ByVal TargetDate As String)
Dim db As New Database
Dim args As New List(Of SqlParameter)
Dim sql As String = "select apptdate from appointments where apptdate between @date1 and @date2 order by apptdate"
args.Add(New SqlParameter("@date1", TargetDate & " 12:00:00"))
args.Add(New SqlParameter("@date2", TargetDate & " 23:59:59"))
Dim dr As SqlDataReader = db.GetReader(sql, args.ToArray)
If dr.HasRows Then
dr.Read()
For i As Integer = 0 To ddlAppointmentTime.Items.Count - 1
If Left(ddlAppointmentTime.Items(i).Value, 5) = Mid(dr("apptdate"), 12, 5) Then
ddlAppointmentTime.Items.RemoveAt(i)
End If
Next
End If
dr.Close()
End Sub
如果循环此处,Mid(dr("apptdate"), 12, 5)
的值将返回01:00
。数据库中的实际值显示为13:00
。
有人能说出为什么会发生转换吗?它导致比较失败,因此,没有从DropDownList中删除该时间段。
提前致谢!
答案 0 :(得分:0)
似乎是Mid
做到了。此处涉及的字符串转换更改了值。
我通过删除Mid
并更改sql语句来解决这个问题,如下所示:
Dim sql As String = "select left(CONVERT(time, apptdate, 14), 5) as apptdate from appointments"
Dim dr As SqlDataReader = db.GetReader(sql, args.ToArray)
If dr.HasRows Then
While dr.Read
For i As Integer = 0 To ddlAppointmentTime.Items.Count - 1
If Left(ddlAppointmentTime.Items(i).Value, 5) = dr("apptdate") Then
ddlAppointmentTime.Items.RemoveAt(i)
End If
Next
End While
End If
dr.Close()