Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
152 views
in Technique[技术] by (71.8m points)

.net - Importing a CSV to Dataset

I've done some googling about my issue but I can't seem to find anything that works. I have a .csv (it is formatted just like an excel sheet, with just strings) that I am trying to import into a dataset, but only one of the columns is importing successfully. Everything else is DBNull.

Here is my dataset:

dset.Tables.Add(New DataTable With {.TableName = "ImportedData"})
dset.Tables("ImportDataTable").Columns.Add("ID", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Name", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Type", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc1", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc2", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc3", GetType(String))
dset.Tables("ImportDataTable").Columns.Add("Misc4", GetType(String))

And here is my import code:

Dim ImportFileCSV As New OpenFileDialog
With ImportFileCSV 
    .Title = "Import Overview"
    .Filter = "CSV (*.csv)|*.csv"
End With

If ImportFileCSV .ShowDialog = DialogResult.OK Then
    Dim ImportPath As String = ImportDetailedOverview.FileName
    Dim ImportDirectoryPath As String = Path.GetDirectoryName(ImportPath) & ""
    Dim ImportFileName As String = Path.GetFileName(ImportPath)

    Using MyConnection As System.Data.OleDb.OleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=" & ImportDirectoryPath & ";Extended Properties=""Text;HDR=YES;""")
        Using MyCommand As System.Data.OleDb.OleDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " & ImportFileName, MyConnection)
            MyConnection.Open()
            MyCommand.Fill(dset.Tables("ImportedData"))
            MyConnection.Close()
        End Using
    End Using

Does anyone have any insight?





Edit:

Okay, so as per Ahmed, I am using the GenericParser: Below code to select the file via OpenFileDialog and then to add the returned table to dset.Tables("ImportDataTable")

  If ImportDetailedOverview.ShowDialog = DialogResult.OK Then
            Dim ImportPath As String = ImportDetailedOverview.FileName
            dset.Tables("ImportDataTable").Merge(ParseCSV(ImportPath))




I don't get any errors. It seems to finish successfully, but everything except Misc3 is blank. Through the power of binding sources, I am displaying that information to confirm:

            CType(fBindingSource.Current, DataRowView)("Name") = drow("Name").ToString
            MsgBox(drow("BuildingName").ToString)
            CType(fBindingSource.Current, DataRowView)("Type") = drow("Type").ToString
            CType(fBindingSource.Current, DataRowView)("Misc1") = drow("Misc1").ToString
            CType(fBindingSource.Current, DataRowView)("Misc2") = drow("Misc2").ToString
            CType(fBindingSource.Current, DataRowView)("Misc3") = drow("Misc3").ToString
            CType(fBindingSource.Current, DataRowView)("Misc4") = drow("Misc4").ToString

The CSV is very simple, it's an Excel CSV, so for example: col1 has and ID field (numeric only) col2 has a name field (string) so on and so forth.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a simple example of how to use GenericParser in VB.NET:

First, you need to import the namespace:

Imports GenericParsing

To return a DataTable:

Private Shared Function ParseCSV(filePath As String) As DataTable
    Using parser As New GenericParserAdapter(filePath)
        parser.FirstRowHasHeader = True
        Return parser.GetDataTable()
    End Using
End Function

To return a DataSet:

Private Shared Function ParseCSV(filePath As String) As DataSet
    Using parser As New GenericParserAdapter(filePath)
        parser.FirstRowHasHeader = True
        Return parser.GetDataSet()
    End Using
End Function

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...