Linq to Db Sp

download Linq to Db Sp

of 17

Transcript of Linq to Db Sp

  • 7/28/2019 Linq to Db Sp

    1/17

    Linq ToADO.Net

  • 7/28/2019 Linq to Db Sp

    2/17

    Linq to

    DBStoreProcedure

  • 7/28/2019 Linq to Db Sp

    3/17

    Linq to DBStored Procedure

    LINQ to ADO.NET deals with data from external sources,basically anything ADO.NET can connect to. Any class that

    implements IEnumerable can be queried with SQO(standard

    query operators).

    Its the simplest process in LINQ.

    First you add LINQ To SQL class and Its extension is .dbml.

    It will ask you to added into App_Code folder in Solution

    Exploler and Click on OK Button.

  • 7/28/2019 Linq to Db Sp

    4/17

  • 7/28/2019 Linq to Db Sp

    5/17

    Linq to DBStored Procedure Now, Drag and drop tables and stored procedures on dbml page.

    Drage the table here.

    Drage stored procedures in this part

    like insert,delete,update,display etc.

  • 7/28/2019 Linq to Db Sp

    6/17

    Linq to DBStored Procedure

    This designed page generates designer.cs class automatically.

    You just use that class to access the functionalty on your aspx.cs page.

    You have to create the object of class in .aspx page.

    With the help of object, access the functions named as stored procedures

    and pass parameters and get records from database. You can do all operations like insert,delete,update,retrive data using

    LINQ.

  • 7/28/2019 Linq to Db Sp

    7/17

    How to retrive data from database?

    private void display()

    {

    EmployeeDataContext objDb = new EmployeeDataContext();

    var q = objDb.selectAllEmployee();

    GridView1.DataSource = q;

    GridView1.DataBind();

    }

    Create object of EmployeeDataContext

    class

  • 7/28/2019 Linq to Db Sp

    8/17

    How to retrive data from database?

    private void display()

    {

    EmployeeDataContext objDb = new EmployeeDataContext();

    var q = objDb.selectAllEmployee();

    GridView1.DataSource = q;

    GridView1.DataBind();

    }

    Create object of EmployeeDataContextclass

    Call the stored procedure

    With help of object

  • 7/28/2019 Linq to Db Sp

    9/17

    How to retrive data from database?

    private void display()

    {

    EmployeeDataContext objDb = new EmployeeDataContext();

    var q = objDb.selectAllEmployee();

    GridView1.DataSource = q;

    GridView1.DataBind();

    }

    Create object of EmployeeDataContextclass

    Call the stored procedure

    With help of objectSet object to

    Gridview

    datasource

  • 7/28/2019 Linq to Db Sp

    10/17

    How to retrive data from database?

    private void display()

    {

    EmployeeDataContext objDb = new EmployeeDataContext();

    var q = objDb.selectAllEmployee();

    GridView1.DataSource = q;

    GridView1.DataBind();

    }

    Create object of EmployeeDataContextclass

    Call the stored procedure

    With help of objectSet object to

    Gridview

    datasourceDataBind() method usedto bind data to gridview

  • 7/28/2019 Linq to Db Sp

    11/17

    How to save data into database?

    protected void btnSave_Click(object sender, EventArgs e)

    {

    using( DataClassesDataContext d1 = new DataClassesDataContext())

    {

    tb_Employee emp = new tb_Employee();

    emp.sEmpName = txtName.Text;

    emp.sAddress = txtAddress.Text;

    emp.iMobileNo =Convert.ToInt64(txtMobile.Text);

    emp.dSalary = Convert.ToDecimal(txtsalary.Text);

    d1.tb_Employees.InsertOnSubmit(emp);

    d1.SubmitChanges();

    };

    }

  • 7/28/2019 Linq to Db Sp

    12/17

    How to Update data into database?

    protected void btnUpdate_Click(object sender, EventArgs e)

    {

    DataClassesDataContext d1=new DataClassesDataContext();

    d1.updateEmployee(Convert.ToInt32(HiddenField1.Value), txtName.Text,txtAddress.Text, Convert.ToInt64(txtMobile.Text),

    Convert.ToDecimal(txtsalary.Text));

    d1.SubmitChanges();

    display();

    }

  • 7/28/2019 Linq to Db Sp

    13/17

    How to Delete data into database?

    protected void btnDelete_Click(object sender, EventArgs e)

    {

    DataClassesDataContext d1 = new DataClassesDataContext();

    d1.deleteEmployee(Convert.ToInt32(txtId.Text));

    d1.SubmitChanges();

    }

  • 7/28/2019 Linq to Db Sp

    14/17

    How to display data in TextBoxs from Gridview

    Step1

    Step2:Field Window will be open and select CommandField from list and click

    on Add Button.Step3: In the command Field, Show SelectButton set true.

    Select Button will be displayed in gridview.

    Step4:In SelectedIndexChanged event, type code

    Step5:In code, GridView1.SelectedDataKey.Value is used to access the EmpIdor value of any column.

    Step6: DataKeyNames property of Gridview should be set With column like

    EmpId.

    Click on arrow

    select Edit Columns

  • 7/28/2019 Linq to Db Sp

    15/17

    How to display data in TextBoxs from Gridview

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

    DataClassesDataContext d1 = new DataClassesDataContext();

    var q=

    d1.selectEmployeeByEmpID(Convert.ToInt32(GridView1.SelectedDataKey.

    Value));

    foreach (var e1 in q)

    {

    HiddenField1.Value = e1.iEmployeeId.ToString();

    txtId.Text = e1.iEmployeeId.ToString();

    txtName.Text = e1.sEmpName;

    txtAddress.Text = e1.sAddress;

    txtMobile.Text = e1.iMobileNo.ToString();

    txtsalary.Text = e1.dSalary.ToString();

    }}

    Call selectEmployeeByEmpIDstored procedure and pass datakey

    to retrive data from database

  • 7/28/2019 Linq to Db Sp

    16/17

    How to display data in TextBoxs from Gridview

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

    DataClassesDataContext d1 = new DataClassesDataContext();

    var q=

    d1.selectEmployeeByEmpID(Convert.ToInt32(GridView1.SelectedDataKey.

    Value));

    foreach (var e1 in q)

    {

    HiddenField1.Value = e1.iEmployeeId.ToString();

    txtId.Text = e1.iEmployeeId.ToString();

    txtName.Text = e1.sEmpName;

    txtAddress.Text = e1.sAddress;

    txtMobile.Text = e1.iMobileNo.ToString();

    txtsalary.Text = e1.dSalary.ToString();

    }}

    Call selectEmployeeByEmpIDstored procedure and pass datakey

    to retrive data from database

    We get values from e1

    object and Assign the

    values to TextBoxes

    Here we set empId

    value in HiddenField1

    control for used any

    purpose .

  • 7/28/2019 Linq to Db Sp

    17/17