OLEDB 命令中忽略的最后一个 'OR' 语句
Last 'OR' statement Ignored in OLEDB command
我试图通过在单个数据库字段上使用多个 'OR' 语句来过滤 OLEDB 命令,但链中的最后一个语句被忽略并且没有 return 任何东西。
我现在有一个解决方法,@null 请求 returns "DBNull.value" 但是如果我从下面删除“[doctype] = @null”,它会忽略“[doctype] =文档类型 3"
& "WHERE [doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] = @null " _
我可以根据需要增加或减少 'OR Statements',但总是忽略最后一个 'OR Statement'。
我试过将 'OR statements' 放在方括号中,但 return 什么都没有,或者我做错了。
我正在寻找为什么最后一个 'or statement' 在 oledbcommand 字符串中被忽略,但如果你能改进我写的任何代码,请做,但向我解释 why/how。
它们的使用位置(每个其他值都有 DBNull.value 用于测试目的)
f_doctype = "MS"
f_doctype2 = "TMS"
f_doctype3 = "CS"
With cmdaRefresh.Parameters
.AddWithValue("doctype", CType(f_doctype, String))
.AddWithValue("doctype2", CType(f_doctype2, String))
.AddWithValue("doctype3", CType(f_doctype3, String))
.AddWithValue("null", DBNull.Value)
.AddWithValue("docnum", DBNull.Value)
.AddWithValue("docrev", DBNull.Value)
.AddWithValue("matname", DBNull.Value)
.AddWithValue("status", DBNull.Value)
.AddWithValue("actionreq", DBNull.Value)
.AddWithValue("createdby", DBNull.Value)
.AddWithValue("createddate", DBNull.Value)
.AddWithValue("finalby", DBNull.Value)
.AddWithValue("finaldate", DBNull.Value)
End With
Dim cmdRefresh As New OleDbDataAdapter(cmdaRefresh)
'open connection
myconnection.Open()
'read and fill dataset for gridview
cmdRefresh.Fill(dsRefresh, tbl_string.tablename)
'close connection
myconnection.Close()
'fill datagrid with values from database and alter column headers to match criteria
With dg_speclog
.DataSource = dsRefresh.Tables(0)
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Category"
...
End With
完整'oledbcommand'创作
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE [doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] = @null " _
& "AND [docnum] = @docnum " _
& "AND [docrev] = @docrev " _
& "AND [matname] = @matname " _
& "AND [status] = @status " _
& "AND [actionreq] = @actionreq " _
& "AND [createdby] = @createdby " _
& "AND [createddate] = @createddate " _
& "AND [finalby] = @finalby " _
& "AND [finaldate] = @finaldate " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
您需要使用 IS NULL
来检查空值(在大多数(也许是所有)数据库中,空值不等于空值,因此您需要改用 IS
运算符)。这也意味着您不需要所有这些参数。
根据您打算如何将 [doctype] IS NULL
检查与其他空检查结合起来,您 可能 还需要将 OR
括在括号中, 因为 AND
的优先级可能高于 OR
.
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(
" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE ([doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] IS NULL) " _
& "AND [docnum] IS NULL " _
& "AND [docrev] IS NULL " _
& "AND [matname] IS NULL " _
& "AND [status] IS NULL " _
& "AND [actionreq] IS NULL " _
& "AND [createdby] IS NULL " _
& "AND [createddate] IS NULL " _
& "AND [finalby] IS NULL " _
& "AND [finaldate] IS NULL " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
如果[doctype] IS NULL
支票与其他NULL
支票属于同一支票,则您不需要括号。
已找到无法正常工作的原因。在我搜索的某个地方,我读到 DBNull.value
与 .addwithvalue
语句一起使用时会 return 数据库中的任何内容(这可能会混淆整个事情)。这是不正确的,它 return 是 no white space or value
中任何具有 nothing
值的东西。这就是为什么 [doctype] IS NULL
给出了与上面相同的问题,我认为它做同样的事情。
我一直在寻找一种方法来搜索我的 [doctype]
和 return 其他数据库字段中的任何值,而无需指定任何内容 'for testing'
我正在寻找的语句是 [docnum] LIKE '%'
这称为通配符。通配符可用于替换字符串中的任何其他字符,因此这意味着任何内容都是 returned。但是,如果数据库字段的值为 Null
,则仍然需要 IS NULL
语句,因此我在下面进行了 [finalyby] & [finaldate]
更改。
我现在想到的是:
'CHANGED (dataadapter > command)
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE [doctype] IN (@doctype, @doctype2 ,@doctype3) " _
& "AND [docnum] LIKE '%' " _
& "AND [docrev] LIKE '%' " _
& "AND [matname] LIKE '%' " _
& "AND [status] LIKE '%' " _
& "AND [actionreq] LIKE '%' " _
& "AND [createdby] LIKE '%' " _
& "AND [createddate] LIKE '%' " _
& "AND ([finalby] LIKE '%' OR [finalby] IS NULL) " _
& "AND ([finaldate] LIKE '%' OR [finaldate] IS NULL) " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
感谢帮助过的人
我试图通过在单个数据库字段上使用多个 'OR' 语句来过滤 OLEDB 命令,但链中的最后一个语句被忽略并且没有 return 任何东西。
我现在有一个解决方法,@null 请求 returns "DBNull.value" 但是如果我从下面删除“[doctype] = @null”,它会忽略“[doctype] =文档类型 3"
& "WHERE [doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] = @null " _
我可以根据需要增加或减少 'OR Statements',但总是忽略最后一个 'OR Statement'。
我试过将 'OR statements' 放在方括号中,但 return 什么都没有,或者我做错了。
我正在寻找为什么最后一个 'or statement' 在 oledbcommand 字符串中被忽略,但如果你能改进我写的任何代码,请做,但向我解释 why/how。
它们的使用位置(每个其他值都有 DBNull.value 用于测试目的)
f_doctype = "MS"
f_doctype2 = "TMS"
f_doctype3 = "CS"
With cmdaRefresh.Parameters
.AddWithValue("doctype", CType(f_doctype, String))
.AddWithValue("doctype2", CType(f_doctype2, String))
.AddWithValue("doctype3", CType(f_doctype3, String))
.AddWithValue("null", DBNull.Value)
.AddWithValue("docnum", DBNull.Value)
.AddWithValue("docrev", DBNull.Value)
.AddWithValue("matname", DBNull.Value)
.AddWithValue("status", DBNull.Value)
.AddWithValue("actionreq", DBNull.Value)
.AddWithValue("createdby", DBNull.Value)
.AddWithValue("createddate", DBNull.Value)
.AddWithValue("finalby", DBNull.Value)
.AddWithValue("finaldate", DBNull.Value)
End With
Dim cmdRefresh As New OleDbDataAdapter(cmdaRefresh)
'open connection
myconnection.Open()
'read and fill dataset for gridview
cmdRefresh.Fill(dsRefresh, tbl_string.tablename)
'close connection
myconnection.Close()
'fill datagrid with values from database and alter column headers to match criteria
With dg_speclog
.DataSource = dsRefresh.Tables(0)
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Category"
...
End With
完整'oledbcommand'创作
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE [doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] = @null " _
& "AND [docnum] = @docnum " _
& "AND [docrev] = @docrev " _
& "AND [matname] = @matname " _
& "AND [status] = @status " _
& "AND [actionreq] = @actionreq " _
& "AND [createdby] = @createdby " _
& "AND [createddate] = @createddate " _
& "AND [finalby] = @finalby " _
& "AND [finaldate] = @finaldate " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
您需要使用 IS NULL
来检查空值(在大多数(也许是所有)数据库中,空值不等于空值,因此您需要改用 IS
运算符)。这也意味着您不需要所有这些参数。
根据您打算如何将 [doctype] IS NULL
检查与其他空检查结合起来,您 可能 还需要将 OR
括在括号中, 因为 AND
的优先级可能高于 OR
.
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(
" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE ([doctype] = @doctype OR [doctype] = @doctype2 OR [doctype] = @doctype3 OR [doctype] IS NULL) " _
& "AND [docnum] IS NULL " _
& "AND [docrev] IS NULL " _
& "AND [matname] IS NULL " _
& "AND [status] IS NULL " _
& "AND [actionreq] IS NULL " _
& "AND [createdby] IS NULL " _
& "AND [createddate] IS NULL " _
& "AND [finalby] IS NULL " _
& "AND [finaldate] IS NULL " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
如果[doctype] IS NULL
支票与其他NULL
支票属于同一支票,则您不需要括号。
已找到无法正常工作的原因。在我搜索的某个地方,我读到 DBNull.value
与 .addwithvalue
语句一起使用时会 return 数据库中的任何内容(这可能会混淆整个事情)。这是不正确的,它 return 是 no white space or value
中任何具有 nothing
值的东西。这就是为什么 [doctype] IS NULL
给出了与上面相同的问题,我认为它做同样的事情。
我一直在寻找一种方法来搜索我的 [doctype]
和 return 其他数据库字段中的任何值,而无需指定任何内容 'for testing'
我正在寻找的语句是 [docnum] LIKE '%'
这称为通配符。通配符可用于替换字符串中的任何其他字符,因此这意味着任何内容都是 returned。但是,如果数据库字段的值为 Null
,则仍然需要 IS NULL
语句,因此我在下面进行了 [finalyby] & [finaldate]
更改。
我现在想到的是:
'CHANGED (dataadapter > command)
Dim cmdaRefresh As OleDbCommand = New OleDbCommand(" SELECT [doctype], [docnum], [docrev], [matname], [status], [actionreq], [createdby], [createddate], [finalby], [finaldate] " _
& "FROM " & tbl_string.tablename & " " _
& "WHERE [doctype] IN (@doctype, @doctype2 ,@doctype3) " _
& "AND [docnum] LIKE '%' " _
& "AND [docrev] LIKE '%' " _
& "AND [matname] LIKE '%' " _
& "AND [status] LIKE '%' " _
& "AND [actionreq] LIKE '%' " _
& "AND [createdby] LIKE '%' " _
& "AND [createddate] LIKE '%' " _
& "AND ([finalby] LIKE '%' OR [finalby] IS NULL) " _
& "AND ([finaldate] LIKE '%' OR [finaldate] IS NULL) " _
& "ORDER BY [docnum] ASC, [docrev] ASC " _
, myconnection)
感谢帮助过的人