Professional Documents
Culture Documents
http://msdn.microsoft.com/en-us/library/bb386927
Note
Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Visual Studio Settings. This walkthrough was written by using Visual C# Development Settings.
Prerequisites
This walkthrough requires the following: This walkthrough uses a dedicated folder ("c:\linqtest6") to hold files. Create this folder before you begin the walkthrough. The Northwind sample database. If you do not have this database on your development computer, you can download it from the Microsoft download site. For instructions, see Downloading Sample Databases (LINQ to SQL). After you have downloaded the database, copy the northwnd.mdf file to the c:\linqtest6 folder. A C# code file generated from the Northwind database. You can generate this file by using either the Object Relational Designer or the SQLMetal tool. This walkthrough was written by using the SQLMetal tool with the following command line: sqlmetal /code:"c:\linqtest6\northwind.cs" /language:csharp "C:\linqtest6 \northwnd.mdf" /pluralize For more information, see SqlMetal.exe (Code Generation Tool).
Overview
This walkthrough consists of six main tasks: Creating the LINQ to SQL solution in Visual Studio. Adding the database code file to the project. Creating a new customer object. Modifying the contact name of a customer. Deleting an order. Submitting these changes to the Northwind database.
1 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386927
To add System.Data.Linq
1. In Solution Explorer, right-click References, and then click Add Reference. 2. In the Add Reference dialog box, click .NET, click the System.Data.Linq assembly, and then click OK. The assembly is added to the project. 3. Add the following directives at the top of Program.cs:
2 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386927
// Use the following connection string. Northwnd db = new Northwnd(@"c:\linqtest6\northwnd.mdf"); // Keep the console window open after activity stops. Console.ReadLine();
2. Press F5 to test the application at this point. A Console window opens. You can close the application by pressing Enter in the Console window, or by clicking Stop Debugging on the Visual Studio Debug menu.
// Create the new Customer object. Customer newCust = new Customer(); newCust.CompanyName = "AdventureWorks Cafe"; newCust.CustomerID = "ADVCA"; // Add the customer to the Customers table. db.Customers.InsertOnSubmit(newCust); Console.WriteLine("\nCustomers matching CA before insert"); foreach (var c in db.Customers.Where(cust => cust.CustomerID.Contains("CA"))) { Console.WriteLine("{0}, {1}, {2}", c.CustomerID, c.CompanyName, c.Orders.Count); }
3 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386927
2. Press F5 to debug the solution. 3. Press Enter in the Console window to stop debugging and continue the walkthrough.
Updating an Entity
In the following steps, you will retrieve a Customer object and modify one of its properties.
// Query for specific customer. // First() returns one object rather than a collection. var existingCust = (from c in db.Customers where c.CustomerID == "ALFKI" select c) .First(); // Change the contact name of the customer. existingCust.ContactName = "New Contact";
Deleting an Entity
Using the same customer object, you can delete the first order. The following code demonstrates how to sever relationships between rows, and how to delete a row from the database. Add the following code before Console.ReadLine to see how objects can be deleted:
To delete a row
Add the following code just above Console.ReadLine();:
// Access the first element in the Orders collection. Order ord0 = existingCust.Orders[0]; // Access the first element in the OrderDetails collection. OrderDetail detail0 = ord0.OrderDetails[0]; // Display the order to be deleted. Console.WriteLine ("The Order Detail to be deleted is: OrderID = {0}, ProductID = {1}", detail0.OrderID, detail0.ProductID);
4 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386927
// Mark the Order Detail row for deletion from the database. db.OrderDetails.DeleteOnSubmit(detail0);
db.SubmitChanges();
2. Insert the following code (after SubmitChanges) to show the before and after effects of submitting the changes:
Console.WriteLine("\nCustomers matching CA after update"); foreach (var c in db.Customers.Where(cust => cust.CustomerID.Contains("CA"))) { Console.WriteLine("{0}, {1}, {2}", c.CustomerID, c.CompanyName, c.Orders.Count); }
3. Press F5 to debug the solution. 4. Press Enter in the Console window to close the application.
Note
After you have added the new customer by submitting the changes, you cannot execute this solution again as is. To execute the solution again, change the name of the customer and customer ID to be added.
See Also
Concepts Learning by Walkthroughs (LINQ to SQL)
Yes
No
5 of 6
5/25/2012 11:39 AM
http://msdn.microsoft.com/en-us/library/bb386927
6 of 6
5/25/2012 11:39 AM