Friday, January 16, 2009

SubSonic versus LinqToSQL: Simple Update

Continuing with my series "SubSonic versus LinqToSQL", here I'm going to compare a simple update using the Northwind database.

If you are looking for other posts in this series, here is the list:

  • SubSonic versus LinqToSQL: Simple Select

  • SubSonic versus LinqToSQL: Simple Insert

  • SubSonic versus LinqToSQL: Simple Update

  • SubSonic versus LinqToSQL: Simple Delete


  • Here is a Simple Update while using SubSonic:
    using SubSonic;
    using MyCustomDAL;

    ...

    protected void UpdateProduct(ProductID)
    {
    Product p = new Product(int ProductID)
    p.ProductName = txtProductName.Text;
    p.UnitPrice = double.Parse(txtUnitPrice.Text);
    p.Save();
    }

    And here is a Simple Update while using LinqToSQL:
    using System.Linq;

    ...

    protected void UpdateProduct(int ProductID)
    {
    NorthwindDataContext db = new NorthwindDataContext();

    Product p = (from products in db.Products
    where products.ProductID == ProductID
    select products).FirstOrDefault();
    p.UnitPrice = 0;
    p.Discontinued = true;
    db.SubmitChanges();
    }

    As you can see there are some similarities as there are also differences. I encourage everyone to look into what those differences are to better prepare your programming toolset for jobs that lie ahead. :)