2011-08-24 130 views
1

我想過濾我的結果相對於日期和時間....我有日期存儲在一個單獨的字段是datetime數據類型...另一個字段存儲時間值,但因爲它是一個日期時間數據類型,所以它存儲當前系統日期以及輸入的時間。現在,當我使用日期篩選結果時,它運行良好,並顯示特定時期之間的結果,例如2011年10月12日至2011年12月15日期間的日期...但是,當我想要顯示關於時間的結果時, t顯示任何結果...我想比較一個時間值和日期時間值

我正在使用dataadapter和數據集從sql server數據庫中檢索記錄...請告訴我比較從文本框中獲取的時間值與存儲在數據庫中的日期時間值的解決方案我得到這樣的下午12:00和下午6:00

更加清晰之間的具體時間之間的結果:

其實我有存儲在數據庫中的日期時間值。我從文本框中得到的是一個時間值。我想從數據庫中提取給定時間值之間的結果....爲了更加簡潔,我的應用程序是一個體育俱樂部的預訂系統,並提供一個選項來查看alreaady預訂。在這裏,我提供了兩個選項可以查看特定遊戲的所有預訂或篩選預訂。在過濾預訂中,有一個選項是過濾日期和時間...日期選項運行正常,但問題在於時間部分...我提供兩次但無法查看它們之間的預訂...

我的代碼是:

 Dim prmtimefrom As New SqlParameter("@booking_time", SqlDbType.DateTime) 
     prmtimefrom.Value = TextBox3.Text 

     Dim prmtimeto As New SqlParameter("@booking_tim", SqlDbType.DateTime) 
     prmtimeto.Value = TextBox4.Text 




     Dim da As New SqlDataAdapter("select * from Bookings where booking_time Between @booking_time AND @booking_tim AND game = " & x, con) ' x is the name of a specific game 

     da.SelectCommand.Parameters.Add(prmtimefrom) 
     da.SelectCommand.Parameters.Add(prmtimeto) 
     da.Fill(ds, "Bookings") 

回答

0

我不確定你是問如何做到這一點在vb.net,或如何從SQL數據庫中做到這一點。爲了在.NET中進行篩選,您需要使用DateTime.TimeOfDay屬性。一個簡單的例子是:

'Set the from time to start Jan 1, 2011 at 8:00am 
Dim startTime as DateTime = New DateTime(2011, 1, 1, 8, 0, 0) 

'Set the to time to end Feb 3, 2006 at 1:30pm 
Dim endTime as DateTime = New DateTime(2006, 2, 3, 13, 30, 0) 

'Compare the current time to the start/end times 
'Notice that the dates are ignored, we are only comparing times 
If Now.TimeOfDay >= startTime.TimeOfDay AndAlso Now.TimeOfDay <= endTime.TimeOfDay Then 
    MsgBox("Within time span") 
Else 
    MsgBox("Outside of time span") 
End If 
0

事情是這樣的:

Dim starting = TimeSpan.Parse("12:00") 
Dim ending = TimeSpan.Parse("18:00") 

Dim query = _ 
    From dr in dt.Rows.Cast(Of DataRow)() _ 
    Let tod = dr.Field(Of DateTime)(0).TimeOfDay _ 
    Where tod >= starting AndAlso tod < ending _ 
    Select dr