Linq to SQL Example Tutorial - Visual Studio ORCAS March 2007 CTP - ASP.NET

Linq to SQL Example Tutorial - Visual Studio ORCAS March 2007 CTP - ASP.NET

by David Hayden ( Florida .NET C# SQL Server Developer ), Filed: Linq to SQL

 

I had a chance to play with Linq to SQL this evening in the Visual Studio ORCAS March 2007 CTP. Although I am sure a lot has changed since I played with it last, it has that same wonderful visual designer that I talked about here:

DLinq Designer - Visual Drag and Drop O/R Mapping Design Surface in Visual Studio 2005 - DLinq Tutorials

The visual designer for Linq to SQL makes things a snap, allowing you to drag SQL Server Database Tables from the Server Explorer to the designer surface. Linq to SQL creates all the classes, relationships, etc. for you as soon as you drop each table onto the surface.

Here is a quick Linq to SQL Tutorial I whipped together in about 5 minutes:

 

Linq To SQL Tutorial

 

Database1 has a Subscribers Table in it for inserting new subscribers.

 

Linq To SQL Tutorial

 

As shown in the project above, I added a new “Linq to SQL“ item to my website, called DataClasses1.dbml, which is a visual designer for Linq to SQL. As soon as I drag and drop my Subscribers Table from the Server Explorer to the Linq to SQL Visual Designer I get the following:

 

Linq To SQL ASP.NET Website Visual Designer

 

And that is it! Just code. Let's insert a new Subscriber into the Subscribers Table based on all the code that the Linq to SQL Visual Design Surface has generated for us under the covers:

 

DataClasses1DataContext context =
new DataClasses1DataContext(); Subscriber subscriber = new Subscriber(); subscriber.Name = "David Hayden"; subscriber.Email = "emailaddress"; context.Subscribers.Add(subscriber); context.SubmitChanges();

 

I kept this example simple just to re-introduce the players and concepts in Linq to SQL, but of course you can persist entire object graphs with relationships, etc. Here is the newly inserted subscriber in the database that took about 5 minutes to code:

 

Linq To SQL Example

 

Conclusion

Linq to SQL is indeed cool and a useful O/R Mapper from Microsoft that will be released with Visual Studio ORCAS.

 

Linq to Sql Examples and Tutorials

 

Source: David Hayden ( Florida .NET C# SQL Server Developer )

Filed: Linq to SQL

 

posted on Sunday, March 18, 2007 9:39 PM

Main

David Hayden Google +

David Hayden Twitter

Health & Fitness

JavaScript Patterns Book Review

HTML 5 and CSS3 - Develop with Tomorrow's Standards Today

Professional ASP.NET Design Patterns Book Review

Beginning Mac Programming Book Review

C# in Depth Book Review

ASP.NET MVC

Orchard CMS

Categories