ARTICLE

Using LINQ to SQL Query

Posted by Lakshmanan P Articles | LINQ May 04, 2010
Here in this article, you will get very basic idea of LINQ to SQL Query using code.
Reader Level:

Here you get very basic idea of LINQ to SQL Query using code.

Step by step Linq to SQL

1. Open visual studio 2008 and create a new project --> Web --> ASP.NET Web Application --> LinqBasic(Project Name)

1.gif

2. Once project is load we need to create a database.

3. Right Click the App_data --> Add --> New Item

2.gif

4. Select SQL Server Database --> give the database name as "Employee.mdf" and Click the "Add" button

3.gif

5. Now open the "Server Explorer" go to employee database create a new table with following fields. 

Empid Int,
Employeename nvarchar(50),
Designation nvarchar(50),
DOJ  datetime.
Table name: emp

4.gif

6. Insert the data into "emp" table

5.gif

7. Now we can add a LINQ to SQL Class

8. Right click on "LinqBasic" Application > Add > New Item 

9. Select "LINQ to SQL Classes" Name as "LinqtoEmp.dbml" and click add.

6.gif

10. Now you will get "Object Relational Designer " window

11. Drag and drop the "emp" table from "server explorer" to "Object Relational Designer". It should appear as follows.

7.gif

12. Save all and build.

13. Now open default.aspx and change title tag as you like.

14. Switch to design view. Drag and drop the "GridView" from Toolbox to designer view.

8.gif

15. Right click and select  code view.

16. Paste this code I your page load event.

protected void Page_Load(object sender, EventArgs e)
{
    LinqtoEmpDataContext LinqEmp = new LinqtoEmpDataContext();
    var emplist = from em in LinqEmp.emps select em;
    GridView1.DataSource = emplist;
    GridView1.DataBind();
}

17. Build and run the application.

9.gif

COMMENT USING

Trending up