0

分隔文件我需要的是从逗号分隔文件中读取并从它们中检索ownley某些行到组合框,然后组合框必须ownley显示特定名称。我在我正在寻找的内容下方添加了一个数据库工作代码,但在使用数据库时,我需要逗号分隔文件的代码。

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 一次

4

2 回答 2

2

您可以使用 aTextFieldParser和 a HastSet

这是一个简单的例子:

' 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()
于 2013-08-27T15:03:44.707 回答
0

首先,我会一次读取一行文件。然后,我将使用正则表达式来提取您要查找的名称字段。接下来,我会将每个项目添加到 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 应该相当容易。

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

于 2013-08-27T15:21:45.573 回答