1

VB.NET と SQL SERVER 2005 プラットフォームを使用して最終試験プロジェクトに取り組んでいます。データベース内に特定の日付または時刻がある場合は、特定の日付または時刻を選択するときに競合を検出する必要があります。

私はすでにSQLサーバーに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 に答える