.NET
  Home arrow .NET arrow Page 5 - Introducing LINQ to SQL Designer using Vis...
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 11
    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

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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

    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET

    Iron Speed




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway