現在、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
ジェイソン、返信いただきありがとうございます。私は彼らの例を使って私のCSVファイルを使って作業してきました。私は実際に.Netオブジェクトに格納できる場所に持っています。オブジェクトに入れたら、データテーブルやデータセットにどのように転送できますか? – user1143550
を手動で入力してください。私は、filehelpersのデータテーブルに直接インポートするオプションがあると思っていました。 –
あなたはそうです。彼らにはクラスビルダーと呼ばれるものがあります。このリンクに行くとhttp://www.filehelpers.com/runtime_classes.htmlに例があります。区切られた例を使って、正しい数の列を追加する必要があることを確認した後、正しく機能するようにしました。このcsvファイルは第三者から来たので、もう一つの素敵な機能は上からの行を無視できるということでした。本当にありがとう! – user1143550