Wednesday, December 08, 2004

How to create a strongly typed dataset from an existing dataset.

This is a little trick I used to convert any dataset to a strongly typed dataset so you can use the table and column names in intellisense.
(Examples in C#)

First we need to write the original dataset to an XML-file.

using System.Xml;
using System.Xml.Serialization;
using System.IO;

Create your dataset and fill it.

XmlSerializer ser = new XmlSerializer(typeof (DataSet));
XmlTextWriter wrt = new XmlTextWriter(@"filename.XML",null);
ser.Serialize (wrt, yourDataSet);

Run this code once to create an XML file of your dataset.
Then start the Visual Studio Command prompt.

Create the XSD schema file using the XSD.EXE tool.

xsd filename.xml

This creates the schema file filename.xsd.
Now use the same tool to create the class for the typed dataset;

xsd /d /l:CS filename.xsd

Add the created file filename.cs to your project.
Use this new class instead of the normal dataset.

No comments: