我正在使用 VB.NET 和 SQL SERVER 2005 平台完成我的期末考试项目。当我选择数据库中的特定日期或时间时,我需要检测冲突。
我已经在 sql server 中有我的 sql 查询,它运行良好。但是当我要在我的 vb.net 项目中执行它时,问题开始出现。我很难受。任何帮助将不胜感激。
我的表
Item DateFrom DateTo TimeFrom TimeTo
Diamond 3/10/2013 3/20/2013 NULL NULL
Cooler 3/10/2013 3/20/2013 NULL NULL
Cooler 3/21/2013 NULL 9:30:00 AM 11:00:00 AM
Diamond 3/21/2013 NULL 8:00:00 AM 9:30:00 AM
mysql
select count(*) from myTable
where '3/21/2013' between(datefrom)and(dateto) and item = 'Diamond'
or datefrom = '3/21/2013' and timefrom between '8:00 AM' and '10:00 AM' and item = 'Diamond'
or datefrom = '3/21/2013' and timeto between '8:00 AM' and '10:00 AM' and item = 'Diamond'
or datefrom = '3/21/2013' and '8:00 AM' between(timefrom)and(timeto) and item = 'Diamond'
结果:1
这是我的 vb.net 代码
SQLQUERY = "Select Count(*) as Conflicts from myTable" & _
"Where '" & dtpTFrom.Value & "' between(datefrom)and(dateto) and item = '" & cmbItems.Text & "'" & _
"Or (datefrom = '" & dtpTFrom.Value & "' and timefrom between '" & Format(CDate(cmTFrom.Text), "hh:mm tt") & "' and '" & Format(CDate(cmTTo.Text), "hh:mm tt") & "' and item = '" & cmbItems.Text & "')" & _
"Or (datefrom = '" & dtpTFrom.Value & "' and timeto between '" & Format(CDate(cmTFrom.Text), "hh:mm tt") & "' and '" & Format(CDate(cmTTo.Text), "hh:mm tt") & "' and item = '" & cmbItems.Text & "')" & _
" Or (datefrom = '" & dtpTFrom.Value & "' and '" & Format(CDate(cmTFrom.Text), "hh:mm tt") & "' between(timefrom)and(timeto) and item ='" & cmbItems.Text & "')"
com = new sqlcommand(SQLQUERY,con)
dr = com.ExecuteReader()
dr.Read()
If dr.HasRows Then
MsgBox(CInt(dr.GetValue(0)))
If CInt(dr.GetValue(0)) > 0 Then
MessageBox.Show("Selected item is not available for that date and time.", AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
万一
消息框结果:0