Skip to content

Simple Linq to SQL Insert and Update

Last updated on July 10, 2019

private void InsertAndUpdate()
{
  NorthwindDataContext db = new NorthwindDataContext();

  //Insert a record

  Customer newCus = new Customer();
  newCus.CustomerID = "YYYZZ";
  newCus.CompanyName = "Company_Z";

  db.Customers.InsertOnSubmit(newCus);
  db.SubmitChanges();      

  //Update a record
  Customer record = (from p in db.Customers
         where p.CustomerID == "12345" 
         select p).SingleOrDefault();

  Console.WriteLine(default(Customer));
  if (record != default(Customer))
  {
    record.CompanyName = "Company_A";
  }

  db.SubmitChanges();
}

 

Published inC#CodingSQL

Be First to Comment

Leave a Reply

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