For Using Linq to Sql In VS2008
1.) Add New Item from Solution Explorer
2.) Select LinqtoSql Class from template ,Name it
3.) Above will generate .dbml file
4.) From server Explorer Add New Connection
5.) Drag the tables(those you want to interact from the application ) from Serverexplorer to .dbml page
6.) Save .dbml File
7.) LINQ Mapping will be automatically done with above steps
Now you can use the LINQ for communicating with the above tables
Now in Simpler terms
.dbml file class will be the database Class
Tables which u dragged on to .dbml file will be Table Classes
columns in above tables will be Properties of the above classes
Below is the Example of Selecting Records From DB Using LINQ
1.) Add New Item from Solution Explorer
2.) Select LinqtoSql Class from template ,Name it
3.) Above will generate .dbml file
4.) From server Explorer Add New Connection
5.) Drag the tables(those you want to interact from the application ) from Serverexplorer to .dbml page
6.) Save .dbml File
7.) LINQ Mapping will be automatically done with above steps
Now you can use the LINQ for communicating with the above tables
Now in Simpler terms
.dbml file class will be the database Class
Tables which u dragged on to .dbml file will be Table Classes
columns in above tables will be Properties of the above classes
Below is the Example of Selecting Records From DB Using LINQ
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//autogenerated DataClassesDataContext's object when dragged Tables on DataClasses.dbml
DataClassesDataContext db = new DataClassesDataContext();
var results = from o in db.Orders
select o;
GridView1.DataSource = results;
GridView1.DataBind();
}
}
Orders is table in my DB
Below is the o/p
No comments:
Post a Comment