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.

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

The < p r o v i d e r > C omm<strong>and</strong> classes have a property called Comm<strong>and</strong>Type , which is used to defi ne whether the<br />

comm<strong>and</strong> is a SQL clause, a call to a stored procedure, or a full table statement (which simply selects all<br />

columns <strong>and</strong> rows from a given table). The following table summarizes the Comm<strong>and</strong>Type enumeration.<br />

Comm<strong>and</strong>TyPe<br />

Text (default)<br />

StoredProcedure<br />

TableDirect<br />

eXamPle<br />

String select = “SELECT ContactName FROM Customers”;SqlComm<strong>and</strong><br />

cmd = new SqlComm<strong>and</strong>(select, conn);<br />

SqlComm<strong>and</strong> cmd = new SqlComm<strong>and</strong>(“CustOrderHist”, conn);cmd<br />

.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;cmd.Parameters<br />

.AddWithValue(“@CustomerID”, “QUICK”);<br />

OleDbComm<strong>and</strong> cmd = new OleDbComm<strong>and</strong>(“Categories”, conn);cmd<br />

.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.TableDirect;<br />

When executing a stored procedure, it might be necessary to pass parameters to that procedure. The<br />

previous example sets the @CustomerID parameter directly, although there are other ways of setting<br />

the parameter value, which you look at later in this chapter. Note that since .<strong>NET</strong> 2.0, the AddWithValue()<br />

method is included in the comm<strong>and</strong> parameters collection — <strong>and</strong> the Add(name, value) member was<br />

attributed as Obsolete . If you have used this original method of constructing parameters for calling a<br />

stored procedure, you will receive compiler warnings when you recompile your code. We suggest altering<br />

your code now because Microsoft will most likely remove the older method in a subsequent release of .<strong>NET</strong>.<br />

The TableDirect comm<strong>and</strong> type is valid only for the OleDb provider; other providers<br />

will throw an exception if you attempt to use this comm<strong>and</strong> type with them.<br />

executing Comm<strong>and</strong>s<br />

After you have defi ned the comm<strong>and</strong>, you need to execute it. A number of ways exist to issue the statement,<br />

depending on what you expect to be returned (if anything) from that comm<strong>and</strong>. The C o m m a n d<br />

classes provide the following execute methods:<br />

➤<br />

➤<br />

➤<br />

ExecuteNonQuery() — Executes the comm<strong>and</strong> but does not return any output<br />

ExecuteReader() — Executes the comm<strong>and</strong> <strong>and</strong> returns a typed IDataReader<br />

ExecuteScalar() — Executes the comm<strong>and</strong> <strong>and</strong> returns the value from the fi rst column of the fi rst<br />

row of any result set<br />

In addition to these methods, the SqlComm<strong>and</strong> class exposes the following method:<br />

➤<br />

executenonQuery()<br />

ExecuteXmlReader() — Executes the comm<strong>and</strong> <strong>and</strong> returns an XmlReader object, which can be<br />

used to traverse the XML fragment returned from the database<br />

The ExecuteNonQuery() method is commonly used for UPDATE , INSERT , or DELETE statements, where the<br />

only returned value is the number of records affected. This method can, however, return results if you call a<br />

stored procedure that has output parameters:<br />

using System;<br />

using System.Data.SqlClient;<br />

public class ExecuteNonQueryExample<br />

{<br />

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

{<br />

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

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

"database=Northwind";<br />

string select = "UPDATE Customers " +<br />

"SET ContactName = 'Bob' " +<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!