1

我正在使用 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

4

2 回答 2

2

您必须OR通过将条件放在括号内来对条件进行分组,(您没有得到不正确结果的原因是条件相互重叠

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')
于 2013-03-10T09:52:16.677 回答
0

不确定,但我需要发布作为答案,因为评论中的代码格式不太好。
让我知道它是否有效

com = new sqlcommand(SQLQUERY,con) 
Dim result = com.ExecuteScalar() 
if Convert.ToInt32(result) = 0 Then
    MessageBox.Show("Selected item is not available for that date and time.", ...)
    Exit Sub
End If

还有参数的问题。您需要传递参数以避免 SQL 注入,还需要确保像您这样的字符串被数据库引擎正确解释为查询所需的日期

这是使用参数而不是手工制作的字符串的查询的粗略翻译

SQLQUERY = "Select Count(*) as Conflicts from myTable " & _
     "Where @dateFrom  between(datefrom)and(dateto) and item = @item " & _
     "Or (datefrom = @dateFrom and timefrom between @timeFrom and @timeTo and item = @item) " & _
     "Or (datefrom = @dateFrom and   timeto between @timeFrom and @timeTo and item = @item) " & _
     "Or (datefrom = @dateFrom and @timeFrom between(timefrom)and(timeto)and item =@item)"

com = new sqlcommand(SQLQUERY,con) 
com.Parameters.AddWithValue("@dateFrom",  Convert.ToDateTime(dtpTFrom.Value))
com.Parameters.AddWithValue("@item",  cmbItems.Text)
com.Parameters.AddWithValue("@timeFrom",  Convert.ToDateTime(cmTFrom.Text))
com.Parameters.AddWithValue("@timeTo",  Convert.ToDateTime(cmTTo.Text))
于 2013-03-10T10:59:40.763 回答