刚开始尝试使用JOIN子句,这样我就可以在gridview中显示多个表中的数据。但是,我不能为我的生活弄清楚我的错误在这里。我试过切换调用的表的顺序,调用的列,这样就没有成功。我认为,这是最接近工作的我能得到的陈述。它基本上是一个所谓的工作声明的直接副本,但仍然无法正常工作。
感谢任何帮助。 (我使用VB.Net编写代码,使用Access' 13作为数据库(可能是问题的一部分;我已经阅读了它,并且对语法非常挑剔)并使用VS 2013。)
@tim:根据要求,这里是完整的子。
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
If cbSearchType.Text = "Guest Surname" Then
Dim searchSQL As String = "SELECT bookings.bookingStartDate, bookings.bookingEndDate, guests.guestFirstName, guests.guestSurname, locations.locationName FROM bookings JOIN guests ON guests.guestID = bookings.guestID JOIN locations ON locations.locationID = bookings.locationID WHERE guests.guestSurname = @guestSurname"
Dim searchCommand = New OleDbCommand(searchSQL, globalVariables.objConnection)
Dim searchAdapter As New OleDbDataAdapter(searchSQL, globalVariables.objConnection)
Dim searchDataTable As New DataTable
searchCommand.Parameters.AddWithValue("guestSurname", tbSearchTextBox.Text)
searchAdapter.SelectCommand = searchCommand
searchAdapter.Fill(searchDataTable)
globalVariables.objConnection.Open()
searchAdapter.Fill(searchDataTable)
gvSearchResults.DataSource = searchDataTable
globalVariables.objConnection.Close()
ElseIf cbSearchType.Text = "Location" Then
ElseIf cbSearchType.Text = "Booking Start Date" Then
ElseIf cbSearchType.Text = "Booking End Date" Then
End If
End Sub
结束班
答案 0 :(得分:1)
在Access中,您需要指定INNER JOIN
。当有多个连接时,您还需要括号。我会把它写成:
SELECT b.bookingStartDate, b.bookingEndDate, g.guestFirstName, g.guestSurname, l.locationName
FROM (bookings as b INNER JOIN
guests as g
ON g.guestID = b.guestID) INNER JOIN
locations as l
ON l.locationID = b.locationID
WHERE g.guestSurname = @guestSurname;