Arrays 将CSV文件读入多个数组

Arrays 将CSV文件读入多个数组,arrays,vb.net,Arrays,Vb.net,如果我想将以下内容从CSV文件读入4个单独的数组,我将如何继续? 例: 我需要记下学生的名字和姓氏。一年级是他们的期中考试,二年级是他们的期末考试 Dim Name As String = "" Dim inFile As StreamReader Dim outFile As StreamWriter inFile = File.OpenText("grades.csv") outFile = File.CreateText("report.txt")

如果我想将以下内容从CSV文件读入4个单独的数组,我将如何继续?
例:

我需要记下学生的名字和姓氏。一年级是他们的期中考试,二年级是他们的期末考试

    Dim Name As String = ""
    Dim inFile As StreamReader
    Dim outFile As StreamWriter

    inFile = File.OpenText("grades.csv")
    outFile = File.CreateText("report.txt")


    Do While (Not inFile.EndOfStream)
        Name = CStr(inFile.ReadToEnd)
        outFile.WriteLine(Name)
    Loop

    inFile.Close()
    outFile.Close()

我看到了一百万种不同的分割方式,我只输出文件来查看我得到了什么。有人能帮我把这些分开吗?谢谢你试试这个。。。。。一个可能给你一些想法的例子

Imports System.Data.OleDb

Public Class Form1

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim folder = "<Path to the folder of your grades.csv file (don't include the file name, just the path up to the folder)>"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    ' in the next line replace grades.csv with the name of your file....
    Using Adp As New OleDbDataAdapter("select F1 + ' ' + F2 as FirstSecondName, F3 as MidTerm, F4 as Final from [grades.csv] ", CnStr)
        Try
            Adp.Fill(dt)
        Catch ex As Exception

        End Try
    End Using
    Me.ListBox1.DataSource = dt
    ListBox1.DisplayMember = "FirstSecondName"
    ListBox1.ValueMember = "FirstSecondName"

    AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim senderListBox As ListBox = sender
    If senderListBox.SelectedIndex <> -1 Then
        Dim SelectedData As DataRowView = senderListBox.SelectedItem
        MessageBox.Show(String.Format("Selected - {0} :: Mid-Term Result = {1} :: Final Result = {2}", SelectedData("FirstSecondName").ToString, SelectedData("MidTerm").ToString, SelectedData("Final").ToString))
    End If
End Sub

End Class
导入System.Data.OleDb
公开课表格1
私有dt作为新数据表
私有子表单1_Load(发送方作为对象,e作为事件参数)处理MyBase.Load

Dim文件夹=“为什么要分别存储相关数据,尤其是名字和姓氏?我将把这些名字存储到一个列表框中。因此,当你点击时,你可以生成一份报告/查看他们的等级。正如@puropoix所指出的,将相关数据存储在不同的地方不是一个好主意。解决这类问题的典型方法是创建一个具有FirstName、LastName、MidtermGrade和FinalGrade属性的学生类。然后,您可以从CSV文件中的每一行创建一个学生对象,并将这些对象添加到
列表(学生)
。您可能希望在
列表视图
数据网格视图
中显示数据。以现代取代并行arrays@LamarZ别忘了选择一个答案。。。。
Imports System.Data.OleDb

Public Class Form1

Private dt As New DataTable

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim folder = "<Path to the folder of your grades.csv file (don't include the file name, just the path up to the folder)>"
    Dim CnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & folder & ";Extended Properties=""text;HDR=No;FMT=Delimited"";"
    ' in the next line replace grades.csv with the name of your file....
    Using Adp As New OleDbDataAdapter("select F1 + ' ' + F2 as FirstSecondName, F3 as MidTerm, F4 as Final from [grades.csv] ", CnStr)
        Try
            Adp.Fill(dt)
        Catch ex As Exception

        End Try
    End Using
    Me.ListBox1.DataSource = dt
    ListBox1.DisplayMember = "FirstSecondName"
    ListBox1.ValueMember = "FirstSecondName"

    AddHandler ListBox1.SelectedIndexChanged, AddressOf ListBox1_SelectedIndexChanged
End Sub

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim senderListBox As ListBox = sender
    If senderListBox.SelectedIndex <> -1 Then
        Dim SelectedData As DataRowView = senderListBox.SelectedItem
        MessageBox.Show(String.Format("Selected - {0} :: Mid-Term Result = {1} :: Final Result = {2}", SelectedData("FirstSecondName").ToString, SelectedData("MidTerm").ToString, SelectedData("Final").ToString))
    End If
End Sub

End Class