Friday, January 16, 2009

SubSonic versus LinqToSQL: Simple Delete

Continuing the series, SubSonic versus LinqToSQL, this example shows a simple delete using SubSonic and then the equivalent using LinqToSQL. It is worth to note that SubSonic has two ways to delete records, on just updates a bit field called "IsDeleted" and essentially turns the record "off" while the "Destroy" method actually deletes the record.

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 the simple delete using SubSonic:
    using SubSonic;
    using MyCustomDAL;

    ...

    protected void DeleteProduct(int ProductID)
    {
    Product p = new Product(ProductID)
    p.Destroy();
    }

    And here is the simple delete using LinqToSQL:
    using System.Linq;

    ...

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

    Product p = (from products in db.Products
    where products.ProductID == ProductID
    select products).FirstOrDefault();
    db.Products.DeleteOnSubmit(p);
    db.SubmitChanges();

    }

    SubSonic versus LinqToSQL: Simple Insert

    Continuing the series, here I demonstrate a simple insert using SubSonic and then the equivalent using LinqToSQL.

    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 Insert using SubSonic:
    using SubSonic;
    using MyCustomDAL;

    ...

    protected void InsertProduct()
    {
    Product p = new Product();

    p.ProductName = txtProductName.Text;
    p.UnitPrice = decimal.Parse(txtUnitPrice.Text);
    p.UnitsInStock = short.Parse(txtUnitsInStock.Text);
    p.UnitsOnOrder = short.Parse(txtUnitsOnOrder.Text);
    p.QuantityPerUnit = txtQuantityPerUnit.Text;
    p.ReorderLevel = short.Parse(txtReorderLevel.Text);
    p.Discontinued = false;

    p.Save();
    }

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

    ...

    protected void InsertProduct()
    {
    NorthwindDataContext db = new NorthwindDataContext();

    Product p = new Product();
    {
    p.ProductName = txtProductName.Text;
    p.UnitPrice = decimal.Parse(txtUnitPrice.Text);
    p.UnitsInStock = short.Parse(txtUnitsInStock.Text);
    p.UnitsOnOrder = short.Parse(txtUnitsOnOrder.Text);
    p.QuantityPerUnit = txtQuantityPerUnit.Text;
    p.ReorderLevel = short.Parse(txtReorderLevel.Text);
    p.Discontinued = false;
    }
    db.SubmitChanges();

    }
    It is worth to note that between instaniating the Northwind database ("db") in the LinqToSQL and the "db.SubmitChanges();", any inserting calls that you are doing are just submitted once to the database. So, if any of them fail, they all fail.

    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. :)

    SubSonic versus LinqToSQL: Simple Select

    I have used SubSonic for years, and quite frankly was avoiding LinqToSQL because I didn't think that I would want to use it. After playing around with LinqToSQL a bit (forced by using ASP.NET Dynamic Data), it's not too bad. I thought that I would give a few quick comparisons for those who are also using SubSonic and have not drawn a correlation yet on how to achieve similar results while using LinqToSQL. Both are looking at the Northwind database, btw.

    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 Select command using SubSonic:
    using SubSonic;
    using MyCustomDAL;

    ...

    protected void GetProduct(ProductID)
    {
    Product p = new Product(int ProductID)
    txtProductName = p.ProductName;
    txtUnitPrice = p.UnitPrice;
    }

    And here is the LinqToSQL equivalent:
    using System.Linq;

    ...

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

    var products = from p in db.Products
    where p.ID == ProductID
    select p;

    foreach (var product in products)
    {
    txtProductName = product.ProductName;
    txtUnitPrice = product.UnitPrice;
    }
    }

    As you can see, there are similarities between the two. I hope that this will encourage hardcore SubSonic users who have blown off LinqToSQL like I did to at least investigate LinqToSQL since I think it is not going away. I will still be using SubSonic where I can, but if I have a requirement of "All Microsoft" then I will be using LinqToSQL.

    Thursday, January 15, 2009

    New Year, New Choices

    After much deliberation, I have decided to remove Haloscan as the default comment management for this blog. While it had many high points, the fact of when I would get a new notification that a comment has been posted, Haloscan does not provide a link to which Blog post was commented.

    As you can imagine, when trying to keep a dialog or help someone, it makes it difficult to do without this very simple piece of code missing. So, I apologize to all of the people's comments that were posted before today (Jan 14th, 2009), but I need to be able to respond to you guys. :)

    Happy 2009, BTW! :)