使用Access数据库

时间:2017-06-03 12:19:29

标签: vb.net button

我正在尝试建立一个酒店预订系统。然而,可用性让我有点困惑。我有大约15个按钮,我可以将数字保存到数据库,但表单加载/日期更改。我需要按钮保持红色并且不可点击。例如,如果我从3/06/175/06/17预订了11个房间,那么我需要按钮保持红色从3/06/174/06/17,因为房间能够清洁后仍在5/06/17预订。我希望这是有道理的。下面是我用来尝试执行此操作的代码。代码确实运行,但按钮不会变为红色。

我在想我的SQL语句是否需要更改,但我不太确定。我对编码很新,所以解释会有所帮助。感谢。

Private Sub ReadRecords()
            Dim btn As Button = Nothing
            Dim BookingFound As Boolean = False
            Using MyConn As New OleDbConnection
                MyConn.ConnectionString = connString
                MyConn.Open()
                Dim check As String = "SELECT COUNT(*) FROM [BookingInformation] WHERE [Date In] = '" & dtpDateIn.Value.Date & "' AND [Date Out] = '" & dtpDateOut.Value.Date & "'"
                Dim BookingExists As Boolean = False
                Dim command As OleDbCommand = New OleDbCommand(check, MyConn)
                Using reader As OleDbDataReader = command.ExecuteReader()
                    While reader.Read()
                        If reader(0) = 0 Then
                            BookingExists = False
                        Else
                            BookingExists = True
                        End If
                    End While
                End Using
                If BookingExists = True Then
                    Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = '" & dtpDateOut.Text & "'"
                    Dim command2 As OleDbCommand = New OleDbCommand(getData, MyConn)
                    Using reader As OleDbDataReader = command2.ExecuteReader()
                        While reader.Read()
                            BookingFound = True
                            strDateIn = reader("Date In").ToString()
                            strDateOut = reader("DateOut").ToString
                            strRoomNumber = reader("Room Number").ToString
                        End While
                        If BookingFound = True Then
                            btn.BackColor = Color.Red
                        End If
                    End Using
                End If
                MyConn.Close()
            End Using
        End Sub

         Private Sub Room_Booking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ReadRecords()
        End Sub

1 个答案:

答案 0 :(得分:0)

您应该使您的访问数据库将输入理解为日期,访问数据库对数据类型非常敏感,例如,如果您编写

 "SELECT * FROM [user_tb] WHERE user_id=1"

如果您的user_id数据类型是自动编号,您的代码将正常工作。 所以试试

 Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = #" & dtpDateOut.Text & "#"

而不是

 Dim getData As String = "SELECT * FROM [BookingInformation] WHERE [Date Out] = '" & dtpDateOut.Text & "'"

用#替换'