.NET
  Home arrow .NET arrow Page 5 - Introducing LINQ to SQL Designer using Vis...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing LINQ to SQL Designer using Visual Studio 2008
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 19
    2008-04-07

    Table of Contents:
  • Introducing LINQ to SQL Designer using Visual Studio 2008
  • Creating a simple LINQ to SQL application: fetching information onto a web page
  • Creating a simple LINQ to SQL application: updating information to the database
  • Creating a simple LINQ to SQL application: handling transactions using LINQ
  • Explanation of handling transactions in LINQ to SQL

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing LINQ to SQL Designer using Visual Studio 2008 - Explanation of handling transactions in LINQ to SQL


    (Page 5 of 5 )

    The "LINQ to SQL" framework automatically encloses the modifications into a transaction, if it is trying to handle more than one row at a time (say, submitting more rows to save to a database).

    Let us try to understand each line from the previous section.


    Dim db As New SampleDBDataContext

    Dim depts As Table(Of dept) = db.depts


    The above creates an instance to the database context. Through the context object, we interact with the database. In other words, it is the main interface. As "Department" is the parent table, I created a reference to the collection of "dept" models. This can be used to add "dept" objects directly to it. We can also do the same using "db.depts" directly.


    Dim NewDept As New dept

    NewDept.deptno = 50

    NewDept.dname = "Legal"


    Adding a new department involves creating a new "dept" object. The above does the same. It instantiates a "dept" object and provides the values for the properties.


    'create new emp

    Dim NewEmp1 As New emp

    NewEmp1.empno = 2001

    NewEmp1.ename = "aaa"

    NewEmp1.sal = "2500"

    NewEmp1.deptno = 50


    'create another emp

    Dim NewEmp2 As New emp

    NewEmp2.empno = 2002

    NewEmp2.ename = "bbb"

    NewEmp2.sal = "5500"

    NewEmp2.deptno = 50


    Once the "Dept" object is ready, we need to add a few employees (say "emp" objects) to the newly created "dept" object. The above creates two new "emp" objects and provides the values for their properties.


    NewDept.emps.Add(NewEmp1)

    NewDept.emps.Add(NewEmp2)


    As the two employees are related to the same department, we need to add those to the sub-collection of "Dept" object (which is nothing but the "Emps" collection). The above does the same.


    db.depts.InsertOnSubmit(NewDept)

    db.SubmitChanges()


    The first line in the above sample adds the entire dept structure (along with employees) to the "depts" collection of the database context. Finally, we save it using the second line.

    The "db.SubmitChanges()" opens a transaction. The transaction is implicitly handled as part of the "SubmitChanges" method.

    Make sure that if the "SubmitChanges" method needs to work with the explicit transaction (say distributed), we can execute the statement in a separate "TransactionScope" enclosure as follows:


    Using ts As New System.Transactions.TransactionScope

    .

    .

    db.SubmitChanges()

    .

    .

    End Using


    I hope you enjoyed the article. Any suggestions, bugs, errors, enhancements, etc. are highly appreciated at http://jagchat.spaces.live.com


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Hello guys, This is my second article in the series and is an introductory...
     

    .NET ARTICLES

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek