CheckedListBox项目为SQL Select语句

时间:2013-06-17 20:50:03

标签: sql vb.net

我正在尝试根据Checked List Box中的用户输入执行SQL Select查询。我能够找到一些代码,但解决方案似乎没有用。在调试时,它继续在第3行开始,我不知道为什么。

任何帮助都会感激不尽。

Dim DistrictString As StringBuilder = New StringBuilder()
    For h = 0 To Me.District.CheckedItems.Count - 1
        If Me.District.CheckedItems(h).Selected Then
            DistrictString.Append(Me.District.CheckedItems(h).Value & ",")
        End If
    Next

根据一些建议,这里是新编辑的代码版本,它现在在Append行崩溃。

新错误是“未找到”字符串'''上的'公共成员'值'

Dim DistrictW As String
Dim DistrictX As String
Dim DistrictString As StringBuilder = New StringBuilder
    For h = 0 To Me.District.CheckedItems.Count - 1
        DistrictString.Append(Me.District.CheckedItems(h).Value & ",")
    Next
DistrictW = DistrictString.ToString
DistrictX = DistrictW.Substring(0, DistrictString.Length - 1)

3 个答案:

答案 0 :(得分:0)

If Me.District.CheckedItems.Items(h).Selected Then

     DistrictString.Append(Me.District.CheckedItems.Items(h).Value & ",")

End If

使用CheckboxList.Items

答案 1 :(得分:0)

由于您正在创建类似...xyz in ('a', 'b', 'c', 'd')...的表达式,因此以下代码可能更合适:

  

对于h = 0 To Me.District.CheckedItems.Count - 1

       DistrictString += "'" & Me.District.CheckedItems(h).Value & "',"
     

下一步

     

DistrictString = DistrictString.Substring(0,DistrictString.Length - 1)

根本没有弄乱.SELECTED属性。为您的目的选择的所有内容都是“选中”。

答案 2 :(得分:0)

感谢所有帮助过的人,这是我实施的解决方案

从这个小练习中学到很多东西。

Dim DistrictW As String
Dim DistrictX As String
Dim DistrictString As StringBuilder = New StringBuilder
    For h = 0 To Me.District.CheckedItems.Count - 1
        DistrictString.Append(Me.District.CheckedItems(h).ToString & ",")
    Next
DistrictW = DistrictString.ToString
DistrictX = DistrictW.Substring(0, DistrictString.Length - 1)