Wednesday 8 May 2013

LINQ to SQL (Example)

Example : Add a new form in project and design it as following.



Output :




Coding:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LinqProject1
{
   public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        CS11DBDataContext dc;
        List cust;
        int rno = 0;
        private void Form2_Load(object sender, EventArgs e)
        {
            dc = new CS11DBDataContext();
            cust = dc.GetTable().ToList();
            ShowData();
        }
        private void ShowData()
        {
            txtCustID.Text = cust[rno].CustId.ToString();
            txtCname.Text = cust[rno].Cname.ToString();
            txtCity.Text = cust[rno].City.ToString();
            txtBalance.Text = cust[rno].Balance.ToString();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (rno0)
            {
                rno -= 1;
                ShowData();
            }
            else
            {
                MessageBox.Show("First record of the table", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        } 
    } 
}

LINQ to SQL

Microsoft has provided LINQ as a lightweight facade that provides a strongly typed interface to the underlying data stores. LINQ provides the means for developers to stay within the coding environment they are used to and access the underlying data as objects that work with the IDE, intellisense, and even debugging. With LINQ the queries you create now become first-class citizens within the .Net framework alongside everything else you are used to. When you work with queries for the data store you are working with, you will quickly realize that they now work and behave as if they are types in the system.

LINQ to SQL in particular is a means to have a strongly typed interface against a SQL Server database. You will find the approach that LINQ to SQL provides is by far the easiest approach to the querying SQL Server available at the momoent. It is important to remember that LINQ to SQL is not only about querying data, but you also are able to perform Insert/Update/Delete statements that you need to perform which are known as CRUD operations (Create/Read/Update/Delete). Visual Studio comes into strong play with LINQ to SQL in that you will find an extensive user interface that allows you to design the LINQ to SQL classes you will work with.

Example : 

Place a DataGridView control on the first Form of project, change the name of DataGridView as gdView and write the following code:

Output:

 

Coding :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Linq;

namespace LinqProject1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            CS11DBDataContext dc = new CS11DBDataContext();
            Table tab = dc.GetTable();
            dgview.DataSource = tab;
        }  
      
    }
}