473,377 Members | 1,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,377 software developers and data experts.

Writing dataset to CSV file?

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
Nov 21 '05 #1
6 4249
On Tue, 14 Jun 2005 02:40:02 -0700, "dast" <da**@discussions.microsoft.com> wrote:

¤ 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.

I don't believe this will work since the Text ISAM driver does not have update capability.
Paul
~~~~
Microsoft MVP (Visual Basic)
Nov 21 '05 #2
Hi,

Dont see where you added the update and insert commands. Try
using a commandbuilder to create them. Never tried this with a csv

http://msdn.microsoft.com/library/de...classtopic.asp

Ken
----------------------
"dast" <da**@discussions.microsoft.com> wrote in message
news:E6**********************************@microsof t.com...
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
Nov 21 '05 #3
My CSV utility will write a DataTable out to a CSV file in roughly a
line or two of code. Just look at the WriteAll method of the CsvWriter
class.

http://www.geocities.com/shriop/index.html

Paul Clement wrote:
On Tue, 14 Jun 2005 02:40:02 -0700, "dast" <da**@discussions.microsoft.com> wrote:

¤ 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 thecsv
¤ 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.

I don't believe this will work since the Text ISAM driver does not have update capability.


Paul
~~~~
Microsoft MVP (Visual Basic)


Nov 21 '05 #4
Hello again,

When testing shriops demoversion I get this error on the WriteAll line:
"The process cannot access the file "c:\temp\data.csv" because it is being
used by another process."

Shouldn't closing the reader free the file for use again? Or is there some
way to go around this and make sure the file is closed properly?

Below is my code.

Dim reader As New CsvReader(path & "\" & file)
csvDataset.Tables.Add(reader.ReadToEnd(False))
csvDataset.Merge(otherDataset)
reader.Close()
reader.Dispose()

Dim writer As New CsvWriter(path & "\" & file)
writer.WriteAll(csvDataset.Tables.Item(0), False)
writer.Close()
writer.Dispose()

Best regards
/Daniel

"shriop" wrote:
My CSV utility will write a DataTable out to a CSV file in roughly a
line or two of code. Just look at the WriteAll method of the CsvWriter
class.

http://www.geocities.com/shriop/index.html


Nov 21 '05 #5
Yes, closing the reader should absolutely free it up for use again and
I've never ran into an issue in it not doing so. I would bet that
you've opened the file in Excel after writing it and still have Excel
open, or another program with that file opened in it. I created a file
called C:\test.csv with a couple of rows of data in it, then ran this
code over and over with no issues.

Module ErrorTest

Sub Main()
Dim Path As String = "c:"
Dim File As String = "test.csv"

Dim csvDataset As New DataSet
Dim otherDataset As New DataSet

Dim reader As New CsvReader(Path & "\" & File)
csvDataset.Tables.Add(reader.ReadToEnd(False))
csvDataset.Merge(otherDataset)
reader.Close()
reader.Dispose()

Dim writer As New CsvWriter(Path & "\" & File)
writer.WriteAll(csvDataset.Tables.Item(0), False)
writer.Close()
writer.Dispose()
End Sub

End Module

Nov 21 '05 #6
Mea Culpa.

I had another reader which I didn't close properly in some scenarios.
Now everything works fine.
Thanks for all help.

/Daniel
"shriop" wrote:
Yes, closing the reader should absolutely free it up for use again and
I've never ran into an issue in it not doing so. I would bet that
you've opened the file in Excel after writing it and still have Excel
open, or another program with that file opened in it. I created a file
called C:\test.csv with a couple of rows of data in it, then ran this
code over and over with no issues.

Module ErrorTest

Sub Main()
Dim Path As String = "c:"
Dim File As String = "test.csv"

Dim csvDataset As New DataSet
Dim otherDataset As New DataSet

Dim reader As New CsvReader(Path & "\" & File)
csvDataset.Tables.Add(reader.ReadToEnd(False))
csvDataset.Merge(otherDataset)
reader.Close()
reader.Dispose()

Dim writer As New CsvWriter(Path & "\" & File)
writer.WriteAll(csvDataset.Tables.Item(0), False)
writer.Close()
writer.Dispose()
End Sub

End Module

Nov 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Deepa | last post by:
I am writing a console app in c# wherein am converting a dataset into a CSV file. It works fine. But I have some values in the dataset which have a comma within(eg. A,B,C). When I view the CSV file...
4
by: Simon | last post by:
Hi all, I have a process, where I take a dataset from an SQL call, and need to write an XML file from that dataset. The data set can contain 10's of tables, each with 100's of rows, and I have...
3
by: theKirk | last post by:
using Visual Studio 2005 C# ASP.NET I know there has to be a simple way to do this....I want to use C# in a code behind for aspx. Populate a GridView from an xml file Add Fields to the...
19
by: Noozer | last post by:
I need to keep my application settings in a file that users can copy/backup/etc. Before I start using the old INI file standard, is there any easy way to use XML files to hold application...
5
by: UJ | last post by:
I have a system that has five programs that all communicate with each other via Message Queues. Works well. One program is a watchdog that will make sure the others are up and going. Currently I...
5
by: slinky | last post by:
I'm struggling with what should be a very basic .aspx/XML issue. I have an XML file. I have a textbox for a user to enter data and a button to submit the data. I don't wont the user to see the...
3
by: maw | last post by:
Hi, could somebody point me in the right direction for adding, removing and modifying nodes in an xml file programatically using vb.net (.net framework 2.0)? I have an xml file in the following...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.