Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Optimistic control for concurrency in ADO.NET

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 53
    Comment on it

    When we are working on a shared database multiple users will made changes to it simultaneously.

    So in that case when any update is being performed by one person does not gets reflect to the other person using the same table.

     

    So mainly the problem is the update problem. To solve this problem the optimistic way uses the parameter before the update and after the update is being made.

    Optimistic way mainly focus on the way update is being made into the database.

    In any case the change is been made to the database the where clause is fired and the update is being stopped if new value is found out.

    And in the case when data is not altered the same row is been found and updation is being performed.

    You can do the similar thing in ADO.NET by performing validation in the updation of the record

     

     private void TestOptimisticConcurrency()
            {
                try
                {
                    string ConnectionString = "Integrated security = SSPI;" +
                    "Initial Catalog = Northwind;" +
                    "Data Source = localhost;";
                    SqlConnection conn = new SqlConnection(ConnectionString);
                    conn.Open();
                    SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Orders", conn);
                    DataSet ds = new DataSet("test");
                    SqlCommand updateCmd = new SqlCommand();
                    updateCmd.CommandText = @"UPDATE Orders SET CustomerID = @CustomerID," + "OrderData = @OrderDate, ShippedDate = @ShippedData WHERE " +
                    "(OrderID = @Original_OrderID)AND(CustomerID = @Original_CustomerID" +
                    "OR @Original_CustomerID IS NULL AND  customer ID IS NULL) AND " +
                    "(OrderDate = @Original_OrderDate OR @Original_OrderDate " +
                    "IS NULL AND OrderDate IS NULL) AND (ShippedDate = " +
                    "@Original_ShippedDate OR @Original_ShippedDate IS NULL AND " +
                    "ShippedDate IS NULL); SELECT CustomerID, OrderDate, ShippedDate, " +
                    "OrderID FROM Orders WHERE (OrderID = @OrderID)";
                    updateCmd.Connection = conn;
    
                    // CustomerID parameter
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@CustomerID", SqlDbType.NVarChar, 5, "Customer ID"));
    
                    // orderDate Parameters
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@ order Date", SqlDbType.DateTime, 8, "OrderDate"));
    
                    // ShippedDate Parameter
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@ShippedDate", SqlDbType.DateTime, 8, "ShippedDate"));
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@original_OrderID", SqlDbType.Int, 4,
                    ParameterDirection.Input, false, ((System.Byte)(0)),
                    ((System.Byte)(0)), "CustomerID",
                    DataRowVersion.Original, null));
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@Original_CustomerID", SqlDbType.NVarChar,
                    5, ParameterDirection.Input, false, ((System.Byte)(0)),
                    ((System.Byte)(0)), "CustomerID",
                    DataRowVersion.Original, null));
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@Original_OrderDate", SqlDbType.DateTime,
                    8, ParameterDirection.Input, false, ((System.Byte)(0)),
                    ((System.Byte)(0)), "order Date",
                    DataRowVersion.Original, null));
                    updateCmd.Parameters.Add(new SqlParameter
                    ("@Original_ShippedDate", SqlDbType.DateTime,
                    8, ParameterDirection.Input, false, ((System.Byte)(0)),
                    ((System.Byte)(0)), "ShippedDate",
                    DataRowVersion.Original, null));
                    updateCmd.Parameters.Add
                    (new SqlParameter("@OrderID", SqlDbType.Int,
                    4, "OrderID"));
                    da.UpdateCommand = updateCmd;
                    da.Fill(ds, "Orders");
    
                    // update the row in the dateset
                    ds.Tables["Orders"].Rows[0].BeginEdit();
                    ds.Tables["Orders"].Rows[0]["order Date"] = DateTime.Now;
                    ds.Tables["Orders"].Rows[0].EndEdit();
    
                    // update the row in the data source (Orders Table)
                    da.Update(ds, "Orders");
                    MessageBox.Show("Finished updating First row.");
    
                    // close connection
                    conn.Close();
            So in that case when any update is being performed by one person doe not gets reflect to the other person using the same table    }
    
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
    

     

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: