2014-11-21 42 views
1

需要一些幫助,我遇到的問題。 這是代碼:問題與選擇哪裏不是/存在子查詢(VB.NET,訪問)

Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged 
    startDateChanged = 1 

    If endDateChanged = 1 Then 
     cbLocation.Enabled = True 
     cbLocation.Items.Clear() 
     cbLocation.Items.Add(New ListViewItem("")) 
     Dim unbookedLocationsSQL As String = "SELECT locationID FROM Locations WHERE NOT EXISTS (Select LocationID FROM Bookings WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate)" 
     Dim unbookedLocationsCommand = New OleDbCommand(unbookedLocationsSQL, globalVariables.objConnection) 
     Dim unbookedLocationsAdapter As New OleDbDataAdapter(unbookedLocationsSQL, globalVariables.objConnection) 
     Dim unbookedLocationsDataSet As New DataSet 

     unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value) 
     unbookedLocationsCommand.Parameters.AddWithValue("endDate", dtpEndDate.Value) 

     unbookedLocationsAdapter.Fill(unbookedLocationsDataSet, "Locations") 

     With cbLocation 
      .DataSource = unbookedLocationsDataSet.Tables("Locations") 
      .DisplayMember = "locationID" 
      .ValueMember = "locationID" 
     End With 
    End If 
End Sub 

首先第一件事情,如果你改變了SQL語句「SELECT * FROM位置」組合框中顯示的只是所有的位置。

我想要達到的是這個;當某人更改了兩個日期時間選擇器時,將啓用組合框並填充未在這兩個日期之間預訂的位置列表(由預訂表確定)。 我知道SQL語句是錯誤的。我嘗試過各種各樣的組合,並嘗試隔離點和片斷,但我無法獲得任何子查詢來執行我想要的操作。

任何幫助,將不勝感激。

回答

0

這裏是代碼,現在它的工作:

Private Sub dtpStartDate_ValueChanged(sender As Object, e As EventArgs) Handles dtpStartDate.ValueChanged 
     startDateChanged = 1 

     If endDateChanged = 1 Then 
      cbLocation.Enabled = True 
      Me.Refresh() 
      cbLocation.Items.Add(New ListViewItem("")) 
      Dim unbookedLocationsSQL As String = "SELECT * FROM Locations WHERE LocationID NOT IN (SELECT LocationID FROM Bookings WHERE bookingEndDate >= @startDate AND bookingStartDate <= @endDate)" 
      Dim unbookedLocationsCommand = New OleDbCommand(unbookedLocationsSQL, globalVariables.objConnection) 
      Dim unbookedLocationsAdapter As New OleDbDataAdapter(unbookedLocationsSQL, globalVariables.objConnection) 
      Dim unbookedLocationsDataSet As New DataSet 

      unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value) 
      unbookedLocationsCommand.Parameters.AddWithValue("endDate", dtpEndDate.Value) 
      unbookedLocationsAdapter.SelectCommand = unbookedLocationsCommand 
      unbookedLocationsAdapter.Fill(unbookedLocationsDataSet, "Locations") 

      With cbLocation 
       .DataSource = unbookedLocationsDataSet.Tables("Locations") 
       .DisplayMember = "LocationName" 
       .ValueMember = "LocationID" 
      End With 
     End If 
    End Sub 
0

我覺得有什麼錯在這裏

WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate 

嘗試將其更改爲

WHERE bookingStartDate >= @startDate AND bookingEndDate <= @endDate 

也把心中把 「.Date」 在您的參數..

unbookedLocationsCommand.Parameters.AddWithValue("startDate", dtpStartDate.Value.Date) 
+0

你嘗試過隔離此查詢? 'Select LocationID FROM Bookings WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate)「' – Codemunkeee 2014-11-21 03:06:40

0

如果我理解正確,Bookings表中的每個條目都反映一個預訂,即使在某個位置。在這種情況下,你想:

WHERE @startDate >= bookingEndDate OR bookingStartDate >= @endDate 

,而不是

WHERE @startDate <= bookingEndDate AND bookingStartDate <= @endDate 

而且,如果Bookings表可以有同一地點的多個條目,這是不行的:你必須做出的肯定NONE該地點的預訂與輸入日期重疊。

由於我不能評論(還)其他答案,我會在這裏注意到,Codemunkeee的答案是錯誤的 - 使用他的查詢(WHERE bookingStartDate >= @startDate AND bookingEndDate <= @endDate),你將得到正好那些預訂與請求日期重疊的位置,所以它是與你想要的相反。當然,除非我誤解了Bookings表的內容。