2012-01-11 2 views
0

현재 CSV 파일이 포함 된 프로젝트에서 작업하고 있습니다. 처음에는 Microsoft.Jet.OLEDB.4.0을 사용하여 모든 것을 추출했습니다. 그러나 때로는 넘기지 않고 "숨긴"큰 숫자가 있습니다.문자열 목록을 데이터 집합 또는 데이터 테이블 중 하나로 변환

그래서 조사를 한 후에 모든 것을 목록에 채우는 예제를 발견 할 수있었습니다. 예 :

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

여기서 SteamReader를 사용하여 csv 파일을 가져 와서 저장할 수 있습니다. 처음 세 줄은 쓰레기이므로, 나는 그 줄을 건너 뛰고 쉼표로 구분 된 약 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

당신은 단순히 .NET 개체를 http://www.filehelpers.com 시도로 csv 파일을 구문 분석하려고합니다. csv 파일을 POCO 나 데이터 테이블로 파싱하는 간단한 방법입니다.

+0

제이슨, 답장을 보내 주셔서 감사합니다. 나는 그들의 예제를 사용하여 내 CSV 파일로 작업 해왔다. 나는 실제로 그것을 닷넷 객체에 저장할 수있는 곳을 가지고있다. 개체에 넣었으므로 어떻게 데이터 테이블이나 데이터 세트로 전송할 수 있습니까? – user1143550

+0

을 수동으로 입력하십시오. 비록 내가 filehelpers에서 datatable에 직접 가져 오는 옵션이 있다고 생각했지만. –

+0

네 말이 맞아. 그들은 클래스 빌더라고 불리는 것을 가지고 있습니다. 이 링크로 이동 http://www.filehelpers.com/runtime_classes.html 예제를 보여줍니다. 구분 된 예제를 사용하여 추가해야 할 정확한 열 수를 확보 한 후 올바르게 작동하게했습니다. 다른 멋진 기능은이 CSV 파일이 타사에서 온 것으로부터 맨 위의 행을 무시할 수 있다는 것입니다. 무리 감사! – user1143550

관련 문제