从字典生成数据表

时间:2013-07-04 11:09:34

标签: .net vb.net data-structures dictionary datatable

我目前正在开发一个.NET WinForms应用程序,该应用程序读取相同格式的8个csv文件,并将每个CSV文件的数据存储在一个对象中,并提供修改/检索数据的方法。

CSV文件的每一行都包含有关政策的数据。

我正在使用的类/对象的结构是:

Policy
该类/对象存储与单个策略相关的所有数据,即CSV文件的一行。此类有一个名为polData的成员OrderedDictionarypolData将每个字段存储为字符串,并将字段标题作为键。

DataFile
该类/对象存储从csv文件读取的所有数据,即每个策略。此类有一个名为polCollection的成员OrderedDictionarypolCollection使用策略ID作为密钥存储所有策略对象。

对于这两个类,我使用了OrderedDictionary,因为我需要保留数据添加到字典中的顺序。

创建并填充这些对象后,我需要将其中一个输出到datagridview。因此,我想使用字典中的数据创建DataTable。请在下面找到我的方法。我的目标是有一种更有效的方法,所以我很感激任何反馈:

DataFile课程中,我有以下方法:

 Public Function toDataTable() As DataTable

    Dim dt As New DataTable
    Dim fieldHeader As String
    Dim i As Integer
    Dim pol As Policy

    '//add columns to datatable
    '//there are 68 columns. 
    '//the calcFieldHeader function returns the header string based on the header index
    For i = 0 To 67
        fieldHeader = tools.calcFieldHeader(i)
        dt.Columns.Add(fieldHeader)
    Next i

    '//loop through each policy in polCollection
    '//add policy rows to datatable
    For Each key In polCollection.keys
        pol = polCollection(key)
        dt.Rows.Add(pol.toArray)
    Next key

    Return dt

End Function

Policy课程中,我有以下方法:

 Public Function toArray() As String()
    Dim result(67) As String
    Dim inx As Integer
    inx = 0

    For Each key In polData.keys
        result(inx) = polData(key).ToString
        inx = inx + 1
    Next (key)

    Return result
End Function

1 个答案:

答案 0 :(得分:1)

您不需要使用词典。将CSV直接解析为数据表。

Private Function csvToDatatable(files As List(Of String), Optional skipxLines As Integer = 0) As DataTable

    Dim vbReader As Microsoft.VisualBasic.FileIO.TextFieldParser
    Dim dt As New DataTable

    'loop through the files from the list
    For Each filePath In files

        If File.Exists(filePath) Then

            'keep track of lineCount for each file
            Dim lineCount As Integer = 1

            'setup TextFieldParser for CSV
            vbReader = New Microsoft.VisualBasic.FileIO.TextFieldParser(filePath)
            vbReader.TextFieldType = FileIO.FieldType.Delimited
            vbReader.SetDelimiters(",")

            While Not vbReader.EndOfData

                Try
                    'read each line item
                    Dim currentRow = vbReader.ReadFields()

                    'Option skip x lines incase there are header records
                    If lineCount > skipxLines Then

                        'set generic columns using count of fields
                        If dt.Columns.Count = 0 Then
                            For i = 0 To UBound(currentRow)
                                dt.Columns.Add("Column" & i.ToString)
                            Next
                        Else

                            If UBound(currentRow) <> dt.Columns.Count - 1 Then
                                'check to make sure array of fields is the same size as data columns
                                Throw New Microsoft.VisualBasic.FileIO.MalformedLineException

                            End If

                        End If
                        'add row with data from currentRow array
                        dt.Rows.Add(currentRow)

                    End If

                Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                    MsgBox("Line " & lineCount & " in file " & filePath & " is not valid and will be skipped.")
                End Try

                lineCount += 1

            End While

        End If

    Next

    Return dt

End Function

使用此功能看起来像这样。

Dim fileList As New List(Of String)

fileList.Add("C:\exports\texting.csv")
fileList.Add("C:\exports\texting2.csv")

theGrid.DataSource = csvToDatatable(fileList, 1)
theGrid.DataBind()

在这个例子中,我假设存在标题记录。