将字符串列表转换为数据集或数据表

时间:2012-01-11 16:34:28

标签: .net dataset arraylist

我目前正在处理涉及CSV文件的项目。我最初使用Microsoft.Jet.OLEDB.4.0来提取所有内容。但是,有大量的数字有时候无法完成并被“消隐”。

所以,在做了一些研究后,我找到了一个例子,我可以将所有内容都填入列表中。例如:

Dim lstExample As New List(Of String)().  

从这里我可以使用SteamReader来获取csv文件并存储它。由于前3行是垃圾,我正在跳过这些并读取所有其余行以及大约30列逗号分隔数据。

我现在的问题是我无法将lstExample放入数据集或数据表中。我甚至手动创建了一个列表,但仍然出错。

此行发生错误:

dataRow(i) = itemProperties(i).GetValue(item, Nothing) saying "Parameter count mismatch."

将List放入数据集/数据表的任何想法?

   Public Shared Sub ManualReadCSVFile(ByVal strFilePath As String)
    Dim lstExample As New List(Of String)()

    Using reader = New StreamReader("c:\SFTP_Target\ATL-536437.csv")
        For i As Integer = 0 To 2
            reader.ReadLine()
        Next

        While Not reader.EndOfStream
            Dim line = reader.ReadLine()
            Dim values = line.Split(",")

            lstExample.Add(values(0))

        End While

        reader.Dispose()
    End Using

    'Dim list As New List(Of String)(New String() {"nile", _
    '    "amazon", _
    '    "yangtze", _
    '    "mississippi", _
    '    "yellow"})

    Dim dsTest As New DataSet
    dsTest = CreateDataSet(lstExample)
    'dsTest = CreateDataset(list)

End Sub

Public Shared Function CreateDataSet(Of T)(ByVal list As List(Of T)) As DataSet
    'list is nothing or has nothing, return nothing (or add exception handling)
    If list Is Nothing OrElse list.Count = 0 Then
        Return Nothing
    End If

    'get the type of the first obj in the list
    Dim obj = list(0).[GetType]()

    'now grab all properties
    Dim properties = obj.GetProperties()

    'make sure the obj has properties, return nothing (or add exception handling)
    If properties.Length = 0 Then
        Return Nothing
    End If

    'it does so create the dataset and table
    Dim dataSet = New DataSet()
    Dim dataTable = New DataTable()

    'now build the columns from the properties
    Dim columns = New DataColumn(properties.Length - 1) {}
    For i As Integer = 0 To properties.Length - 1
        columns(i) = New DataColumn(properties(i).Name, properties(i).PropertyType)
    Next

    'add columns to table
    dataTable.Columns.AddRange(columns)

    'now add the list values to the table
    For Each item In list
        'For Each item As T In list
        'create a new row from table
        Dim dataRow = dataTable.NewRow()

        'now we have to iterate thru each property of the item and retrieve it's value for the corresponding row's cell
        Dim itemProperties = item.[GetType]().GetProperties()

        For i As Integer = 0 To itemProperties.Length - 1
            dataRow(i) = itemProperties(i).GetValue(item, Nothing)
        Next

        'now add the populated row to the table
        dataTable.Rows.Add(dataRow)
    Next

    'add table to dataset
    dataSet.Tables.Add(dataTable)

    'return dataset
    Return dataSet
End Function

1 个答案:

答案 0 :(得分:0)

如果您只是尝试将csv文件解析为.net对象,请尝试http://www.filehelpers.com。这是将csv文件解析为POCO或数据表的简单方法。