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:-

Stored Procedures etc.  Add a reference System. This namespace contains Data Context object.Data. Select the LINQ to SQL classes:- .Linq to the project. 1) Using visual Studio:Right click on the selected project and choose “Add New Item”. Creation of O/R map:This can be done in three ways.Below Steps are required to start writing LINQ code against the SQL database.  Connect to the database using the DataContext object of LINQ to SQL.  Create an O/R map that connects all database objects like data tables.

.

layout File contains XML that represents the metadata of the database. Visual studio does the rest by generating the code relating these entities.cs represents the coded form of the dbml file..cs extensions. designer. two other files are created with dbml. Dbml. An empty dbml file will look like this. .net methods.layout and designer. On the addition of this file.The LINQ to SQL database file extension is dbml which stand for “database markup language”. 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. 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. Now we can build our map by just dragging and droping the database entities from server explorer onto the designer surface.

dbml . To Create dbml File:Syntax: Sqlmetal “Path of the database file” \ Name of the DB file.The following lines show an example for generating the dbml and code file of a database.dbml Example: Sqlmetal “c:\code\Northwinds and pubs dbs” \ Northwind.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.mdf /Language of code(CS or VB)/dbml:Name of dbml file.mdf /language:CS/dbml:Northwind.

Linq Using System. At the top of the class file add the fllowing namespaces.Data. 3)Using Code Editor:To get started with this we need to create a class file.CS Example: Sqlmetal “c:\code\Northwinds and pubs dbs “\ Northwind.Linq.Data. Using System.mdf /language:CS/code:Northwind.mdf /Language of code(CS or VB)/code:Name of CS file.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.To Create Code File:Syntax: Sqlmetal “Path of the database file” \ Name of the DB file.cs The above method is useful when we are dealing with large databases where dragging and dropping is a bit difficult. The sample code looks like the following .

cs file) instances and save changes.-----------------------------------------------------------------------------------------------------------------------Connecting to the Database:To create connection with the database. first we need to define a Data Context Object. 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. to update the collection or remove a collection and then submit changes. We will work with our object model to add a new collection. Create an instance of the dataContext object and pass the connection string as the parameter.

Delete Will also work on the same way except that we need to use the function DeleteOn Submit() Sample code for delete operation:- .the sample code will look like this.Suppose if we wish to add a product to our database .

.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”. The following is the code for update command. This will save the changes to the database.

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

Sign up to vote on this title
UsefulNot useful