2012-01-11 210 views
0

我目前正在處理涉及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." 

任何想法得到一個列表到數據集/數據表?

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 

回答

0

如果您只是試圖將csv文件解析爲.net對象,請嘗試http://www.filehelpers.com。這是將csv文件解析爲POCO或數據表的簡單方法。

+0

傑森,感謝您的答覆。我一直在用他們的例子來處理我的csv文件。實際上,我已經將它存儲在一個.Net對象中。現在我把它放到一個對象中,我怎樣才能將它轉換爲數據表或數據集?手動輸入 – user1143550 2012-01-11 19:14:54

+0

。儘管我認爲有一個選項可以直接導入到文件保護程序中的數據表。 – 2012-01-11 19:19:01

+0

你是對的。他們有一個叫做班級建造者的東西。去鏈接http://www.filehelpers.com/runtime_classes.html顯示一個例子。在確定我需要添加正確的列數後,我使用分隔的示例並正確地工作。另一個很好的功能是可以忽略來自頂端的行,因爲這個csv文件來自第三方。謝謝一堆! – user1143550 2012-01-11 19:40:13