15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XMl schemas: Generating Code with XsD ❘ 849<br />

In addition, for each nullable field, there are functions to set the field to null <strong>and</strong> to check if the field is<br />

null. The following example shows the functions for the SupplierID column:<br />

public partial class ProductRow : global::System.Data.DataRow {<br />

private ProductDataTable tableProduct;<br />

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

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

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

internal ProductRow(global::System.Data.DataRowBuilder rb) :<br />

base(rb) {<br />

this.tableProduct = ((ProductDataTable)(this.Table));<br />

}<br />

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

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

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

public int ProductID {<br />

get {<br />

return ((int)(this[this.tableProduct.ProductIDColumn]));<br />

}<br />

set {<br />

this[this.tableProduct.ProductIDColumn] = value;<br />

}<br />

}<br />

// Other column accessors/mutators removed for clarity<br />

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

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

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

public bool IsSupplierIDNull() {<br />

return this.IsNull(this.tableProduct.SupplierIDColumn);<br />

}<br />

}<br />

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

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

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

public void SetSupplierIDNull() {<br />

this[this.tableProduct.SupplierIDColumn] = global::System.Convert.DBNull;<br />

}<br />

code download product.cs<br />

The following code uses this class’s output from the XSD tool to retrieve data from the Products table <strong>and</strong><br />

display that data to the console:<br />

using System;<br />

using System.Data;<br />

using System.Data.SqlClient;<br />

public class XSD_DataSet<br />

{<br />

public static void Main()<br />

{<br />

string source = "server=(local);" +<br />

" integrated security=SSPI;" +<br />

"database=northwind";<br />

string select = "SELECT * FROM Products";<br />

SqlConnection conn = new SqlConnection(source);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!