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?

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)
dd 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;
}
}

Explanation:
Optimistic concurrency means that you assume no one will be making edits to a record while you are making your edits. Because you are “optimistic” that two people will not edit the same record simultaneously, you do not apply a lock to the record as soon as a user starts to edit it. Instead, you apply the lock only when the actual update is attempted. This results in a much shorter lock, and in database terms, this is good. Locks are smart; but long locks are not smart.

protected static void OnRowUpdated(object sender, SqlRowUpdatedEventArgs args)
{
if (args.RecordsAffected == 0)
{
args.Row.RowError = “Optimistic Concurrency Violation Encountered”;
args.Status = UpdateStatus.SkipCurrentRow;
}
}

http://msdn.microsoft.com/en-us/library/aa0416cz.aspx



Leave a Reply 0

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