15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

846 ❘ ChaPTer 30 cOre AdO.net<br />

}<br />

"System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]<br />

public Products() {<br />

this.BeginInit();<br />

this.InitClass();<br />

global::System.ComponentModel.CollectionChangeEventH<strong>and</strong>ler<br />

schemaChangedH<strong>and</strong>ler = new<br />

global::System.ComponentModel.CollectionChangeEventH<strong>and</strong>ler(<br />

this.SchemaChanged);<br />

base.Tables.CollectionChanged += schemaChangedH<strong>and</strong>ler;<br />

base.Relations.CollectionChanged += schemaChangedH<strong>and</strong>ler;<br />

this.EndInit();<br />

code download product.cs<br />

All private <strong>and</strong> protected members have been removed to concentrate on the public interface. The<br />

ProductDataTable <strong>and</strong> ProductRow definitions show the positions of two nested classes, which will<br />

be implemented next. You review the code for these classes after a brief explanation of the<br />

DataSet-derived class.<br />

The Products() constructor calls a private method, InitClass(), which constructs an instance of the<br />

DataTable-derived class ProductDataTable, <strong>and</strong> adds the table to the Tables collection of the DataSet<br />

class. The Products data table can be accessed by the following code:<br />

DataSet ds = new Products();<br />

DataTable products = ds.Tables["Products"];<br />

Or, more simply by using the property Product, available on the derived DataSet object:<br />

DataTable products = ds.Product;<br />

Because the Product property is strongly typed, you could naturally use ProductDataTable rather than the<br />

DataTable reference shown in the previous code.<br />

The ProductDataTable class includes far more code (note this is an abridged version of the code):<br />

[global::System.Serializable()]<br />

[global::System.Xml.Serialization.XmlSchemaProviderAttribute("GetTypedTableSchema")]<br />

public partial class ProductDataTable : global::System.Data.DataTable,<br />

global::System.Collections.IEnumerable {<br />

private global::System.Data.DataColumn columnProductID;<br />

private global::System.Data.DataColumn columnProductName;<br />

private global::System.Data.DataColumn columnSupplierID;<br />

private global::System.Data.DataColumn columnCategoryID;<br />

private global::System.Data.DataColumn columnQuantityPerUnit;<br />

private global::System.Data.DataColumn columnUnitPrice;<br />

private global::System.Data.DataColumn columnUnitsInStock;<br />

private global::System.Data.DataColumn columnUnitsOnOrder;<br />

private global::System.Data.DataColumn columnReorderLevel;<br />

private global::System.Data.DataColumn columnDiscontinued;<br />

[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]<br />

[global::System.CodeDom.Compiler.GeneratedCodeAttribute(<br />

"System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]<br />

public ProductDataTable() {<br />

this.TableName = "Product";<br />

this.BeginInit();<br />

this.InitClass();<br />

this.EndInit();<br />

}<br />

code download product.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!