从数据库中选择就可以得到第一个数据

时间:2015-03-21 18:25:04

标签: asp.net vb.net

    Dim reserveTime As String
    Dim Timer As String = ddlTime.SelectedValue
    Dim dat As String
    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Beauty shop\Beauty shop\Beauty.accdb'")
    If conn.State = ConnectionState.Open Then
        conn.Close()
    End If
    conn.Open()
    Dim reserveDate As String = Calendar1.SelectedDate
    Dim bookingTime As String = "SELECT Booking_Date,Booking_Time FROM CustomerBooking where Booking_Date='" & reserveDate & "'"
    Dim sqlcommand2 As New OleDbCommand(bookingTime, conn)
    Dim dr2 As OleDbDataReader
    dr2 = sqlcommand2.ExecuteReader
    If dr2.HasRows Then
        dr2.Read()
        Dim count As Integer = dr2.FieldCount
        While dr2.Read()
            For i As Integer = 0 To count - 1
                dat = dr2.Item("Booking_Date")
                reserveTime = dr2.Item("Booking_Time")
            Next
        End While
        dr2.Close()
        If Timer = reserveTime And dat = reserveDate Then
            ShowTimeLbl.Text = "This time have been reserve"
        Else
            Dim sqlString As String
            sqlString = "Insert INTO CustomerBooking(Customer_Name, Package_Name, Price, Booking_Date, Booking_Time, Card_Bank, Cardholder_Name, Card_Number,Expired_Month,Expired_Date) VALUES ('" & User_Name & "','" & Package_Name & "','" & Price_Package & "','" & Calendar_Book & "','" & Time_Book & "','" & Card_Name & "','" & Card_Holder & "','" & CreditCard_Number & "','" & Month_Card & "','" & Year_Card & "')"
            Dim sqlcommand As New OleDbCommand(sqlString, conn)
            sqlcommand.ExecuteNonQuery()
            conn.Close()
            Response.Redirect("ConfirmBook.aspx?Name=" + Master.NameMaster + "&UserName=" + User_Name + "&PackageName=" + Package_Name + "&PackagePrice=" + Price_Package + "&Calender=" + Calendar_Book + "&TimeBook=" + Time_Book + "&CardName=" + Card_Name + "&CardHolder=" + Card_Holder + "&CreditCardNumber=" + CreditCard_Number + "&MonthCard=" + Month_Card + "&YearCard=" + Year_Card)
        End If
    End If
当我选择时间所有日期都无法运行时,我的问题是什么,我想比较日期和时间,如果时间已预订则会显示完整,但只是可以读取第一个日期,为什么不能运行第二个日期?日期相同

1 个答案:

答案 0 :(得分:1)

你在循环之前保留了额外的dr.Read。

If dr2.HasRows Then
        ***dr2.Read()***
        Dim count As Integer = dr2.FieldCount
        While dr2.Read()
            For i As Integer = 0 To count - 1

删除额外的read()语句并在循环中获取字段计数。它应该工作正常。

If dr2.HasRows Then           
     While dr2.Read()
         Dim count As Integer = dr2.FieldCount
         For i As Integer = 0 To count - 1

更新 - 我正在放一个伪代码只是为了给你一个想法。还有很大的改进空间。但由于这不是代码审查,我将继续关注此事。

    query = "SELECT Booking_Date,Booking_Time FROM CustomerBooking where Booking_Date= <inputDate> & and Booking_Time=<inputTime>"
    dr2 = get datareader using <query>
    If dr2.HasRows Then
    'It means there are records in DB with input date and Time so show error             

    Else
       'code to insert'

    End If