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.

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

}<br />

}<br />

SqlDataAdapter da = new SqlDataAdapter(select, conn);<br />

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

da.Fill(ds, "Product");<br />

foreach(Products.ProductRow row in ds.Product )<br />

Console.WriteLine("'{0}' from {1}",<br />

row.ProductID,<br />

row.ProductName);<br />

code download XSDDataSet.cs<br />

The output of the XSD file contains a class derived from DataSet, Products, which is created <strong>and</strong> then<br />

filled by the use of the data adapter. The foreach statement uses the strongly typed ProductRow <strong>and</strong> also<br />

the Product property, which returns the Product data table.<br />

To compile this example, issue the following comm<strong>and</strong>s:<br />

<strong>and</strong><br />

xsd product.xsd /d<br />

csc /recurse:*.cs<br />

The first generates the Products.cs file from the Products.XSD schema, <strong>and</strong> then the csc comm<strong>and</strong> uses<br />

the /recurse:*.cs parameter to go through all files with the extension .cs <strong>and</strong> add these to the resulting<br />

assembly.<br />

PoPulaTing a daTaseT<br />

After you have defined the schema of your data set, replete with DataTable, DataColumn, <strong>and</strong> Constraint<br />

classes, <strong>and</strong> whatever else is necessary, you need to be able to populate the DataSet class with some<br />

information. You have two main ways to read data from an external source <strong>and</strong> insert it into the<br />

DataSet class:<br />

➤<br />

➤<br />

Use a data adapter.<br />

Read XML into the DataSet class.<br />

Populating a dataset Class with a data adapter<br />

The section on data rows briefly introduced the SqlDataAdapter class, as shown in the following code:<br />

string select = "SELECT ContactName,CompanyName FROM Customers";<br />

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

SqlDataAdapter da = new SqlDataAdapter(select, conn);<br />

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

da.Fill(ds, "Customers");<br />

The bold line shows the SqlDataAdapter class in use; the other data adapter classes are again virtually<br />

identical in functionality to the Sql equivalent.<br />

To retrieve data into a DataSet, it is necessary to have some form of comm<strong>and</strong> that is executed to select that<br />

data. The comm<strong>and</strong> in question could be a SQL SELECT statement, a call to a stored procedure, or for the<br />

OLE DB provider, a TableDirect comm<strong>and</strong>. The preceding example uses one of the constructors available<br />

on SqlDataAdapter that converts the passed SQL SELECT statement into a SqlComm<strong>and</strong>, <strong>and</strong> issues this<br />

when the Fill() method is called on the adapter.<br />

In the stored procedures example earlier in this chapter, the INSERT, UPDATE, <strong>and</strong> DELETE procedures were<br />

defined, but the SELECT procedure was not. That gap is filled in the next section, which also shows how to<br />

call a stored procedure from a SqlDataAdapter class to populate data in a DataSet class.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!