Friday, November 12, 2010

LINQ To SQL


LINQ to SQL provides a runtime infrastructure for managing relational data as objects without losing the ability to query. Your application is free to manipulate the objects while LINQ to SQL stays in the background tracking your changes automatically.

Here I have created one small database application with basic operations using c# and I have implemented Linq to sql concept.
Step 1: Create one windows application like this






Step 2: Create a database and table with this structure.

















Step 3: Add new dbml file(LINQ to SQL Classses).




 













Step 4: Add new Data Connection in Server Explorer window.

Step 5: Drag the database tables into dbml file



















Once you dragged the tables you can see the classes are created in designer.cs (class file). Each table act as one class and the columns act as properties.

Now you can write the code for buttons
To Add New Record



private void btnAdd_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                //Assigment of values to columns
                employee.Emp_ID = Convert.ToInt32(txtEmpId.Text);
                employee.Emp_Name = txtname.Text;
                employee.Address = txtAddress.Text;
                employee.Contactno = txtContact.Text;

                //Adding the instance to the database
                db.EmployeeTables.InsertOnSubmit(employee);
                db.SubmitChanges();


                //To retrieve the employee details from database
                List lstEmployee = new List();
                lstEmployee = (from emp in db.EmployeeTables
                               select emp).ToList();

                //Bind the data to gridview
                dataGridView2.DataSource = lstEmployee;

                MessageBox.Show("Employee Details Added Successfully");
            }
            catch (Exception)
            {
                MessageBox.Show("Error on adding Employee details");
            }
        }

To Updated Existing Record

private void btnUpdate_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                int id = Convert.ToInt32(txtEmpId.Text);

                //fetching the record which is going to update
                employee = (from emp in db.EmployeeTables
                            where emp.Emp_ID == id
                            select emp).SingleOrDefault();

                //Assigment of new values
                employee.Emp_Name = txtname.Text;
                employee.Address = txtAddress.Text;
                employee.Contactno = txtContact.Text;

                //Updating in database
                db.SubmitChanges();

                MessageBox.Show("Employee Details Updated Successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error on Updating Employee Details");
            }

        }


To Delete a Record

private void btnDelete_Click(object sender, EventArgs e)
        {
            //Creating instance of dbml class
            Employee_DataClassesDataContext db = new Employee_DataClassesDataContext();

            try
            {
                //Createing instance of table
                EmployeeTable employee = new EmployeeTable();

                int id = Convert.ToInt32(txtEmpId.Text);

                //fetching the record which is going to update
                employee = (from emp in db.EmployeeTables
                            where emp.Emp_ID == id
                            select emp).SingleOrDefault();

                if (MessageBox.Show("Are you sure to Delete?", "Confirm Box", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    //Delete the record
                    db.EmployeeTables.DeleteOnSubmit(employee);

                    //Updating in database
                    db.SubmitChanges();
                }

                //To retrieve the employee details from database
                List lstEmployee = new List();
                lstEmployee = (from emp in db.EmployeeTables
                               select emp).ToList();

                //Bind the data to gridview
                dataGridView2.DataSource = lstEmployee;

            }
            catch (Exception Ex)
            {

            }
        }

Its a simple example i wrote but. LINQ to SQL contains lots of interesting thinks lets see in other article 
Thanks for reading this article

No comments:

Post a Comment