0

我的应用程序允许用户进行会议预约。在约会被保存到数据库之前,用户从日历控件中选择一个日期。一旦选择了日期,该页面应该从数据库中获取当天的约会列表,并检查每个记录约会的时间。任何已经预订的时间段都将从 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

如果在此处循环,则 for 的值Mid(dr("apptdate"), 12, 5)将作为01:00. 数据库中的实际值表示13:00

谁能建议为什么会发生这种转换?它导致比较失败,因此该时间段没有从 DropDownList 中删除。

提前致谢!

4

1 回答 1

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()
于 2012-06-27T09:29:13.393 回答