我有一个带有多选的列表框,它提供了多个值。我需要一个来自下面的选择语句
select Amount from tblEmployeeTransactions _
where PayrollCode_Code = '" & code & "' "
我的listboxdata如下所示
For Each drv As CListItem In lstnoncash.SelectedItems
code =drv.ItemData
Next
我想要的查询应该是
select sum(Amount) from tblEmployeeTransactions _
where PayrollCode_Code = '1' or PayrollCode_Code ='2' or PayrollCode_Code ='3'"
如果它有3行数据
答案 0 :(得分:0)
你可能想要这样的东西:
Dim a As String() = {"1", "2", "3"}
Dim query = "select sum(Amount) from tblEmployeeTransactions " _
& "where PayrollCode_Code IN "
'here inStatement will contain : ('1', '2', '3')'
Dim inStatement = "('" & String.Join("', '", a) & "')"
Console.WriteLine(query & inStatement)
答案 1 :(得分:0)
你PayrollCode_Code
是数字吗?如果是,那么您不需要为此添加单引号。我更喜欢使用IN
语句而不是多个OR
运算符。
Dim sCode As String
For Each drv As CListItem In lstnoncash.SelectedItems
sCode &= drv.ItemData & ","
Next
IF sCode.Length > 0 THEN
sCode = sCode.Substring(0,sCode.Length-1);
Dim _SQL As String = "Select SUM(Amount) FROM tblEmployeeTransactions " &
"WHERE PayrollCode_Code IN (" & sCode & ")"
''Execute ur _SQL
END IF