本文介绍了Visual Basic如何从逗号分隔的文件而不是数据库添加一个Destinct Combobox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

delimited files what i need is to read from a comma-delimited file and retrieve ownley certain lines off them to a combobox, the combobox must then ownley show destinct names. I added a database-working code below of what im looking for but instaed of using a database i need the code for comma-delimeted files.

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

My comma delimited file will have the following lines for 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

我需要combobox显示每个名称,ownley一次

I need the combobox to show every name, ownley once

推荐答案

您可以使用和。

You can use a TextFieldParser and a HastSet.

这里有一个简单的例子:

Here's a simple example:

' 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()

这篇关于Visual Basic如何从逗号分隔的文件而不是数据库添加一个Destinct Combobox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 04:25