Introduction:
In this article,we are going to discuss about how to use LINQ against SQL datasource.
Main:
Linq-to-SQL is an Object/relational (O/R) mapping implementation that allows developers and architectures to model a relation database using .NET classes.
Operations on classes are translated to commands for Crate/Read/Update/Delete (CRUD) statements that modify the underlying database through a regular connection.
LINQ_To_SQL.aspx,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "<a href="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</a>">
<html xmlns="<a href="http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml</a>">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.newStyle1
{
position: absolute;
}
#form1
{
height: 604px;
}
</style>
</head>
<body>
<form id="form1" runat="server" style="position: absolute">
<div class="newStyle1">
<asp:Button ID="Button1" runat="server" Font-Size="Large" Height="67px"
onclick="Button1_Click" Text="Get using Linq code" Width="195px" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="deptno"
DataSourceID="LinqDataSource1">
<Columns>
<asp:BoundField DataField="deptno" HeaderText="deptno" ReadOnly="True"
SortExpression="deptno" />
<asp:BoundField DataField="dname" HeaderText="dname" SortExpression="dname" />
<asp:BoundField DataField="loc" HeaderText="loc" SortExpression="loc" />
</Columns>
</asp:GridView>
<asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<br />
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="DeptClassDataContext" TableName="depts"
Where="deptno &gt; @deptno">
<WhereParameters>
<asp:Parameter DefaultValue="10" Name="deptno" Type="Int32" />
</WhereParameters>
</asp:LinqDataSource>
</div>
</form>
</body>
</html> |
LINQ_To_SQL.aspx.cs,
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Linq_To_Sql : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { DeptClassDataContext dc = new DeptClassDataContext(); var data = from c in dc.depts where c.loc=="hyd" orderby c.dname select c; GridView2.DataSource = data; GridView2.DataBind(); } } |
Conclusion:
Hope this helps,
Happy Coding.