To begin working with LINQ to SQL in Visual Studio 2008 you will need a database that you can query and a copy of MS SQL Server or SQL Express. In this post, I'm going to focus on SQL Express since it is free and since it gets installed by default when you install all versions of Visual Studio except for some forms of Visual Studio Express. If for any reason you don't have SQL Express installed on your system, you can download it here.
Follow these steps to install and access the copy of the Northwind database that accompanies the samples that ship with Visual Studio 2008:
NOTE: Additional Information on installing the Northwind database is available here.
Choose New Project from the File menu (Ctrl + Shift + N) and create a new console application:
Figure 1: Creating a new console application.
Create a connection to the Northwind database:
At this stage Northwnd.mdf should appear in your server or database explorer, as shown in Figure 3.
Figure 2: Select the Browse button in the Add Connection dialog and locate your copy of Northwnd.mdf
Figure 3: The Server Explorer provides a view of the Northwind database.
The LINQ to SQL Designer allows you to configure and view the metadata of the tables from the database that you want to query. There is a command line version of this tool called SqlMetal that is not covered in this document. By default, SqlMetal is part of the Visual Studio and .NET framework 3.5 install and is stored here: %ProgramFiles%\Microsoft SDKs\Windows\v6.0A\bin.
From the Project menu select Add New Item (Ctrl+Shift+A). Select LINQ to SQL Classes from the list of Visual Studio Installed Templates, as shown in Figure 4.
Figure 4: Choose the LINQ to SQL Designer from the list of available templates available in the Add New Item dialog.
Drag the Customer table from the Server Explorer onto the designer, as shown in Figure 5.
Figure 5: The LINQ to SQL designer with the Server Explorer on the left and the Solution Explorer on the right.
In Figure 5 the Customer table has been dragged from the Server Explorer onto the SQL Designer. Stored Procedures can be dragged onto the area where you see the text that begins “Create methods by dragging items…”
Several things happened as you completed the steps outlined above:
This is not the place to fully explore the SQL Designer and the code it generates. However, the steps shown above give you two key benefits:
After you drag items from the Server Explorer onto the SQL Designer, you can modify the view that your program will have of the data. For instance, you can delete some of the fields from the Customers table, as shown in Figure 5. This operation modifies the classes generated, and not the actual table on the server.
Figure 6: A modified view of the Customer table with only three fields visible.
You can now test your work by opening up Program.cs in the Visual Studio editor and typing in the following code:
using System; using System.Linq; namespace ConsoleApplication41 { class Program { static void Main(string[] args) { DataClasses1DataContext db = new DataClasses1DataContext(); var query = from c in db.Customers where c.City == "London" select c.City; foreach (var q in query) { Console.WriteLine(q); } } } }
In the post, you have seen how to:
You can learn more about LINQ by running the SampleQueries project that ships with Visual Studio samples referenced in this article.