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.

Comm<strong>and</strong>s ❘ 831<br />

This comm<strong>and</strong> accepts only a single parameter, as shown in the following code, which will execute the<br />

RegionDelete stored procedure; here, you see an example of setting the parameter by name. If you have<br />

many similar calls to make to the same stored procedure, constructing SqlParameter instances <strong>and</strong><br />

setting the values as in the following code may lead to better performance than reconstructing the entire<br />

SqlComm<strong>and</strong> for each call.<br />

cmd.Parameters["@RegionID"].Value= 999;<br />

cmd.ExecuteNonQuery();<br />

Calling a stored Procedure That returns output Parameters<br />

Both of the previous examples execute stored procedures that return nothing. If a stored procedure<br />

includes output parameters, these need to be defined within the .<strong>NET</strong> client so that they can be filled<br />

when the procedure returns. The following example shows how to insert a record into the database <strong>and</strong><br />

return the primary key of that record to the caller.<br />

Record Insertion<br />

The Region table consists of only a primary key (RegionID) <strong>and</strong> description field (RegionDescription). To<br />

insert a record, this numeric primary key must be generated, <strong>and</strong> then a new row needs to be inserted into<br />

the database. The primary key generation in this example has been simplified by creating one within the<br />

stored procedure. The method used is exceedingly crude, which is why there is a section on key generation<br />

later in this chapter. For now, this primitive example suffices:<br />

CREATE PROCEDURE RegionInsert(@RegionDescription NCHAR(50),<br />

@RegionID INTEGER OUTPUT)AS<br />

SET NOCOUNT OFF<br />

SELECT @RegionID = MAX(RegionID)+ 1<br />

FROM Region<br />

INSERT INTO Region(RegionID, RegionDescription)<br />

VALUES(@RegionID, @RegionDescription)<br />

GO<br />

code download StoredProcs.sql<br />

The insert procedure creates a new Region record. Because the primary key value is generated by the<br />

database itself, this value is returned as an output parameter from the procedure (@RegionID). This is<br />

sufficient for this simple example, but for a more complex table (especially one with default values), it<br />

is more common not to use output parameters, <strong>and</strong> instead select the entire inserted row <strong>and</strong> return this<br />

to the caller. The .<strong>NET</strong> classes can cope with either scenario.<br />

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

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

cmd.Parameters.Add(new SqlParameter("@RegionDescription",<br />

SqlDbType.NChar,<br />

50,<br />

"RegionDescription"));<br />

cmd.Parameters.Add(new SqlParameter("@RegionID",<br />

SqlDbType.Int,<br />

0,<br />

ParameterDirection.Output,<br />

false,<br />

0,<br />

0,<br />

"RegionID",<br />

DataRowVersion.Default,<br />

null));<br />

cmd.UpdatedRowSource = UpdateRowSource.OutputParameters;<br />

code download StoredProcs.cs<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!