Skip to content

Month: January 2014

Simple Linq to SQL Insert and Update

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();
}

 

Leave a Comment