G
Guest
Hello,
I’m having trouble writing the contents of a dataset to a csv file.
I have made a solution that loops through the rows in the dataset, but it’s
too slow. There must be a faster and easier way.
Here is a stripped down version of my failed attempt of writing without
looping through the dataset:
Private Sub WriteCSVFile(ByVal path As String, ByVal file As String, ByVal
dsNewMeasurements As DataSet)
'Connect to csv file
Dim csvConnection As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
";Extended Properties='text;FMT=Delimited;HDR=NO';")
Dim csvCommand As New OleDbCommand("SELECT * FROM " + file, csvConnection)
Dim csvAdapter As New OleDbDataAdapter(csvCommand)
csvConnection.Open()
'Read csv file, merge with new data and write back
Dim csvDataset As New DataSet
csvAdapter.Fill(csvDataset)
csvDataset.Merge(dsNewMeasurements)
Dim rowsUpdated As Integer = csvAdapter.Update(csvDataset)
csvConnection.Close()
End Sub
The two datasets seem to get merged (at least according to the row count in
the table), but rowsUpdated becomes zero, and no changes are made to the csv
file. How come this doesn’t work?
Oughtn’t there be some way to do this without having to read from the csv
file first?
I have fought with this for a while now, and searched the net for solutions.
I’ve seen hints that this is possible, but haven’t found any examples. I’d be
very thankful for any help on this.
Best regards
/Daniel
I’m having trouble writing the contents of a dataset to a csv file.
I have made a solution that loops through the rows in the dataset, but it’s
too slow. There must be a faster and easier way.
Here is a stripped down version of my failed attempt of writing without
looping through the dataset:
Private Sub WriteCSVFile(ByVal path As String, ByVal file As String, ByVal
dsNewMeasurements As DataSet)
'Connect to csv file
Dim csvConnection As New
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path +
";Extended Properties='text;FMT=Delimited;HDR=NO';")
Dim csvCommand As New OleDbCommand("SELECT * FROM " + file, csvConnection)
Dim csvAdapter As New OleDbDataAdapter(csvCommand)
csvConnection.Open()
'Read csv file, merge with new data and write back
Dim csvDataset As New DataSet
csvAdapter.Fill(csvDataset)
csvDataset.Merge(dsNewMeasurements)
Dim rowsUpdated As Integer = csvAdapter.Update(csvDataset)
csvConnection.Close()
End Sub
The two datasets seem to get merged (at least according to the row count in
the table), but rowsUpdated becomes zero, and no changes are made to the csv
file. How come this doesn’t work?
Oughtn’t there be some way to do this without having to read from the csv
file first?
I have fought with this for a while now, and searched the net for solutions.
I’ve seen hints that this is possible, but haven’t found any examples. I’d be
very thankful for any help on this.
Best regards
/Daniel