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.

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

Here, the definition of the parameters is much more complex. The second parameter, @RegionID, is defined<br />

to include its parameter direction, which in this example is Output. In addition to this flag, on the last line<br />

of the code, the UpdateRowSource enumeration is used to indicate that data will be returned from this<br />

stored procedure via output parameters. This flag is mainly used when issuing stored procedure calls from a<br />

DataTable (which is discussed later in this chapter).<br />

Calling this stored procedure is similar to the previous examples, except in this instance the output<br />

parameter is read after executing the procedure:<br />

cmd.Parameters["@RegionDescription"].Value = "South West";<br />

cmd.ExecuteNonQuery();<br />

int newRegionID = (int) cmd.Parameters["@RegionID"].Value;<br />

After executing the comm<strong>and</strong>, the value of the @RegionID parameter is read <strong>and</strong> cast to an integer. A<br />

shorth<strong>and</strong> version of the preceding is the ExecuteScalar() method, which will return (as an object) the<br />

first value returned from the stored procedure.<br />

You might be wondering what to do if the stored procedure you call returns output parameters <strong>and</strong> a set of<br />

rows. In this instance, define the parameters as appropriate, <strong>and</strong> rather than calling ExecuteNonQuery(),<br />

call one of the other methods (such as ExecuteReader()) that will permit you to traverse any record(s)<br />

returned.<br />

fasT daTa aCCess: The daTa reader<br />

A data reader is the simplest <strong>and</strong> fastest way of selecting some data from a data source, but it is also the<br />

least capable. You cannot directly instantiate a data reader object — an instance is returned from the<br />

appropriate database’s comm<strong>and</strong> object (such as SqlComm<strong>and</strong>) after having called the ExecuteReader()<br />

method.<br />

The following code demonstrates how to select data from the Customers table in the Northwind<br />

database. The example connects to the database, selects a number of records, loops through these selected<br />

records, <strong>and</strong> outputs them to the console.<br />

This example uses the OLE DB provider as a brief respite from the SQL provider. In most cases,<br />

the classes have a one-to-one correspondence with their SqlClient cousins; for example, there is the<br />

OleDbConnection object, which is similar to the SqlConnection object used in the previous examples.<br />

To execute comm<strong>and</strong>s against an OLE DB data source, the OleDbComm<strong>and</strong> class is used. The following<br />

code shows an example of executing a simple SQL statement <strong>and</strong> reading the records by returning an<br />

OleDbDataReader object.<br />

Note the second using directive, which makes the OleDb classes available:<br />

using System;<br />

using System.Data.OleDb;<br />

Most of the data providers currently available are shipped within the same assembly, so it is only necessary<br />

to reference the System.Data.dll assembly to import all classes used in this section.<br />

public class DataReaderExample<br />

{<br />

public static void Main(string[] args)<br />

{<br />

string source = "Provider=SQLOLEDB;" +<br />

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

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

"database=northwind";<br />

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

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

conn.Open();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!