- •Contents
- •Lab 1: linq to sql: Database Language Integrated Queries
- •Lab Objective
- •Task 4 – Querying Database Data
- •Task 5 – Exploring the ide
- •Exercise 2 – Creating an Object Model
- •Task 1 – Creating the order entity
- •Task 2 – Mapping Relationships
- •Task 3 – Strongly Typing the DataContext Object
- •Exercise 3 Using Code Generation to Create the Object Model
- •Task 1 - Adding a linq to sql Classes file
- •Task 2 – Create your object model
- •Task 3 – Querying your object model
- •Task 4 – Mapping a stored procedure
- •Task 5 – Retrieving new results
- •Exercise 4 – Modifying Database Data
- •Task 1 – Modifying your object model
- •Task 2 – Creating a new Entity
- •Task 3 – Updating an Entity
- •Task 4 – Deleting an Entity
- •Task 5 – Submitting changes
- •Task 6 – Using Transactions
- •Lab Summary
Task 6 – Using Transactions
In the Solution Explorer, right-click References, then click Add Reference
In the .NET tab, click System.Transactions, then click OK
By default LINQ to SQL uses implicit transactions for insert/update/delete operations. When SubmitChanges() is called, it generates SQL commands for insert/update/delete and wraps them in a transaction. But it is also possible to define explicit transaction boundaries using the TransactionScope the .NET Framework 2.0 provides. In this way, multiple queries and calls to SubmitChanges() can share a single transaction. The TransactionScope type is found in the System.Transactions namespace, and operates as it does with standard ADO.NET.
At the top of Program.cs, add the following using directive:
using System.Transactions;
In Main, replace the existing code with the following code to have the query and the update performed in a single transaction:
static void Main(string[] args)
{
NorthwindDataContext db = new NorthwindDataContext();
using (TransactionScope ts = new TransactionScope())
{
var q =
from p in db.Products
where p.ProductID == 15
select p;
Product prod = q.First();
// Show UnitsInStock before update
Console.WriteLine("In stock before update: {0}", prod.UnitsInStock);
if (prod.UnitsInStock > 0) prod.UnitsInStock--;
db.SubmitChanges();
ts.Complete();
Console.WriteLine("Transaction successful");
}
Console.ReadLine();
}
Press F5 to debug the application
Lab Summary
Instructor's Note: LINQ to SQL is still an early technology that is evolving, but sufficient progress has been made to demonstrate powerful data capabilities.
In this lab you performed the following exercises:
Exercise 1 – Creating your first LINQ to SQL application Exercise 2 – Creating an object model from a database Exercise 3 – Using code generation to create the object model Exercise 4 – Modifying database data |
In this lab, you learned about how the LINQ Project is advancing query capabilities in the .NET Framework. You mapped database tables to .NET classes and populated objects with live data. You also saw how seamlessly data can be retrieved and updated. Finally, you saw how richer capabilities such as transactions, custom queries, and object identity can make it easier for developers to simply concentrate on the application logic. LINQ to SQL provides a powerful bridge from objects to relational data and makes data-driven applications easier to build than ever.
Thank you for trying LINQ to SQL. We hope you enjoy using LINQ in Visual Studio 2008.
