Sunday, January 14, 2007

Typed Datasets and MS SQL Tables

This posting explains how to create a Typed Dataset using an XSD generated from a MS SQL database table.

Once you have the XSD that represents a database table, you can use the XSD.EXE command line tool to generate a strong Typed DataSet related classes with an appropriate namespace. The XSD.EXE file is shipped as part of the .NET Framework SDK. It is located in \Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\ folder.

  1. Create the XSD file out of the database table: There are two ways to generate the XSD. First is to programmatically use the SQLDataAdapter to get the table, and then use the FillSchema method to get the data as a System.Data.DataTable. Then use the .WriteXMLSchema method of the DataTable object to write the XSD for the table in memory. The second method is the easier method. Launch VS IDE. In the Server Explorer window pane create a Data Connection to your database. Once you have a connection, you need to create a blank XSD file. In the Server Explorer, drag-and-drop the table for which you need a XSD for, into the blank XSD file in VS.NET. Right-click in the XSD file, and View Code. You have the XSD created for you.
  2. Generate the strongly Typed DataSet classes: Here you will use the Microsoft XML Schema/Data Types support utility tool, i.e. xsd.exe to generate the Typed DataSet.
Go to the BIN folder of SDK and do the following:
xsd /d /l:cs [name of file].xsd /n:[your desired namespace name] /o:[output directory]
/d : Indicates to generate a DataSet
/l : Language. In this case CS is for C#
Path to the generated XSD file in previous steps
/n : namespace name

/o : output directory, excluding the name of file. Put the output directory in double-quotes.

Also for more information go to msdn site:
http://msdn2.microsoft.com/en-us/library/wha85tzb(VS.80).aspx

Good luck!


0 comments: