PrepAway - Latest Free Exam Questions & Answers

Which two actions should you perform?

You are developing an application that includes the following code segment. (Line numbers are included forreference only.)
01 class Animal
02 {
03 public string Color { get; set; }
04 public string Name { get; set; }
05 }
06 private static IEnumerable<Animal> GetAnimals(string sqlConnectionString)
07 {
08 var animals = new List<Animal>();
09 SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
10 using (sqlConnection)
11 {
12 SqlCommand sqlCommand = new SqlCommand(“SELECT Name, ColorName FROM
Animals”, sqlConnection);
13
14 using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader())
15 {
16
17 {
18 var animal = new Animal();
19 animal.Name = (string)sqlDataReader[“Name”];
20 animal.Color = (string)sqlDataReader[“ColorName”];
21 animals.Add(animal);
22 }
23 }
24 }
25 return animals;
26 }
The GetAnimals() method must meet the following requirements:
Connect to a Microsoft SQL Server database.
Create Animal objects and populate them with data from the database.
Return a sequence of populated Animal objects.
You need to meet the requirements. Which two actions should you perform? (Each correct answer presents
part of the solution. Choose two.)

PrepAway - Latest Free Exam Questions & Answers

A.
Insert the following code segment at line 16: while(sqlDataReader.NextResult())

B.
Insert the following code segment at line 13: sqlConnection.Open();

C.
Insert the following code segment at line 13: sqlConnection.BeginTransaction();

D.
Insert the following code segment at line 16: while(sqlDataReader.Read())

E.
Insert the following code segment at line 16: while(sqlDataReader.GetValues())

Explanation:
SqlConnection.Open – Opens a database connection with the property settings specified by the
ConnectionString. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open.aspx
SqlDataReader.Read – Advances the SqlDataReader to the next record. http://msdn.microsoft.com/en-us/
library/system.data.sqlclient.sqldatareader.read.aspx


Leave a Reply