What should you do to ensure that the application updates only records without optimistic concurrency violations?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft ADO.NET.

The application connects to a Microsoft SQL Server 2005 database that contains a table named Customers. Concurrent users update the Customers table frequently.
You write the following code segment. (Line numbers are included for reference only.)

01 SqlDataAdapter adapter = new SqlDataAdapter("SELECT CustomerID, CompanyName, LastUpdated" + " FROM Customers ORDER BY CustomerID", connection);
02 adapter.UpdateCommand = new SqlCommand("UPDATE Customers Set CompanyName = @CompanyName" + " WHERE CustomerID = @CustomerID AND " + "LastUpdated = @LastUpdated", connection);
03 adapter.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.Int, 0, "CustomerID");
04 adapter.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 30, "CompanyName");
05 SqlParameter parameter = adapter.UpdateCommand.Parameters.Add("@LastUpdated", SqlDbType.Timestamp, 0, "LastUpdated");
06 parameter.SourceVersion = DataRowVersion.Original;
07

You need to ensure that the application updates only records without optimistic concurrency violations.

What should you do?

You create an application by using the Microsoft .NET Framework 3.5 and Microsoft ADO.NET.

The application connects to a Microsoft SQL Server 2005 database that contains a table named Customers. Concurrent users update the Customers table frequently.
You write the following code segment. (Line numbers are included for reference only.)

01 SqlDataAdapter adapter = new SqlDataAdapter(“SELECT CustomerID, CompanyName, LastUpdated” + ” FROM Customers ORDER BY CustomerID”, connection);
02 adapter.UpdateCommand = new SqlCommand(“UPDATE Customers Set CompanyName = @CompanyName” + ” WHERE CustomerID = @CustomerID AND ” + “LastUpdated = @LastUpdated”, connection);
03 adapter.UpdateCommand.Parameters.Add(“@CustomerID”, SqlDbType.Int, 0, “CustomerID”);
04 adapter.UpdateCommand.Parameters.Add(“@CompanyName”, SqlDbType.NVarChar, 30, “CompanyName”);
05 SqlParameter parameter = adapter.UpdateCommand.Parameters.Add(“@LastUpdated”, SqlDbType.Timestamp, 0, “LastUpdated”);
06 parameter.SourceVersion = DataRowVersion.Original;
07

You need to ensure that the application updates only records without optimistic concurrency violations.

What should you do?

A.
Insert the following code segment at line 07.
adapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating)
Add the following event handler method.
static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs args)
{
if (args.Row.HasErrors)
{
args.Row.RowError = “Optimistic Concurrency Violation” + “Encountered”;
args.Status = UpdateStatus.SkipCurrentRow;
}
}

B.
Insert the following code segment at line 07.
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
Add the following event handler method.
protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
if (args.RowCount == 0)
{
args.Row.RowError = “Optimistic Concurrency Violation ” + “Encountered”;
args.Status = UpdateStatus.SkipCurrentRow;
}
}

C.
Insert the following code segment at line 07.
adapter.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);
Add the following event handler method.
protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
if (args.RecordsAffected == 0)
{
args.Row.RowError = “Optimistic Concurrency Violation ” + “Encountered”;
args.Status = UpdateStatus.SkipCurrentRow;
}
}

D.
Insert the following code segment at line 07.
adapter.RowUpdating += new SqlRowUpdatingEventHandler(OnRowUpdating)
Add the following event handler method.
static void OnRowUpdating(object sender, SqlRowUpdatingEventArgs args)
{
if (args.Row.RowState == DataRowState.Modified)
{
args.Row.RowError = “Optimistic Concurrency Violation” + “Encountered”;
args.Status = UpdateStatus.SkipCurrentRow;
}
}



Leave a Reply 0

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