Which code segment should you use?

A Windows Forms application contains the following code segment.

string SQL = @”SELECT EmployeeID, LastName, FirstName FROM Employees”;
SqlDataAdapter da = new SqlDataAdapter(SQL, connStr);
DataTable dt = new DataTable();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder bld = new SqlCommandBuilder(da);
da.Fill(dt);

The application allows the user to add rows to the data table. The application will propagate these additions to the database. If the addition of any row fails, the other rows must still be added. The code must log how many new rows failed to be added. You need to propagate the additions to the database and log a failed count.
Which code segment should you use?

A Windows Forms application contains the following code segment.

string SQL = @”SELECT EmployeeID, LastName, FirstName FROM Employees”;
SqlDataAdapter da = new SqlDataAdapter(SQL, connStr);
DataTable dt = new DataTable();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
SqlCommandBuilder bld = new SqlCommandBuilder(da);
da.Fill(dt);

The application allows the user to add rows to the data table. The application will propagate these additions to the database. If the addition of any row fails, the other rows must still be added. The code must log how many new rows failed to be added. You need to propagate the additions to the database and log a failed count.
Which code segment should you use?

A.
da.ContinueUpdateOnError = true;
da.Update(dt);
DataTable dtErrors = dt.GetChanges(DataRowState.Unchanged);
Trace.WriteLine(dtErrors.Rows.Count.ToString() + ” rows not added.”);

B.
da.ContinueUpdateOnError = false;
da.Update(dt);
DataTable dtErrors = dt.GetChanges(DataRowState.Unchanged);
Trace.WriteLine(dtErrors.Rows.Count.ToString() + ” rows not added.”);

C.
da.ContinueUpdateOnError = true;
da.Update(dt);
DataRow[] rows = dt.GetErrors();
Trace.WriteLine(rows.Length.ToString() + ” rows not added.”);

D.
da.ContinueUpdateOnError = false;
da.Update(dt);
DataRow[] rows = dt.GetErrors();
Trace.WriteLine(rows.Length.ToString() + ” rows not added.”);



Leave a Reply 0

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