Click here to Skip to main content
13,352,428 members (39,400 online)
Click here to Skip to main content

Stats

66.9K views
527 downloads
47 bookmarked
Posted 19 Jul 2009

How To Handle Concurrency in LINQ to SQL?

, 19 Jul 2009
How to handle concurrency in LINQ to SQL?
LINQConcurrency
LINQConcurrency
App_Data
bin
LINQConcurrency.dll
LINQConcurrency.pdb
LINQConcurrency.csproj.user
Properties
LINQConcurrency.suo
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Linq;
namespace LINQConcurrency
{
    public partial class _Default : System.Web.UI.Page
    {
        string strConnectionString = @"Data Source=localhost;Initial Catalog=Sales;Integrated Security=True";
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void btnTestLinqConcurrency_Click(object sender, EventArgs e)
        {
            DataContext objContext = new DataContext(strConnectionString);
            clsCustomer objCustomer = objContext.GetTable<clsCustomer>().First<clsCustomer>();
            objCustomer.CustomerName = "Iwanttochange";
            SomeOneChangedData();
            try
            {

                objContext.SubmitChanges();
            }
            catch (ChangeConflictException ex)
            {
                foreach (ObjectChangeConflict objchangeconf in objContext.ChangeConflicts)
                {
                    objchangeconf.Resolve(RefreshMode.OverwriteCurrentValues);
                }
            }
        }
        private void SomeOneChangedData()
        {
            DataContext objContext = new DataContext(strConnectionString);
            clsCustomer objCustomer = objContext.GetTable<clsCustomer>().First<clsCustomer>();
            // changing to some random customer name
            objCustomer.CustomerName = new Random().NextDouble().ToString();
            objContext.SubmitChanges();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author


You may also be interested in...

Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 20 Jul 2009
Article Copyright 2009 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid