Vb.net Visual Basic如何从逗号分隔的文件而不是数据库中添加目标组合框?

Vb.net Visual Basic如何从逗号分隔的文件而不是数据库中添加目标组合框?,vb.net,combobox,Vb.net,Combobox,分隔文件我需要的是从一个逗号分隔的文件中读取数据,并将其中的某些行检索到组合框中,然后组合框必须显示目标名称。我在下面添加了一个数据库工作代码,这是我正在寻找的,但由于使用数据库,我需要逗号删除文件的代码 Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.

分隔文件我需要的是从一个逗号分隔的文件中读取数据,并将其中的某些行检索到组合框中,然后组合框必须显示目标名称。我在下面添加了一个数据库工作代码,这是我正在寻找的,但由于使用数据库,我需要逗号删除文件的代码

Imports System.Data.SqlClient
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call combo1()
    End Sub
    Sub combo1()
        Dim com As SqlConnection
        com = New SqlConnection("Server = Q-DESIGN\SQLEXPRESS; Database = Q-Design Test Results; Trusted_Connection = true;")
        com.Open()
        Dim command As SqlCommand = New SqlCommand("SELECT DISTINCT(Testname) from URL", com)
        Dim reader As SqlDataReader = command.ExecuteReader()
        While reader.Read()
            ComboBox1.Items.Add(reader.GetString(0))
        End While
        reader.Close()
        com.Dispose()
        com.Close()
    End Sub
对于egsample,我的逗号分隔文件将包含以下行

Jenny, 25, Female
Micheal, 100, Female
shaun, 50, male
Cindy, 75, Female
Cindy, 30, Female
Cindy, 20, Female
Micheal, 30, Female
deric, 50, Male
我需要组合框来显示每个名字,ownley once

您可以使用a和a

下面是一个简单的例子:

' a set to store the names
Dim names = new HashSet(Of String)
Using reader = New Microsoft.VisualBasic.FileIO.TextFieldParser("c:\your\path")

    reader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    reader.Delimiters = New String() {","}
    Dim currentRow As String()
    While Not reader.EndOfData
        Try
            ' read rows and add first field to set
            currentRow = reader.ReadFields()
            names.Add(currentRow(0).ToString())
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            ' do something
        End Try 
    End While 
End Using

' bind data to combo box (or use Items.Add instead)
comboBox.DataSource = names.ToList()

首先,我会一次读一行文件。然后,我将使用正则表达式来提取您要查找的名称字段。接下来,我会将每个项目添加到SortedSet

Dim strLine as String = ""
Dim RX as New Regex("^([^,]*),")
Dim MyMatch as Match
Dim NameLookup as New SortedSet(Of String)

'
' Code to read in each line into strLine using StreamReader.
'

MyMatch = RX.Match(strLine)

If MyMatch.Success AndAlso Not NameLookUp.Contains(MyMatch.Result("${1}")) Then
   NameLookup.Add(MyMatch.Result("${1}"))
End If
在此之后,将项目从SortedSet添加到ComboBox应该相当容易

此外,还可以手动创建带有“Name”列的DataTable并使用数据绑定
自动填充组合框。

您的代码有什么问题?使用数据库时,上面的代码工作正常,我需要的是一个替代代码,从逗号分隔的文件而不是数据库中获取数据谢谢您的帮助,如果我在desinct组合框中选择了其中一个项目,那么有没有办法在下一个组合框中自动填充所选项目行的ownley the next rows desinc data。我将把它作为一个新问题输入