You have been assigned the task of writing code that executes an Entity SQL query that returns entity type objects that contain a property of a complex type.
(Line numbers are included for reference only.)
01 using (EntityCommand cmd = conn.CreateCommand())
02 {
03 cmd.CommandText = esqlQuery;
04 EntityParameter param = new EntityParameter();
05 param.ParameterName = “id”;
06 param.Value = 3;
07 cmd.Parameters.Add(param);
08 using (EntityDataReader rdr = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
09 {
10 while (rdr.Read())
11 {
12 …
13 Console.WriteLine(“Email and Phone Info:”);
14 for (int i = 0; i < nestedRecord.FieldCount; i++)
15 {
16 Console.WriteLine(” ” + nestedRecord.GetName(i) + “: ” + nestedRecord.GetValue(i));
17 }
18 }
19 }
20 }
Which code segment should you insert at line 12?
A.
DbDataRecord nestedRecord = rdr[“EmailPhoneComplexProperty”] as DbDataRecord;
B.
DbDataRecord nestedRecord = rdr[“EmailPhoneComplexProperty”]
C.
DataSet nestedRecord = rdr[“EmailPhoneComplexProperty”] as ComplexDataSet
D.
ComplexDataRecord nestedRecord = rdr[“EmailPhoneComplexProperty”]
Explanation:
How to: Execute a Query that Returns Complex Types
(http://msdn.microsoft.com/en-us/library/bb896329.aspx )using (EntityConnection conn = new EntityConnection(ConfigurationManager.ConnectionStrings[“StoreConnection”].ConnectionString))
{
using (EntityCommand comm = conn.CreateCommand())
{
// Here StoreConnection – ObjectContext name, Customers – correct DataSet name
comm.CommandText = “Select Customers.CustomerID, Customers.Name, Customers.Address from StoreConnection.Customers where Customers.CustomerID=@qqqCustomerID”;
EntityParameter param = new EntityParameter(“qqqCustomerID”, DbType.Int32);
param.Value = 1;
comm.Parameters.Add(param);
conn.Open();
var reader = comm.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read())
{
DbDataRecord record = reader[“Address”] as DbDataRecord;
for (int i = 0; i < record.FieldCount; i++)
{
name.Text += “<br/>” + record.GetName(i) + ” : ” + record.GetValue(i).ToString();
}
}
reader.Close();
}
}