Step to work with LINQ to SQL or OR-Mapping tool to perform CRUD operation.
Step1: Creating DBML file
Create Windows Forms Application,
Go to Solution Explorer, click right mouse button, Add, then click New Item.
Select data from Visual C# installed item then select LINQ to SQL classes Template and type the file name Employee.dbml. After click Add,
This will create Employee.dbml file as (shown in red Box).
Step 2: Connecting to database,
Go to Server Explorer, select data connection and click right mouse button. After that click on Add Connection, then click Change, select Microsoft SQL Server data service and data provider as .NET framework data provider for SQL server. Click OK, type the server name, select Use SQL Server Authentication, type username password or (also can be select Windows Authentication type) activate the check box save my password and select the database name EmployeeDetails. Now click OK.
Double click on tables, drag the table Emp on the left side part of DBML file. This will create class with name Emp and properties as the same name as table.
Design the windows form and write the following code,
- using System;
- usingSystem.Collections.Generic;
- usingSystem.ComponentModel;
- usingSystem.Data;
- usingSystem.Drawing;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Threading.Tasks;
- usingSystem.Windows.Forms;
-
- namespaceWindowsApplicationLTSQL
- {
- publicpartialclassForm1: Form
- {
- EmployeeDataContextobjdc = newEmployeeDataContext();
- public Form1()
- {
- InitializeComponent();
- }
- privatevoid Form1_Load(object sender, EventArgs e)
- {
-
- }
- privatevoidbtnInsert_Click(object sender, EventArgs e)
- {
- Employee obje = new Employee()
- Obje.eid = Convert.ToInt32(txtEid.Text);
- Obje.ename = txtEName.Text;
- Obje.edesignation = txtEdesignation.Text;
- Obje.esalary = Convert.ToDouble(txtsalary.Text);
- Objdc.Employee.InsertOnSubmit();
- Objdc.SubmitChanges();
- MessageBox.Show("Data is Inserted to Database ");
- }
- privatevoidbtnUpdate_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
- rec.EName = txtEName.Text;
- rec.EDesignation = txtDesignation.Text;
- rec.ESalary = Convert.ToInt32(txtESalary.Text);
- objdc.SubmitChanges();
- MessageBox.Show("Record is Updated");
- }
-
- privatevoidbtnDelete_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
- objdc.Employee.DeleteOnSubmit();
- objdc.SubmitChanges();
- MessageBox.Show("Record is Deleted");
- }
-
- privatevoidbtnFind_Click(object sender, EventArgs e)
- {
- var rec(From Empinobjdc.Employee where Emp.Eid = Convert.ToInt32(txtEid.Text) Select Emp).First();
- txtEName.Text = rec.EName;
- txtDesignation.Text = rec.EDesignation;
- txtESalary.Text = rec.ESalary.ToString();
- }
-
- }
- }
Recap: For working with LINQ to SQL to perform the CRUD operation.
Step 1: Creating dbml file.
Step 2: Connecting to database.