1

開始時間用と終了時間用の 2 つの列があります。両方ともタイプnvarcharなので、比較できます。

ユーザーから時間を受け取り、時間が有効かどうかを確認するために自動的にポストバックするテキスト ボックスがあります。

Dim compared_time_1 As DateTime
    Dim compared_time_2 As DateTime

    Dim select_time As SqlCommand
    select_time = New SqlCommand("select Start_Time , End_Time from Clinic_Schedule where Schedule_no = @sch_no", appt_DB_1)
    select_time.Parameters.AddWithValue("@sch_no", Sch_no)

    Dim time_rdr As SqlDataReader
    time_rdr = select_time.ExecuteReader()

    While time_rdr.Read
        compared_time_1 = DateTime.Parse(start_time_1)
        compared_time_2 = DateTime.Parse(end_time_1)
        start_time_1 = time_rdr(0).ToString
        end_time_1 = time_rdr(1).ToString




        If appt_time_txt0.Text >= start_time_1 And appt_time_txt0.Text <= end_time_1 Then
            date_valid_lbl0.Visible = True
            date_valid_lbl0.Text = "*Valid Time"

        Else
            time_valid_lbl0.Visible = True
            time_valid_lbl0.Text = "*Not Valid Time"
        End If


    End While
    time_rdr.Close()

ロジック XD に問題があるかどうかはわかりません。これらの列に入力されたデータは次の形式です:00:00AM or 00:00PM. ご協力をお願いします..ありがとう

4

2 に答える 2

0

リーダーからデータをロードする前に解析を行っているようです。

compared_time_1 = DateTime.Parse(start_time_1)
compared_time_2 = DateTime.Parse(end_time_1)
start_time_1 = time_rdr(0).ToString
end_time_1 = time_rdr(1).ToString

する必要があります

start_time_1 = time_rdr(0).ToString
end_time_1 = time_rdr(1).ToString
compared_time_1 = DateTime.Parse(start_time_1)
compared_time_2 = DateTime.Parse(end_time_1)

しかし、私はそれとは違うことをします。時刻が有効かどうかを確認したい場合は、datetime の TryParse メソッドを使用できます。おそらく、少しリファクタリングすることも役立つかもしれません。最後に、スケジュール項目が午前 0 時前に開始され、翌日の午前 0 時以降に終了する可能性がある場合、時間を比較すると問題が生じる可能性があることに注意してください。

Sub ReadingData()

   'initializing reader stuff here...

    Dim dtStart As DateTime, dtEnd As DateTime
    If DateTime.TryParse(time_rdr(0).ToString, dtStart) = False Then
        HandleInvalidTime()
    End If

    If DateTime.TryParse(time_rdr(1).ToString, dtEnd) = False Then
        HandleInvalidTime()
    End If

   'Closing out reader stuff here...

    HandleValidTime(dtStart, dtEnd)

End Sub

Sub HandleValidTime(TheStartTime As DateTime, TheEndTime As DateTime)
    'Do Stuff
End Sub

Sub HandleInvalidTime()
    'Do Stuff
End Sub
于 2012-12-13T17:13:50.887 に答える
0

文字列を日付データ型と比較しているようです。

次のように、両方を日付データ型に変換していることを確認してください。秒と「AM」の間のスペースに注意してください。

time1=CDate("3:19:40 AM")
time2=CDate("3:10:40 AM")

次に、次のように比較を実行します。

if time1>time2 then
   'logic 
end if
于 2012-12-13T16:19:57.537 に答える