home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / SaveDataSetMapXSDSchema.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  8.5 KB  |  239 lines

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" -->
  4.  
  5. <h4>How Do I...Save DataSet mappings to an XSD schema file?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to save the internal DataSet mappings to an XSD schema file. In this sample the mappings are built by using the relational methods
  9. on the DataSet to create tables and columns and an XSD schema representation for these is then written out to a file.</div>
  10.  
  11. <h4>Saving DataSet mappings to an XSD schema file</h4> 
  12. <Acme:SourceRef
  13. ViewSource="/quickstart/howto/samples/Xml/SaveDataSetMapXSDSchema/SaveDataSetMapXSDSchema.src"
  14. RunSample="/quickstart/howto/samples/Xml/SaveDataSetMapXSDSchema/SaveDataSetMapXSDSchema.aspx" 
  15. Icon = "/quickstart/images/genicon.gif"
  16. Caption="SaveDataSetMapXSDSchema.aspx"
  17. runat="server" />
  18.  
  19. <br clear="left"><br>
  20. <div class="indent" style="width:660">
  21. The DataSet and XmlDataDocument classes both represent an in-memory data cache. The XmlDataDocument provides XML navigational and editing methods whilst the DataSet
  22. provides relationally orientated navigational and editing methods. Here we are going to get the DataSet property from the XmlDataDocument, use it to build a set of
  23. tables and columns and then populate them. We can then write out the internally generated schema. The code illustrated below builds up two tables, one of people
  24. and the other of pets. An ID is used as a primary key onto the each of the tables and a relationship table is built between the people and their pets.
  25. </div>
  26.  
  27. <br clear="left"><div class="indent" style="width:660">
  28. First we create an instance of an XmlDataDocument, and pass it's DataSet to the LoadDataSet() method.
  29. </div>
  30.  
  31. <div class="code"><xmp>
  32. XmlDataDocument datadoc = new XmlDataDocument();
  33. LoadDataSet(datadoc.DataSet);
  34. </xmp></div>
  35.  
  36. <div class="indent" style="width:660">
  37. The LoadDataSet() method loads the dataset with data relationally.
  38. </div>
  39.  
  40. <div class="code"><xmp>
  41. // Load a DataSet with relational data
  42. private void LoadDataSet(DataSet dataset)
  43. {
  44.     try
  45.     {
  46.         Console.WriteLine("Loading the DataSet ...");
  47.  
  48.         // Set DataSet name
  49.         dataset.DataSetName = "PersonPet";
  50.  
  51.         // Create tables for people and pets
  52.         DataTable people = new DataTable("Person");
  53.         DataTable pets = new DataTable("Pet");
  54.  
  55.         // Set up the columns in the Tables
  56.         DataColumn personname = new DataColumn ("Name", typeof(String));
  57.         DataColumn personAge = new DataColumn ("Age", typeof(Int32));
  58.  
  59.         DataColumn petname = new DataColumn ("Name", typeof(String));
  60.         DataColumn pettype = new DataColumn ("Type", typeof(String));
  61.  
  62.         // Add columns to person table
  63.         DataColumn id = people.Columns.Add("ID", typeof(Int32));
  64.         id.AutoIncrement = true;
  65.         people.PrimaryKey = new DataColumn[] {id};
  66.         people.Columns.Add (personname);
  67.         people.Columns.Add (personAge);
  68.  
  69.         // Add columns to pet table
  70.         id = pets.Columns.Add("ID", typeof(Int32));
  71.         id.AutoIncrement = true;
  72.         pets.PrimaryKey = new DataColumn[] {id};
  73.         id.AutoIncrement = true;
  74.         DataColumn ownerid = pets.Columns.Add("OwnerID", typeof(Int32));
  75.         DataColumn[] foreignkey = new DataColumn[] {ownerid};
  76.         pets.Columns.Add (petname);
  77.         pets.Columns.Add (pettype);
  78.  
  79.         // Add tables to the DataSet
  80.         dataset.Tables.Add (people);
  81.         dataset.Tables.Add (pets);
  82.  
  83.         // Add people
  84.         DataRow mark = people.NewRow();
  85.         mark[personname] = "Mark";
  86.         mark[personAge] = 18;
  87.         people.Rows.Add(mark);
  88.  
  89.         DataRow william = people.NewRow();
  90.         william[personname] = "William";
  91.         william[personAge] = 12;
  92.         people.Rows.Add(william);
  93.  
  94.         DataRow james = people.NewRow();
  95.         james[personname] = "James";
  96.         james[personAge] = 7;
  97.         people.Rows.Add(james);
  98.  
  99.         DataRow levi = people.NewRow();
  100.         levi[personname] = "Levi";
  101.         levi[personAge] = 4;
  102.         people.Rows.Add(levi);
  103.  
  104.         // Add relationships
  105.         Console.WriteLine("Creating relationships between people and pets ...");
  106.         DataRelation personpetrel = new DataRelation ("PersonPet",people.PrimaryKey, foreignkey);
  107.         dataset.Relations.Add (personpetrel);
  108.  
  109.         // Add pets
  110.         DataRow row = pets.NewRow();
  111.         row["OwnerID"] = mark["ID"];
  112.         row[petname] = "Frank";
  113.         row[pettype] = "cat";
  114.         pets.Rows.Add(row);
  115.  
  116.         row = pets.NewRow();
  117.         row["OwnerID"] = william["ID"];
  118.         row[petname] = "Rex";
  119.         row[pettype] = "dog";
  120.         pets.Rows.Add(row);
  121.  
  122.         row = pets.NewRow();
  123.         row["OwnerID"] = james["ID"];
  124.         row[petname] = "Cottontail";
  125.         row[pettype] = "rabbit";
  126.         pets.Rows.Add(row);
  127.  
  128.         row = pets.NewRow();
  129.         row["OwnerID"] = levi["ID"];
  130.         row[petname] = "Sid";
  131.         row[pettype] = "snake";
  132.         pets.Rows.Add(row);
  133.  
  134.         row = pets.NewRow();
  135.         row["OwnerID"] = levi["ID"];
  136.         row[petname] = "Tickles";
  137.         row[pettype] = "spider";
  138.         pets.Rows.Add(row);
  139.  
  140.         row = pets.NewRow();
  141.         row["OwnerID"] = william["ID"];
  142.         row[petname] = "Tweetie";
  143.         row[pettype] = "canary";
  144.         pets.Rows.Add(row);
  145.                 
  146.         // commit changes
  147.         dataset.AcceptChanges();
  148.     }
  149.  
  150.     catch (Exception e)
  151.     {
  152.         Console.WriteLine("Exception: ", e.ToString());
  153.     }
  154. }    
  155. </xmp></div>
  156.  
  157. <div class="indent" style="width:660">
  158. The DataSet <b>AcceptChanges</b> method commits all the changes that have been made to this DataSet since it was loaded or the last time AcceptChanges was called.
  159. All new and modified rows become unchanged, and deleted rows get removed. For more detail on the other DataSet relational methods see
  160. <A target=content href="/quickstart/howto/doc/adoplus/adoplusoverview.aspx">How do I...Get an Overview of ADO+?</A></div>
  161.  
  162. <br clear="left"><div class="indent" style="width:660">
  163. To save the schema to a file the DataSet <b>WriteXmlSchema</b> method is called passing a StreamWriter class that represents the destination file.
  164. </div>
  165.  
  166. <div class="code"><xmp>
  167. StreamWriter writer = new StreamWriter(m_Schema);
  168. datadoc.DataSet.WriteXmlSchema(writer);
  169. writer.Close();
  170. </xmp></div>
  171.  
  172. <div class="indent" style="width:660">
  173. The output from the DisplayTables() method for the tables built in the DataSet is shown below and the schema is written to the
  174. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/SaveDataSetMapXSDSchema/&file=PersonPet.xsd">PersonPet.xsd</a> file.
  175. </div>
  176.  
  177. <div class="code"><xmp>
  178. Loading the DataSet ...
  179. Creating relationships between people and pets ...
  180.  
  181. DataSet:
  182. PersonPet contains ...
  183. No of Tables: 2  Table content ...
  184.  
  185. TableName = Person
  186. ---------
  187. Columns ...
  188.  
  189. ID                    Name                  Age
  190. Number of rows = 4
  191. Rows ...
  192.  
  193. 0                     Mark                  18
  194. 1                     William               12
  195. 2                     James                 7
  196. 3                     Levi                  4
  197.  
  198. TableName = Pet
  199. ---------
  200. Columns ...
  201.  
  202. ID                    OwnerID               Name                  Type
  203. Number of rows = 6
  204. Rows ...
  205.  
  206. 0                     0                     Frank                 cat
  207. 1                     1                     Rex                   dog
  208. 2                     2                     Cottontail            rabbit
  209. 3                     3                     Sid                   snake
  210. 4                     3                     Tickles               spider
  211. 5                     1                     Tweetie               canary
  212.  
  213. PersonPet
  214.  
  215. Name = Mark owns
  216.         Pet = Frank the cat
  217.  
  218. Name = William owns
  219.         Pet = Rex the dog
  220.         Pet = Tweetie the canary
  221.  
  222. Name = James owns
  223.         Pet = Cottontail the rabbit
  224.  
  225. Name = Levi owns
  226.         Pet = Sid the snake
  227.         Pet = Tickles the spider
  228.  
  229. Writing the schema to PersonPet.xsd ...
  230. </xmp></div>
  231.  
  232. <H4>Summary</H4>
  233. <OL>
  234. <LI>The WriteXmlSchema method saves the mappings for the internal structure of the relation data in the DataSet as an XSD Schema.
  235. <LI>The XmlDataDocument has a DataSet property that enables you to view and manage structured data relationally within an XML document.
  236. </LI></OL>
  237.  
  238. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  239.