This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".
Now you'll look at the Server Explorer option of XML Designer. Clicking on server Explorer launches Server Explorer (see figure 6-24).
Figure 6-24. Server Explorer
In figure 6-24, you see that you can expand a database connection and see its tables and views. You can drag these data objects (tables, views, stored procedures, columns) onto XML Designer. For this example, drag the Employee table onto the designer. After dragging, your XML Designer generates a schema for the table, which looks like figure 6-25.
Figure 6-25. XML Designer – generated schema
Listing 6-35 shows the generated XML code.
Listing 6-35. XML Schema generated for a database table
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="XMLSchema1" targetNamespace="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified"
xmlns="http://tempuri.org/XMLSchema1.xsd" xmlns:mstns="http://tempuri.org/XMLSchema1.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="bookstotre">
<xs:complexType>
<xs:sequence />
</xs:complexType>
</xs:element>
<xs:complexType name="book">
<xs:complexContent>
<xs:restriction base="booktype">
<xs:sequence>
<xs:element name="titleelement1" type="xs:string"></xs:element>
<xs:element name="author" type="authername"></xs:element>
<xs:element name="price" type="xs:decimal"></xs:element>
<xs:element name="categary" type="xs:string"></xs:element>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="author">
<xs:complexContent>
<xs:restriction base="authorName">
<xs:sequence>
<xs:element name="first-name" type="xs:string"></xs:element>
<xs:element name="last-name" type="xs:string" />
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<xs:element name="Document">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Employees">
<xs:complexType>
<xs:sequence>
<xs:element name="EmployeeID" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" />
<xs:element name="LastName" type="xs:string" />
<xs:element name="FirstName" type="xs:string" />
<xs:element name="Title" type="xs:string" minOccurs="0" />
<xs:element name="TitleOfCourtesy" type="xs:string" minOccurs="0" />
<xs:element name="BirthDate" type="xs:dateTime" minOccurs="0" />
<xs:element name="HireDate" type="xs:dateTime" minOccurs="0" />
<xs:element name="Address" type="xs:string" minOccurs="0" />
<xs:element name="City" type="xs:string" minOccurs="0" />
<xs:element name="Region" type="xs:string" minOccurs="0" />
<xs:element name="PostalCode" type="xs:string" minOccurs="0" />
<xs:element name="Country" type="xs:string" minOccurs="0" />
<xs:element name="HomePhone" type="xs:string" minOccurs="0" />
<xs:element name="Extension" type="xs:string" minOccurs="0" />
<xs:element name="Photo" type="xs:base64Binary" minOccurs="0" />
<xs:element name="Notes" type="xs:string" minOccurs="0" />
<xs:element name="ReportsTo" type="xs:int" minOccurs="0" />
<xs:element name="PhotoPath" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="DocumentKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:Employees" />
<xs:field xpath="mstns:EmployeeID" />
</xs:unique>
</xs:element>
</xs:schema>
Generating ADO.NET Typed DataSet from a Schema
There may be occasions when other applications will generate XML schemas and your application needs to use them to access databases. You can generate a typed dataset from an existing schema. But before generating Dataset option generates a typed DataSet for an XML schema. But before generating a DataSet you need to add schema to the project.
Adding an Existing schema to project
Now you'll see how you can generate a DataSet object from an existing schema. To test this. I created a new Windows application project. You can use the Employee table schema generated in the previous section. To add an existing schema to the project, right-click on the project and select Add > Add Existing Item and browse for the schema (see figure 6-26).
Figure 6-26. Adding an existing schema to a project
If your schema name was different, select that schema and click open (see figure 6-27).
Figure 6-27. Browsing for schema
This action adds a schema to the current project. You can also add an XML schema by dragging a database table onto XML Designer.
Generating a Typed Data Set from a schema
Generating a typed dataset from a schema is pretty simple. Right–click on XML Designer and select the Generate Dataset option (see figure 6-28).
Figure 6-28. Generate Data set option of XML Designer
This action generates a DataSet class and adds it to your project. If you look in your Class Wizard, you use the Document class derived from DataSet and its members. The Document class looks like figure 6-29 in the Class View.
Figure 6-29. DataSet-derived class in the Class View
Note: The Generate Data Set option may not generate a Data Set if the XML schema is not designed properly.
Once you've a DataSet object, you can use it the way you want.
Conclusion
Hope this article would have helped you in understanding working with DataSets. See other articles on the website also for further reference.
|
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET. |