Basic LinQ to SQL Queries and Setup ASP.NET 4.0
See more tutorials in Database. This post has Comments Off on Basic LinQ to SQL Queries and Setup ASP.NET 4.0.
This tutorial will show you how to create a LinQ to SQL Class and attach it to a Gridview. Then we will learn some basic LinQ queries to help you sort through a Database.
Basic LINQ to SQL Queries and Setup ASP.NET 4.0
This tutorial will cover how to setup a LINQ to SQL connection from one of your created SQL databases.
It will then show you how to Query through that Database using LINQ queries in C#. This Application will
be created in Web Forms although it is also possible to use LINQ in an MVC Application.
1. Create the Application and the Database
2. Add the GridView Control and the LINQtoSQL Class
3. Add and setup a Filter Button
Create the Application / Database
1. Start an Empty Web Application in Visual Web Developer 2010
2. Create a new Web Form and call it Default.aspx
3. Create a New SQL Database and call it EmployeeDB.mdf
4. Inside the database add a new Table called with the rows ID, Name, and Email
5. Make the ID tab the Primary Key also make it an identity that auto increments and make sure that
none of the columns can be null then save it as Employees It Should look like this:
6. Now Enter Data into the Database so that it looks like this:
Add a GridView Control and a LINQ to SQL class
1. Add a GridView Control to the Form and name it GridView1
2. Right click on the project and select add -> new item
3. Select LINQ to SQL Class and name it EMPDB.dbml
4. When this open it will be blank, we need to drag out the Employees Table onto it and this will appear
5. Now add a Button to the bottom of the Web form name it btnFilter and label it Filter
6. Now Build the project
7. The next step is to add a LINQDatasource to the form from the Data section of the Toolbox and name it LinqDataSource1
8. Now in the design view of the form click the smart tag on the LINQDatasource and click Configure data source.
9. In the wizard you should be able to select a DataContext named (“ProjectName”.EMPDBDataContext)
10. Finish running through the wizard and the datasource should be setup
11. Now we can click the smart tag on the gridview control and select LinqDataSource1 as the datasource
12. Now Run the project and it should display your table
Add the Filter LINQ Query
1. Double click the Filter Button we made to open up the c# code behind
2. Inside the function paste this code
EMPDBDataContext Context = new EMPDBDataContext();
var customers = from c in Context.Employees
where (c.Name=="Jared Pittman")
GridView1.DataSourceID = "";
GridView1.DataSource = customers;
3. This code will filter through the Database and find all of the entries where the Name Column
is equal to “Jared Pittman” and select that line for a full code breakdown click here
4. Now run the project and click the button it should filter the gridview to only show the one line in which my
name is in the Name slot.
In this tutorial we have created a SQL Database and a LINQ to it allowing for easy manipulation inside the c# code behind.
We also used LINQ commands in order to filter out some results for our gridview.