Which code segment should you use?

You use Microsoft .NET Framework 4.0 to develop an application that connects to a Microsoft SQL Server 2008 database.
The application includes a table adapter named taStore, which has the following DataTable.

image:25.png

There is a row in the database that has a ProductID of 680. You need to change the Name column in the row to “New Product Name”.
Which code segment should you use?

You use Microsoft .NET Framework 4.0 to develop an application that connects to a Microsoft SQL Server 2008 database.
The application includes a table adapter named taStore, which has the following DataTable.

image:25.png

There is a row in the database that has a ProductID of 680. You need to change the Name column in the row to “New Product Name”.
Which code segment should you use?

A.
var dt = new taStore.ProductDataTable();
var ta = new taStoreTableAdapters.ProductTableAdapter();
ta.Fill(dt);
taStore.ProductRow row = (taStore.ProductRow)dt.Rows.Find(680) ;
row.Name = “New Product Name”;
ta.Update(row);

B.
var ta = new taStoreTableAdapters.ProductTableAdapter();
var dt = ta.GetData();
var row = dt.Select(“680”) ;
row[0][“Name”] = “New Product Name”;
ta.Update(row);

C.
var dt = new taStore.ProductDataTable();
var ta = new taStoreTableAdapters.ProductTableAdapter();
ta.Fill(dt);
var dv = new DataView();
dv.RowFilter = “680”;
dv[0][“Name”] = “New Product Name”;
ta.Update(dt);

D.
var dt = new taStore.ProductDataTable();
var row = dt.NewProductRow();
row.ProductID = 680;
row.Name = “New Product Name”;
dt.Rows.Add(row) ;

Explanation:
DataRowCollection.Find() Method To use the Find method, the DataTable object to which the DataRowCollection object belongs to
must have at least one column designated as a primary key column. See the PrimaryKey property for details
on creating a PrimaryKey column, or an array of DataColumn objects when the table has more than one primary key.

var dt = new CustomersDS.CustomersDataTable();
var ta = new CustomersDSTableAdapters.CustomersTableAdapter();
ta.Fill(dt);
CustomersDS.CustomersRow row = (CustomersDS.CustomersRow)dt.Rows.Find(4);
row.Name = “A. Found Customer Id”;
ta.Update(row);

DataTable.Select() Method Gets an array of all DataRow objects that match the filter criteria. To create the filterExpression argument,
use the same rules that apply to the DataColumn class’s Expression property value for creating filters.

var ta = new CustomersDSTableAdapters.CustomersTableAdapter();
var dt = ta.GetData();
var row = dt.Select(“CustomerID > 2”);
row[0][“Name”] = “B. Found Customer Id”;
ta.Update(row);

TableAdapter Overview
(http://msdn.microsoft.com/en-us/library/bz9tthwx(v=vs.80).aspx)



Leave a Reply 0

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