Which code segment should you insert at line 06?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. You create a DataSet object in the application.
You add two DataTable objects named App_Products and App_Categories to the DataSet.
You add the following code segment to populate the DataSet object.
(Line numbers are included for reference only.)

01 public void Fill(SqlConnection cnx, DataSet ds)
02 {
03 var cmd = cnx.CreateCommand();
04 cmd.CommandText = “SELECT * FROM dbo.Products; ” + “SELECT * FROM dbo.Categories”;
05 var adapter = new SqlDataAdapter(cmd);
06 …
07 }

You need to ensure that App_Products and App_Categories are populated from the dbo.Products and dbo.Categories database tables.
Which code segment should you insert at line 06?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
The application connects to a Microsoft SQL Server database. You create a DataSet object in the application.
You add two DataTable objects named App_Products and App_Categories to the DataSet.
You add the following code segment to populate the DataSet object.
(Line numbers are included for reference only.)

01 public void Fill(SqlConnection cnx, DataSet ds)
02 {
03 var cmd = cnx.CreateCommand();
04 cmd.CommandText = “SELECT * FROM dbo.Products; ” + “SELECT * FROM dbo.Categories”;
05 var adapter = new SqlDataAdapter(cmd);
06 …
07 }

You need to ensure that App_Products and App_Categories are populated from the dbo.Products and dbo.Categories database tables.
Which code segment should you insert at line 06?

A.
adapter.Fill(ds, “Products”);
adapter.Fill(ds, “Categories”);

B.
adapter.Fill(ds.Tables[“App_Products”]);
adapter.Fill(ds.Tables[“App_Categories”]);

C.
adapter.TableMappings.Add(“Table”, “App_Products”);
adapter.TableMappings.Add(“Table1”, “App_Categories”);
adapter.Fill(ds);

D.
adapter.TableMappings.Add(“Products”, “App_Products”);
adapter.TableMappings.Add(“Categories”, “App_Categories”);
adapter.Fill(ds);

Explanation:
Table Mapping in ADO.NET
(http://msdn.microsoft.com/en-us/library/ms810286.aspx)



Leave a Reply 8

Your email address will not be published. Required fields are marked *


Lalex

Lalex

Yes, C is correct.

Anderson

Anderson

I agree. It should be “C”.

Andi

Andi

C is the correct one and you can test

public void PreFill()
{
var strConn = new SqlConnectionStringBuilder();
strConn.DataSource = “localhost”;
strConn.InitialCatalog = “NORTHWIND”;
strConn.IntegratedSecurity = true;

var ds = new DataSet(“Northwind”);
var app_Products = new DataTable(“App_Products”);
var app_Categories = new DataTable(“App_Categories”);
ds.Tables.Add(app_Categories);
ds.Tables.Add(app_Products);

using (var conn = new SqlConnection(strConn.ToString()))
{
conn.Open();
Fill(conn, ds);
}
}

public void Fill(SqlConnection cnx, DataSet ds)
{
var cmd = cnx.CreateCommand();
cmd.CommandText = “SELECT * FROM dbo.Products; ” + “SELECT * FROM dbo.Categories”;
var adapter = new SqlDataAdapter(cmd);

adapter.TableMappings.Add(“Table”, “App_Products”);
adapter.TableMappings.Add(“Table1”, “App_Categories”);

// In this case, ds.Table.Count = 4
//adapter.TableMappings.Add(“Products”, “App_Products”);
//adapter.TableMappings.Add(“Categories”, “App_Categories”);
adapter.Fill(ds);
}

Jaco

Jaco

C, I concur.
“When the query specified returns multiple results, each result set is placed in a separate table. Additional result sets are named by appending integral values to the specified table name (for example, “Table”, “Table1”, “Table2”, and so on). Since no table is created for a query that does not return rows, if you were to process an insert query followed by a select query, the table created for the select query would be named “Table”, because it is the first table created. Applications using column and table names should ensure that conflicts with these naming patterns does not occur.”
http://msdn.microsoft.com/en-us/library/y4b211hz.aspx

Michael

Michael

Everyone who vote for C are wrong!
The question tells “… You add two DataTable objects named App_Products and App_Categories to the DataSet…”. So D is the right answer!

Steven Muhr

Steven Muhr

D is the correct answer.
You can verify it by coding the use case in visual studio

John Galt

John Galt

Gotta love the two tools above me who say ‘C’ is wrong (Michael and Steven Muhr). Steven even goes as far as stating you can code the use case in Visual Studio, but you can bet the tard didn’t take his own advice.

Kudos to the first poster for not only pointing out the error, and even linking to the relevant MSDN article (if the two fools above me bothered to read it, they wouldn’t come across as complete idiots now).

Yes, the correct answer IS C. The following quote from MSDN documentation says it all:
“The name of the first result set defaults to Table. Further result sets are named Table1, Table2, and so on.”
That’s when you use Fill(ds); call signature, which the questions clearly does.