C# .NET - Dataset

Asked By cool dude on 19-Mar-09 11:56 AM
How can we store a dataset in the sql server2005 database and fetch the database during run time in the application using c#.

Are there any options to store the xml representation of the dataset into the dataset and how to get the dataset back from the xml.
Vasanthakumar D replied to cool dude on 19-Mar-09 12:20 PM

Hi,

you can restore the dataset with XML using

Dataset ds = new Dataset();

ds.ReadXml("file name");

convert dataset to XML as

ds.WriteXml("file name");

 

to commite the dataset to database, use SqlBulkCopy


DataSet ds = new DataSet();
        //load ds here
        SqlBulkCopy bCopy = new SqlBulkCopy("Server=Server;Database=dbName;Trusted_Connection=True;",SqlBulkCopyOptions.TableLock);
        bCopy.DestinationTableName = "target table Name";
        bCopy.WriteToServer(ds.Tables[0]);

Yes

[)ia6l0 iii replied to cool dude on 19-Mar-09 12:22 PM

Ideally you wouldn't store a dataset into a table and read it from the table. That means you need to get the xml out of it and save. and reconstruct the dataset from the xml when you need it.

But if that is your requirement, then you can use the following approach.

To store a dataset in xml format, all you need to do is,

1. Create a method that accepts a dataset and returns you a xml representation of it. like,

public static XMLDocument GetXMLData(DataSet ds)

{

      //You could stop at getxml's string, or prepare a xmldoc so that it can be used by openxml

         string xmlData = ds.GetXml();

         XmlDocument xmldoc = new XmlDocument();

         xmldoc .LoadXml(xmlData);

         return xmldoc ;

}

2. Pass in as XML dbtype through your db classes.

   dbHelper.AddInParameter("@xmlData", xmlData.InnerXml, DbType.Xml);

3. In your sp, take a variable

@xmldoc NTEXT

and then say

DECLARE @xmld INT

EXEC SP_XML_PREPAREDOCUMENT @xmld OUTPUT, @xmldoc

4. Finally insert into your table.

INSERT INTO

[TableName]

SELECT

XMLValue = XMLDOC.XmlValueFROM

OPENXML(@xmld , 'Table/Column', NodePos)

WITH

( XmlValue datatype) XMLDOC

 


dataset

cool dude replied to [)ia6l0 iii on 19-Mar-09 12:33 PM
Hi ,

I got how to store the dataset in the table and now how can we retrive the dataset from the table during run time.
Do a select from the column
[)ia6l0 iii replied to cool dude on 19-Mar-09 12:44 PM
And then use DataSet.ReadXML method
Dataset
cool dude replied to [)ia6l0 iii on 19-Mar-09 01:05 PM
Hi,

When we get the xml representation of the dataset using getxml() method the table with zero rows are ommited and so that when we recreate the dataset i am not getting the those tables with zero rows..

Any alternatives for this.
 

Can you rephrase your question?
[)ia6l0 iii replied to cool dude on 20-Mar-09 01:58 AM
end of post
Vasanthakumar D replied to cool dude on 20-Mar-09 09:16 AM

Hi,

for this purpose, you need to write the schema of the datasset also in XML ...

try the below one..

ds.WriteXml("file Name", XmlWriteMode.WriteSchema);

the above code store the dataset into xml along with its schema. you can restore the dataset even though you have no datarows in it...