Databases Reference
In-Depth Information
Now you have to specify the
reader
object, which will read the data stream from
the database and populate the ListBox control. You do so by using the
EntityDataReader
object, and then you also specify the
ExecuteReader
method to return the results. The
ExecuteReader
method also requires an enumeration value to be specified; for this
example, you use the
CommandBehavior.SequentialAccess
enumeration value to tell the
ADO.NET 3.5 runtime to retrieve and load the data sequentially and receive it in the
form of a stream.
EntityDataReader reader =
command.ExecuteReader(CommandBehavior.SequentialAccess);
Next, you specify the code to tell the reader that it has to add the data values in the
ListBox until the reader is able to read the data.
lstEmployees.Items.Clear();
while (reader.Read())
{
lstEmployees.Items.Add(reader["FirstName"] + " " + reader["LastName"]);
}
Try It Out: Schema Abstraction Using an Entity Data Model
In the previous exercise, you created an Entity Data Model named NorthwindModel; in
this exercise, you will see how this Entity Data Model will help developers achieve
schema abstraction and modify the database without touching the data access code
throughout the project or in the Data Access Layer (DAL).
1.
Start SQL Server Management Studio Express, expand the Database node, expand
the Northwind database node, and then expand the Tables node. In the list of
tables, expand the dbo.Employees node and then expand the
Columns
folder.
2.
Select the LastName column, right-click, and select the Rename option. Rename
the LastName column to EmployeesLastName.
3.
Select the FirstName column, right-click, and select the Rename option. Rename
the FirstName column to EmployeesFirstName.
4.
Now exit from SQL Server Management Studio Express by selecting File
ä
Exit.
5.
Switch to the Chapter20 solution and then run the EntityModel project. The
Employees Detail form should load. Click the Get Employees button; this raises
an exception window with the message “CommandExecutionException was
unhandled.” Click View Detail located under Actions.
Search WWH ::
Custom Search