You are on page 1of 10

LINQ to SQL

Introduction:With .Net3.5 Microsoft also released Language Integrated Query aka LINQ. This will enable developers to query datasources using a query like synatx with both C# and VB.net. LINQ TO SQL is a technology that builds inside of ADO.net built to work directly with a SQL Server Database to Enable LINQ Style Programming against it. LINQ (Language Integrated Query) defines Keywords that could be used to build query expressions .These Query expressions can select, Filter, Group and Transform data. LINQ keywords include From, In, Where and Select. Different Types of LINQs: LINQ to Objects:- This is the simplest form of the LINQ, allows you to query collections of in-memory objects. LINQ to Dataset:-This is used to query in-memory Dataset. LINQ to SQL:-This will allow us to query an SQL database without using ADO.net LINQ to XML:-This will be used to read an XML file without using dot net speciliazed XML classes. LINQ Expressions:A Typical LINQ query uses the following Keywords. From indicates the Data source from which we need to retrieve the Data. Where Specifies the particular condition to fetch the Data. Select select clause indicates the data we want to retrieve.

The main intent of this article is to show how to use LINQ TO SQL to perform basic operations like select, insert, update and delete Getting Started with LINQ:-

Below Steps are required to start writing LINQ code against the SQL database. Add a reference System.Data.Linq to the project. This namespace contains Data Context object. Create an O/R map that connects all database objects like data tables, Stored Procedures etc. Connect to the database using the DataContext object of LINQ to SQL.

Creation of O/R map:This can be done in three ways. 1) Using visual Studio:Right click on the selected project and choose Add New Item. Select the LINQ to SQL classes:-

The LINQ to SQL database file extension is dbml which stand for database markup language. On the addition of this file, two other files are created with dbml.layout and designer.cs extensions. Dbml.layout File contains XML that represents the metadata of the database. designer.cs represents the coded form of the dbml file. An empty dbml file will look like this..

Now we can build our map by just dragging and droping the database entities from server explorer onto the designer surface. Visual studio does the rest by generating the code relating these entities. You can also drag stored procedures from a database into the method window which in turn creates the code that allows these stored procedures to act as asp.net methods. For example if you create an O/R map for the customer and customercustomerdemo tables of the Northwind database then the dbml designer file will look like this.

2)Using Command Line Tool:We can use command line tool(sqlmetal.exe) to generate both dbml and O/R file of a given database.The following lines show an example for generating the dbml and code file of a database. To Create dbml File:Syntax: Sqlmetal Path of the database file \ Name of the DB file.mdf /Language of code(CS or VB)/dbml:Name of dbml file.dbml Example: Sqlmetal c:\code\Northwinds and pubs dbs \ Northwind.mdf /language:CS/dbml:Northwind.dbml

To Create Code File:Syntax: Sqlmetal Path of the database file \ Name of the DB file.mdf /Language of code(CS or VB)/code:Name of CS file.CS Example: Sqlmetal c:\code\Northwinds and pubs dbs \ Northwind.mdf /language:CS/code:Northwind.cs The above method is useful when we are dealing with large databases where dragging and dropping is a bit difficult.

3)Using Code Editor:To get started with this we need to create a class file. At the top of the class file add the fllowing namespaces. Using System.Data.Linq Using System.Data.Linq.Mapping The next step is to link your class to a table in the database through a TABLE attribute and define attributes for the columns of the data table. The sample code looks like the following

-----------------------------------------------------------------------------------------------------------------------Connecting to the Database:To create connection with the database, first we need to define a Data Context Object. Create an instance of the dataContext object and pass the connection string as the parameter. DataContext dc = new DataContext(connection string); Inserting ,Deleting and Updating using LINQ to SQL:For implementing these queries simply make the modification to the object (Objects that are created in the designer.cs file) instances and save changes. We will work with our object model to add a new collection, to update the collection or remove a collection and then submit changes.

Suppose if we wish to add a product to our database .the sample code will look like this.

Delete Will also work on the same way except that we need to use the function DeleteOn Submit() Sample code for delete operation:-

To update the data first we need to retrieve the data using a select query and then we can update the data by calling submitchanges. This will save the changes to the database. The following is the code for update command.

Thus, Microsoft through LINQ to SQL, provides an excellent and more easy way to communicate with the sql database. Please feel free to reach me @ rbethamcharl@csc.com for any queries regarding this implementation.