Condividi tramite


LINQ to SQL : Update data through Object Model

LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT, let me discuss about the update method

static void Main(string[] args)

{

    string strConnection = @"Connection String";

    TestDB db = new TestDB(strConnection);

   

    //Here I am finding the employee with Id 19

    var updateQ = db.Emps.First(e => e.Id == 19);

    //Then I will modify that employee name and give new name

    updateQ.Name = "Updated Employee";

                           

    //Commit the changes to database

    //at this point DML gets generated

    db.SubmitChanges();

    //To view the updated data

    ObjectDumper.Write(db.Emps);

}

All the methods are coming from DataContext class (responsible for SQL query generation). The above method converts the object addition to DML query.

Namoskar!!!

Comments

  • Anonymous
    July 16, 2007
    LINQ to SQL support updating data through object. Continuing with my previous blog on INSERT , let me

  • Anonymous
    August 04, 2007
    Thanks for the useful post. For me, there are still two questions:  - What should I do if I change two objects o1 and o2,  want to persist o1 with database but do not want to   submit the changes made to o2?  - What should I do if I have a distributed architecture and my objects pass remoting or WCF boundaries and their changed forms are sent back to the server-side? Is Linq2SQL able to find the changes and submit them to database?

  • Anonymous
    August 06, 2007
    By default LINQ to SQL uses optimistic concurrency and you could implement the pessimistic concurrency by implementing the TransactionScope which is new in .NET Framework 3.0. If the value changes in the database and you want to update the changed data, LINQ to SQL will throw you an error. Wriju

  • Anonymous
    December 17, 2007
    Well one clue to what I want to do -- DML query..?

  • Anonymous
    March 14, 2008
    What is the equivalent in VB.NET?

  • Anonymous
    April 29, 2008
    "TransactionScope which is new in .NET Framework 3.0." TransactionScope was in 2.0 :D

  • Anonymous
    September 11, 2008
    Nice post.Really helped a lot.Thanks a lot.

  • Anonymous
    December 15, 2008
    Thanks an answer to the equivalent in VB :same :) try it not so different..

  • Anonymous
    September 01, 2009
    Thanks for your helpful tip. Can i translate this lecture to my blog?

  • Anonymous
    September 18, 2009
    @song, Please feel free to do.

  • Anonymous
    September 27, 2009
    I did same as this, But it did not work. I don't know why. Can somebody help me fix it please? my code is: using (DataContext.Order db = new DataContext.Order((string)HttpContext.Current.Session["Level"]))            {                var NewOrder = db.Users.Single(p=>p.Id==intId); // find update record                NewOrder.Name = txtName.Text.Trim();                NewOrder.Address = txtAdd.Text.Trim();                NewOrder.Zip = txtZip.Text.Trim();                db.SubmitChanges();                                //submit update                stMsg.Text = "Record Updated.";            }

  • Anonymous
    March 29, 2010
    The solution presented here does not work. I don't understand why bother wrriting a solution that does not work.

  • Anonymous
    January 19, 2011
    Hi vali... the solution works fine.

  • Anonymous
    March 30, 2011
    Awesome clear