Exporting A DataSet - Recommendations?

D

David P. Donahue

I'm re-writing an application in C# that was originally written in
Delphi 7. The heart of the application is a DataSet displayed in a
DataGrid.

One of the main functions of the previous application was to save the
data to a file (it was XML, but the format is unimportant) that can be
re-loaded into the DataSet at a later time. (The reason for this was
that the queries which populate the DataSet can take a while to run, and
people wanted a way to just quickly load some "close enough to
up-to-date" data to do some quick work.)

Also, the DataSet (which consists of only one DataTable) could be
exported to a few other formats, such as comma-delimited or tab-delimited.

Can anyone recommend a good way of doing this? I Google around on the
subject, and find lots of articles explaining why exporting to XML is
good, but little explanation on ways of doing it. I did find something
that looked promising at
http://codebetter.com/blogs/brendan.tompkins/archive/2004/04/27/12229.aspx
but it seems to rely heavily on being a web application and I guess I'm
just not familiar enough to convert it to WinForms.

Does anyone have any recommendations for this? If I could just find
something that will Save/Load a DataSet to/from XML, and save a DataSet
to comma/tab-delimited, Excel, etc. that would be great. It doesn't
even all have to be the same tool.


Regards,
David P. Donahue
(e-mail address removed)
http://www.cyber0ne.com
 
D

das

As far as Exporting a DataSet to a XML file, simply use:

myDataSet.WriteXml(xmlWriter) //xmlWriter of type: StreamWriter

Vice versa for Reading XML document into a DataSet

myDataSet.ReadXml(xmlDocument) // You might have to read in the schema
for this xml

For Delimited files, first get a DataRow object containing all the data
in the table and loop throug to build a delimited file.

DataRow[] myDataRows = myDataSet.Tables[0].Select();
foreach(DataRow fRow in myDataRows)
{
......
}

May be there are better approaches, but this is what I have used in the
past.

Adios
 
S

sloan

David,

Using the "DataSet" model is a good design, especially with your "it takes a
while to fill it" issue.

Using the basic ReadXml() and WriteXml() are good ways to store the data.

Basically, I do it like this:

I write a wrapper method like this:

public DataSet GetGoodEnoughDataSet ( bool forceRefresh )
{

}

The logic inside the method goes like this:

Check to see if the file exists , where you're written the out the xml for
the DataSet
(exists==true)If it exists, read the xml, and return that object.
(exists==false)If it doesn't exist, get a fresh copy from the database, and
write the file's xml to the filename, and return the DataSet

If I pass in the "forceRefresh" as true, then do the (exists==false) logic.



Now, that's a good way to have a "file cached" version of the DataSet.

If you want an "In Memory" cache for a winforms.. then you can try this
post:
http://groups.google.com/group/micr...st&q=IDataStore&rnum=1&hl=en#f0a125e3bb2f4eb3

that post is a winforms equivalent of my blog posting: (below url)
http://sholliday.spaces.msn.com/PersonalSpace.aspx 10/24/2005 entry


The biggest issue you'll have is to figure out an intelligent (non hard
coded way ) to keep the file location.
C:\program files\myapplication\denormfiles\mydataset.xml
Make sure you do this part smartly, or you'll regret it.


You should also look at Strong Typed DataSets. These are just like DataSets
(they extend them), but you get better intellisense with them, and you
actually create tables in the DataSet you can refer to by name.


Here is the "behind the scenes" look at the strong typed dataset
If you go to your project and do a Add / New Item/ DataSet
give it the name TitlesDS
and then look at the "behind the scenes" xml, you can paste (to complete
replace the existing) lines with these.

Now, you have an "object" called TitlesDS in your project that you can work
with. And you still get the WriteXml() and ReadXml() methods.


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="TitlesDS" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:codegen="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="TitlesDS" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Titles">
<xs:complexType>
<xs:sequence>
<xs:element name="title_id" type="xs:string" minOccurs="0" />
<xs:element name="title" type="xs:string" minOccurs="0" />
<xs:element name="type" type="xs:string" minOccurs="0"
nillable="true" />
<xs:element name="pub_id" type="xs:string" minOccurs="0"
nillable="true" />
<xs:element name="pub_name" type="xs:string" minOccurs="0"
nillable="true" />
<xs:element name="price" type="xs:double" minOccurs="0"
nillable="true" codegen:nullValue="0" />
<xs:element name="advance" type="xs:double" minOccurs="0"
nillable="true" codegen:nullValue="0" />
<xs:element name="royalty" type="xs:string" minOccurs="0"
nillable="true" codegen:nullValue="0" />
<xs:element name="ytd_sales" type="xs:string" minOccurs="0"
nillable="true" codegen:nullValue="0" />
<xs:element name="notes" type="xs:string" minOccurs="0"
nillable="true" codegen:nullValue="" />
<xs:element name="pubdate" type="xs:dateTime" minOccurs="0"
nillable="true" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Publishers">
<xs:complexType>
<xs:sequence>
<xs:element name="pub_id" type="xs:string" minOccurs="0" />
<xs:element name="pub_name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="titlesDSKey1">
<xs:selector xpath=".//Titles" />
<xs:field xpath="title_id" />
</xs:key>
</xs:element>
</xs:schema>
 
A

Alvin Bruney [MVP]

better yet, squeeze some efficiency by using a datatable, it can transition
thru appdomains in .net 2.0

--
________________________
Warm regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Professional VSTO.NET - Wrox/Wiley
The O.W.C. Black Book with .NET
www.lulu.com/owc, Amazon
Blog: http://www.msmvps.com/blogs/alvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top