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.

Persisting Dataset Changes ❘ 853<br />

New row updated <strong>and</strong> new RegionID assigned by database<br />

1 Eastern Unchanged<br />

2 Western Unchanged<br />

3 Northern Unchanged<br />

4 Southern Unchanged<br />

5 North West Unchanged<br />

Look at the last row in the DataTable. The RegionID had been set in code to 999, but after executing<br />

the RegionInsert stored procedure, the value has been changed to 5. This is intentional — the<br />

database will often generate primary keys for you, <strong>and</strong> the updated data in the DataTable appears<br />

because the SqlComm<strong>and</strong> definition within the source code has the UpdatedRowSource property set to<br />

UpdateRowSource.OutputParameters:<br />

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

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

aComm<strong>and</strong>.Parameters.Add(new SqlParameter("@RegionDescription",<br />

SqlDbType.NChar,<br />

50,<br />

"RegionDescription"));<br />

aComm<strong>and</strong>.Parameters.Add(new SqlParameter("@RegionID",<br />

SqlDbType.Int,<br />

0,<br />

ParameterDirection.Output,<br />

false,<br />

0,<br />

0,<br />

"RegionID", // Defines the SOURCE column<br />

DataRowVersion.Default,<br />

null));<br />

aComm<strong>and</strong>.UpdatedRowSource = UpdateRowSource.OutputParameters;<br />

code download DataAdapter2.cs<br />

What this means is that whenever a data adapter issues this comm<strong>and</strong>, the output parameters should be<br />

mapped to the source of the row, which in this instance was a row in a DataTable. The flag states what data<br />

should be updated — the stored procedure has an output parameter that is mapped to the DataRow. The<br />

column it applies to is RegionID because this is defined within the comm<strong>and</strong> definition.<br />

The following table shows the values for UpdateRowSource.<br />

uPdaTeroWsourCe Value<br />

Both<br />

FirstReturnedRecord<br />

None<br />

OutputParameters<br />

desCriPTion<br />

A stored procedure might return output parameters <strong>and</strong> also a complete<br />

database record. Both of these data sources are used to update the<br />

source row.<br />

This implies that the comm<strong>and</strong> returns a single record, <strong>and</strong> that the contents of<br />

that record should be merged into the original source DataRow. This is useful<br />

where a given table has a number of default (or computed) columns because<br />

after an INSERT statement these need to be synchronized with the DataRow<br />

on the client. An example might be ‘INSERT (columns) INTO (table) WITH<br />

(primarykey)’, then ‘SELECT (columns) FROM (table) WHERE (primarykey)’. The<br />

returned record would then be merged into the original row.<br />

All data returned from the comm<strong>and</strong> is discarded.<br />

Any output parameters from the comm<strong>and</strong> are mapped onto the appropriate<br />

column(s) in the DataRow.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!