locked
Using Data Source and Data Context RRS feed

  • Question

  • User-1236212839 posted

    I have a Microsaoft Visual Studio C# program that accesses an SQL database.  I'm familiar with SQL but new to LINQ.  I have created a data source with the database and a LINQ to SQL dbml module and placed my tables into the dbml module.

    What is the best way to add and retrieve records, via the dataset or the datacontext?

    Again this is new to me.

    Thursday, November 24, 2011 12:00 PM

Answers

  • User-1461035352 posted

    Hi

    According to my knowledge If you are using Linq then you must use the potential of DataContext to access database.

    Here is a  link to nice and basic article about creating data access layer using linq and datacontext

    http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

    I hope it will help

    Kind Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 24, 2011 12:26 PM
  • User3866881 posted

    What is the best way to add and retrieve records

    For adding records, You should create an instance of a specific class first, assign each of the properties to the class and calll InsertOnSubmit, and in the end call Submit(). Here's an example for this:

    var newCategory = new Category
    {
        CategoryName = "Widgets",
        Description = "Widgets are the ……"
    };
    var newProduct = new Product
    {
        ProductName = "Blue Widget",
        UnitPrice = 34.56M,
        Category = newCategory
    };
    db.Categories.InsertOnSubmit(newCategory);
    db.SubmitChanges();

    2)About retreiving records, you can directly use LinqDataSource to bind to the dbml file, or just say this——

    var result = from e in XXX.Categories
                      select e;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 26, 2011 2:04 AM

All replies

  • User-1461035352 posted

    Hi

    According to my knowledge If you are using Linq then you must use the potential of DataContext to access database.

    Here is a  link to nice and basic article about creating data access layer using linq and datacontext

    http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx

    I hope it will help

    Kind Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 24, 2011 12:26 PM
  • User3866881 posted

    What is the best way to add and retrieve records

    For adding records, You should create an instance of a specific class first, assign each of the properties to the class and calll InsertOnSubmit, and in the end call Submit(). Here's an example for this:

    var newCategory = new Category
    {
        CategoryName = "Widgets",
        Description = "Widgets are the ……"
    };
    var newProduct = new Product
    {
        ProductName = "Blue Widget",
        UnitPrice = 34.56M,
        Category = newCategory
    };
    db.Categories.InsertOnSubmit(newCategory);
    db.SubmitChanges();

    2)About retreiving records, you can directly use LinqDataSource to bind to the dbml file, or just say this——

    var result = from e in XXX.Categories
                      select e;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, November 26, 2011 2:04 AM