Beginning LINQ to SQL Using Visual Studio 2008
(Page 1 of 6 )
This is an introductory article which focuses on fetching and updating databases using the DataContext object in "LINQ to SQL." By reading this article, you will learn how to work with "LINQ to SQL" manually, without using a designer and with great simplicity. It should serve to give a solid foundation to both VB and C# programmers.
I will not be including great theories of LINQ or related technologies in this article; Microsoft already features great content on those topics on its MSDN. If you are absolutely new to LINQ, I request that you go through the information you'll find at this link first:
http://msdn2.microsoft.com/en-us/netframework/aa904594.aspx
The entire source code for this article is available in the form of a free downloadable zip file. The solution was developed using Microsoft Visual Studio 2008 Team Edition on Microsoft Windows Server 2003 Standard Edition with Microsoft SQL Server 2005 Developer Edition. I didn't really test it in any other environment. I request that you post in the discussion area if you have any problems in execution.
Developing a simple "LINQ to SQL" application (without LINQ designer): creating the project
It is always easier to work with wizards and designers rather than coding manually. But when it comes to customizing the auto-generated code, we should have a fair understanding of generated code and its related pieces.
We can generate lots of code automatically using Visual Studio designers (like DataSet Designer, LINQ to SQL Designer, etc.). As this is the most fundamental article on "LINQ to SQL," I would like to introduce the manual way of coding first rather than using "LINQ to SQL Designer" and complicating things. This is only to give you a better understanding of "LINQ to SQL" from the point of view of grasping the basics. My upcoming articles gradually focus on working with LINQ to SQL Designer.
To make this article simple, I created two tables, "emp" and "dept" as follows:

The following are the steps for creating an ASP.NET 3.5 application with "LINQ to SQL" support:
Open Visual Studio 2008
Go to File || New || Project
In the "New Project" dialog, make sure ".NET Framework 3.5" is selected. In the "Project Type," select either "Visual Basic || Web" or "Visual C# || Web."
Select "ASP.NET Web Application" in the templates, provide the name of the application (Fig 3) and finally hit the OK button.

Next: Developing a simple LINQ to SQL application (without LINQ designer): UI Design and source in VB.NET >>
More .NET Articles
More By Jagadish Chaterjee