使用条件(第一个字符)从仅SQL项填充列表框

使用条件(第一个字符)从仅SQL项填充列表框,sql,vb.net,listbox,conditional-statements,Sql,Vb.net,Listbox,Conditional Statements,我已设法将所有项目放入列表框,并定义了第一个字符kto,如何仅将列表列中以该字符kto开头的值插入列表框 请注意,kto是从0到9的值,始终是一个数字 Dim SqlSb As New SqlConnectionStringBuilder() SqlSb.DataSource = ".\sqlexpress" SqlSb.InitialCatalog = "Konta" SqlSb.IntegratedSecurity = True Using

我已设法将所有项目放入
列表框
,并定义了第一个字符
kto
,如何仅将
列表
列中以该字符
kto
开头的值插入
列表框

请注意,
kto
是从0到9的值,始终是一个数字

        Dim SqlSb As New SqlConnectionStringBuilder()
    SqlSb.DataSource = ".\sqlexpress"
    SqlSb.InitialCatalog = "Konta"
    SqlSb.IntegratedSecurity = True

    Using SqlConn As SqlConnection = New SqlConnection(SqlSb.ConnectionString)
        SqlConn.Open()
        Dim cmd As SqlCommand = SqlConn.CreateCommand()
        cmd.CommandText = "SELECT List FROM Konta"
        Dim kto = Left(Label1.Text, 1)
        'Label3.Text = kto
        Using reader As SqlDataReader = cmd.ExecuteReader
            While (reader.Read())
                Me.ListBox1.Items.Add(reader("LIST"))
            End While

        End Using
        SqlConn.Close()

    End Using

在while循环中,在列表框中添加项目之前,请选中读卡器的日期类型(“列表”),并仅在与所需类型匹配时添加它

可以使用以下代码检查类型:

reader.GetFieldType(0)
试试这个

Dim SqlSb As New SqlConnectionStringBuilder()
SqlSb.DataSource = ".\sqlexpress"
SqlSb.InitialCatalog = "Konta"
SqlSb.IntegratedSecurity = True

Using SqlConn As SqlConnection = New SqlConnection(SqlSb.ConnectionString)
    SqlConn.Open()
    Dim cmd As SqlCommand = SqlConn.CreateCommand()
    Dim kto = Left(Label1.Text, 1)
    cmd.CommandText = "SELECT List FROM Konta WHERE List LIKE '" & kto.toString & "%'"
    ListBox1.Items.Clear
    Using reader As SqlDataReader = cmd.ExecuteReader
        While (reader.Read())
            Me.ListBox1.Items.Add(reader("LIST"))
        End While

    End Using
    SqlConn.Close()

End Using

非常感谢。但是在第一次加载这些值后,listbox不会重新加载。您可以在代码末尾使用
ListBox1.Refresh()
强制加载它……或者您的意思是这些值​​将第二次加载的值添加到第一次加载的值?中?。这是显而易见的。您正在使用
ListBox1.Items。添加
,而不清除旧值。如果是,您可以使用
ListBox1.Items。在加载值之前清除
,现在重新加载。非常感谢你们。