You are on page 1of 4

Home Search Links Add Url Add article Members Namespaces .

NET Chat

Tutorial LINQ to SQL - Part 1 Section: Database serial submitted this resource Visit the profile here Rating: Not rated yet!

Introduction
This article is the first in series, to get closer with LINQ to SQL. Maybe you ask, what is the based language LINQ?

What is LINQ?
LINQ is the abbreviation for Language Integrated Query and it's developed by Microsoft. The goal of LINQ is to directly involve SQL, XLink and XQuery queries in a programming language like C# or VB.net. The advantage of LINQ is that the compiler can check the code for errors or to optimize it.

The syntax is similar to SQL like "select", "from" and "where". Ok, and is LINQ to SQL something special? - LINQ to SQL is a kind of O/R-Mapper, and how it works I will show you in this and the following articles.

What is LINQ to SQL (LtSQL)?


As I said Linq is an O/RM which allows you to model a relational database using classes. You can query your database with LtSQL, delete data and so on. LtSQL supports transactions, views and stored procedures. Now let's start with practice.

Modelling Databases with LtSQL


With the LtSQL-Designer you can easily create a representation of the northwind-database:

Figure 1 In the designer there are four classes designed (INHO the most used tables from northwind around the world?): Product, Category, Order and OrderDetail. Each property of a class represents a column in the table and an instance of a class represesents a row in the database. The arrows and lines are like UML and shows relationships. The right window shows stored procedures wich interacts with our database.

The DataContext, what is it?


If you finished modelling and mapping the database-representation and press "Save", Visual Studio creates classes that represent the database and our relationships. And a DataContext will be created for each LinqToSQL-File we add to our solution. The DataContext is the main object through wich we will communicate with our database. The properties of the DataContext class are the tables and stored procedures in the database we modelled/created. The model we created above, generates this NorthwindDataContext.

Figure 2 So, the database is modelled and our classes are created, and now? Let's show some code-examples for working with all these classes.

Get Products from the Database


The following code creates an instance of the northwind-datacontext through which we get access to the database. After that we use the new variant-datatype to create an IEnumerable products-instance in which all needed products are stored. We get the data with SQL-Like LINQ-Syntax where all Products have the Categoryname "Beverages". The "select product" statement adds the product to the collection.
NorthwindDataContext nwContext = new NorthwindDataContext(); var products = from product in nwContext where product.Category.CategoryName = "Beverages" select product;

Update a Product in the database


Like the first listing, we create a NorthwindDataContext and a product-instance. The product will be the one which has the Name "Toy 1". FEB APR http://w w w .codegod.de/WebAppCodeGod/Tutorial-LINQ-to-SQL---Part-1-AID466.a Go The statement "prod=>prod.ProductName == "Toy 1" " includes the new lambda-expression and means something like 7 captures 13 (example):
18 Apr 08 - 13 Apr 10

MAY Clo He

2008

2010

2011

product =

from product in nwContext.Products where product.ProductName = "Toy 1"

Then we change some details of the product and save the changes in the database.
NorthwindDataContext nwContext = new NorthwindDataContext(); Product product = nwContext.Products.Single( prod => prod.ProductName == "Toy 1"); product.UnitPrice = 100; product.UnitsInStock = 10; nwContext.SubmitChagnes();

Insert a new Category and new Products in the Database


In this sample we create a new category and two new products, for that we create a northwind-datacontext again. After that we add a new category and two new products. As you can see we give the products names, add them to the new category and write the category in the database. Based on our modelled relationships, the products are written correctly to the database, too.
NorthwindDataContext nwContext = new NorthwindDataContext(); Category category = new Category(); category.CategoryName = "Serial - Things"; Product product1 = new Product();

Product product2 = new Product(); product1.ProductName = "Thing 1"; product2.ProductName = "Thing2"; category.Products.Add(product1); category.Products.Add(product2); nwContext.Categories.Add(category); nwContext.SubmitChanges();

And now we just delete some products from the database the same way as before.
NorthwindDataContext nwContext = new NorthwindDataContext(); var products = from product in nwContext.Products where product.ProductName.Contains("Toy") select product; nwContext.Products.RemoveAll(products); nwContext.SubmitChanges();

You see, LINQ to SQL is an easy way to work with databases. I hope this introduction helps you to work with LtSQL. The next articles go deeper into some details, so stay tuned.

Reader-Comments:

Impressum | AGB | Datenschutzerklrung Top-Handy-Bundles | Anzeigen Web | Kostenlose Kleinanzeigen | Kleinanzeigen-Riese anzeigen-kostenlos.com | Singlebrse kostenlos | Suchmaschinenoptimierung | Plastikkarten Drucker | Firmen aus Bayern Website-Work | Fehlsichtigkeit | dotnet-forum | .NET BlogBook | Schufa | SEO | Place my ad here...

You might also like