Tuesday, July 9, 2013

LINQ–Language Integrated Query

Language-Integrated Query (LINQ) is a set of features introduced in Visual Studio 2008 that extends powerful query capabilities to the language syntax of C# and Visual Basic. LINQ introduces standard, easily-learned patterns for querying and updating data, and the technology can be extended to support potentially any kind of data store. Visual Studio includes LINQ provider assemblies that enable the use of LINQ with .NET Framework collections, SQL Server databases, ADO.NET Datasets, and XML documents.

LINQ Providers:
  • LINQ to SQL
  • LINQ to XML
  • LINQ to Dataset
  • LINQ to Objects
LINQ to SQL is a component of .NET Framework version 3.5 that provides a run-time infrastructure for managing relational data as objects. In LINQ to SQL, the data model of a relational database is mapped to an object model expressed in the programming language of the developer. When the application runs, LINQ to SQL translates into SQL the language-integrated queries in the object model and sends them to the database for execution. When the database returns the results, LINQ to SQL translates them back to objects that you can work with in your own programming language.
By using LINQ to SQL, you can use the LINQ technology to access SQL databases just as you would access an in-memory collection.
Step 1: The Below picture will explain how to add LINQ to SQL in your project
  • Open Visual Studio and goto New Project and select the Windows Form Application.
  • In Solution Explorer, right click the application name -> Add -> New item.

Step 2: After open the "Add New Item Wizard" select the LINQ to SQL classes file in Data Category.


Step 3: The Object Relational Designer allows you to visualize data classes in your code. By click Server Explorer to add the table.


Step 4: Connect Database using Server Explorer at Right side or click the link enable text.


Step 5: Connect to Database using Server Explorer.

5 1.Default DataSource is SQL Server(Sqlclient).

2.Type the server name.

3.Select authentication type based on sql server

4.Select the Database.

5.(Optional) Verify the connection using Test Connection.

6.Press OK to make a connection with database and our project.

Step 6: Drag the table from server explorer to Dataclasses file.


Step 7: Double click the Form in solution explorer and place a Datagridview control from Toolbox to form. Write the below code in Form Load event.
LINQ with SELECT Statement and without Where Clause

namespace linq_example
    public partial class Form1 : Form
        public Form1()
        private void Form1_Load(object sender, EventArgs e)
//Create DataContext object by using LINQ file DataClasses.
            DataClasses1DataContext db = new DataClasses1DataContext();
//Linq query using IQueryable for Select statement query.
             IQueryable<tbl_Student> student =
                from stud in db.tbl_Students
                select stud;
//set Datasource to DataGridView for bind the data.
            dataGridView1.DataSource = student;

The Above code used the concept of linq to sql with Select statment and bind the data into datagridview.

For Download Source Code: Click Here