0
Answer

Could any one clarify this concurrency code? {UPDATED POST: LINK FIXED}

petre ricardo

petre ricardo

15y
1.9k
1

Hi,
I have foudn an article on handling Optimistic concurrency. But i dont understand how it's been done. well in few lines:
In the C# code:
1. code doesnt have property refactoring and Data Retreival and Updates has been done in on method.
2. Please refer to the question that i've put inline:
 
   SqlDataAdapter custDA = new SqlDataAdapter("SELECT CustomerID, CompanyName FROM Customers ORDER BY CustomerID", nwindConn);
   //1. Should i put this update operations within the same method block with the select operations?
  //The Update command checks for optimistic concurrency violations in the WHERE clause.
  custDA.UpdateCommand = new SqlCommand("UPDATE Customers (CustomerID, CompanyName) VALUES(@CustomerID, @CompanyName) " +
                                        "WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName", nwindConn);
  custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
  custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 30, "CompanyName");

  //Pass the original values to the WHERE clause parameters.
  SqlParameter myParm;
//2. Why should i include a SQLParameter assignment here?
  myParm = custDA.UpdateCommand.Parameters.Add("@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
//3. What his "myParm.SourceVersion" does? Does it sore the Original fetched value in the select 
//statement?
  myParm.SourceVersion = DataRowVersion.Original;

  myParm = custDA.UpdateCommand.Parameters.Add("@oldCompanyName", SqlDbType.NVarChar, 30, "CompanyName");
  myParm.SourceVersion = DataRowVersion.Original;
  //Add the RowUpdated event handler.
  custDA.RowUpdated += new SqlRowUpdatedEventHandler(OnRowUpdated);

  DataSet custDS = new DataSet();
  //the value
  custDA.Fill(custDS, "Customers");

  //4. why did the author call the update() in here? Why he put Fill() and UPdate() in the same 
//method block?
  custDA.Update(custDS, "Customers");

 5. From the GridView control, how do i find the "Customer" table it's DataSource?
TY