Monday, 13 August 2012

What is LINQ ( Insert,Delete,Update,Select,StartWith,EndWith,Contains Keyword using dbml class)


What is Linq:

 Linq  stand  for  Language Integrated Query (LINQ) is  new component of .NET Framework 3.5. LINQ is the most important new feature of C#.The basic function of Linq is to add native  data  querying capabilities to .NET Framework using syntax similar to SQL .It  is a  provider like for JDBC /ODBC for Java…………….



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.

 Version of LINQ:

o    LINQ to Object
o    LINQ  to  Entity
o    LINQ  to Sql
o    LINQ to Dataset
o    LINQ  to XML


           How  to  used ....................................................LINQ  to SQL


Frist we  create  Database  which name is   ForLinq

Create DataBase ForLinq
                          
After than  we create table  Student  in  Databae ForLinq

use ForLinq

create table Student(Srno int primary key identity(1,1),Name nvarchar(50),FatherName nvarchar(50),Age int,Gender nvarchar(50),Address nvarchar(200))



then we take a Webpage  for  connectivity with data base  using Linq………………….(Linq to Sql).then after  we  take  dbml classes  in our  website …………..


after  taking the dbml class the  dbml class goes to App_Code  folder in Website



dbml class divides itself in two panes  in the first pane  we  drag and  drop the tables and second  pane is used to  put  the  function and  store procedure  ,in the  dbml class we  drag the  tables from Server Explorer  






after than we  create code  in Default.cs  page…………………….



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// using  this  namespace  for  linq  connectivity
using System.Linq;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

//  create  object for  dbml  class ……………………………………

    DataClassesDataContext dd = new DataClassesDataContext();


//Codinng for  save  data  in Table Student…………………………………

    protected void BInsert_Click(object sender, EventArgs e)
    {

        Student t = new Student
        {
            Name = txtName.Text,
            FatherName = txtFatherName.Text,
            Age = Convert.ToInt32(txtAge.Text),
            Gender = RbGender.SelectedValue,
            Address = txtAddress.Text
        };

        dd.Students.InsertOnSubmit(t);
        dd.SubmitChanges();
        LMessage.Visible = true;
        LMessage.Text = "Add Successfully";
    }
   


//Codinng for  select all  data  from Table Student and show in Gridview………………………………

    protected void BSelect_Click(object sender, EventArgs e)
    {
        var v = from m in dd.Students select m;
        GridView1.DataSource = v;
        GridView1.DataBind();
    }


//Codinng for  delete  data  from Table Student by  SrNo…………which is  primary key in table

    protected void BDelete_Click(object sender, EventArgs e)
    {
        var v = from m in dd.Students where m.Srno == Convert.ToInt32(txtSrNo.Text) select m;
        if (v != null)
        {

            dd.Students.DeleteAllOnSubmit(v);
            dd.SubmitChanges();
        }
        LMessage.Visible = true;
        LMessage.Text = "Delete Successfully";
    }


//Codinng for  update  data  from Table Student by  SrNo…………which is  primary key in table

    protected void Bupdate_Click(object sender, EventArgs e)
    {
        var v =( from m in dd.Students where m.Srno == Convert.ToInt32(txtSrNo.Text) select m).FirstOrDefault();
        if(v!=null)

        {

            v.Name = txtName.Text;
            v.FatherName=txtFatherName.Text;
            v.Age = Convert.ToInt32(txtAge.Text);
            v.Gender = RbGender.SelectedValue;
            v.Address = txtAddress.Text;

           }
        dd.SubmitChanges();

        LMessage.Visible = true;
        LMessage.Text = "Update Data Successfully";

    }

//Codinng for  select  data  from Table Student by  SrNo…………which is  primary key in table and  data show  it own textbox……………………….

    protected void BselectByid_Click(object sender, EventArgs e)
    {
        var v = ( from m in dd.Students where m.Srno == Convert.ToInt32(txtSrNo.Text) select m).FirstOrDefault();
        if(v!= null)
        {
            txtName.Text = v.Name;
            txtFatherName.Text = v.FatherName;
            txtAge.Text = v.Age.ToString();
            txtAddress.Text = v.Address;



        }
    }
}

 Then  after  we  can insert ,delete,update, retrieve record  fro table


 For Insert  data…………………….



For select Data from Table






   if  you  want  to use  Like query   in  LINQ then  it  will replace  by  some  keyword

            1.   StartWith
            2.   EndWith
            3.   Contains

//Codinng for  select  data  from Table Student by  Name…………which is  StartWith ....

protected void BStartWith_Click(object sender, EventArgs e)
    {
        var v = from m in dd.Students where m.Name.StartsWith(txtserch.Text) select m;
        GridView1.DataSource = v;
        GridView1.DataBind();

               
    
    }

//Codinng for  select  data  from Table Student by  Name…………which is  EndWith.......
    protected void BEndWith_Click(object sender, EventArgs e)
    {
        var v = from m in dd.Students where m.Name.EndsWith(txtserch.Text) select m;
        GridView1.DataSource = v;
        GridView1.DataBind();

    }


//Codinng for  select  data  from Table Student by  Name…………which is  Contains.......

    protected void BContains_Click(object sender, EventArgs e)
    {

        var v = from m in dd.Students where m.Name.Contains(txtserch.Text) select m;
        GridView1.DataSource = v;
        GridView1.DataBind();
    }





Using Group By keyword in LINQ to Sql

How to use sql function with Linq to Sql

How to execute Sql Raw Query in Linq C# .Net

Basics of LINQ Join with Lamda Expressions(Inner Join with Linq)

Left Right Full Outer Join and Cross join with Linq to Sql

Connectivity Linq with Sql Server 2008 without dbml context

                                                                                                                                       


1 comments: