1. student is a table in the database. The student ID (stu) and name (xingming) are two fields in the table, and the student ID is the primary key.
In the Page_Load event of the page, query the data in the table through Linq:
Protected void Page_Load (object sender, EventArgs e)
{
StudentDataClassesDataContext context = new StudentDataClassesDataContext ();
Table List = context. GetTable ();
Var query = from student in list select student;
Foreach (var student in query)
{
Response. Write (student. xuehao + "," + student. xingming +"
");
}
}
On the page, add data to the database through Linq:
Protected void btn_Add_Click (object sender, EventArgs e)
{
Student stu = new student ();
Stu. xuehao = txt_XueHao.Text;
Stu. xingming = txt_XingMing.Text;
StudentDataClassesDataContext context = new StudentDataClassesDataContext ();
Context. student. InsertOnSubmit (stu );
Context. SubmitChanges ();
// Refresh the data
Table List = context. GetTable ();
Var query = from student in list select student;
Foreach (var student in query)
{
Response. Write (student. xuehao + "," + student. xingming +"
");
}
}
Modify data through Linq:
Protected void btn_Modify_Click (object sender, EventArgs e)
{
StudentDataClassesDataContext context = new StudentDataClassesDataContext ();
Var query = from student in context. student where student. xuehao = "001" select student;
Foreach (var student in query)
{
Student. xingming = "";
}
Context. SubmitChanges ();
}
Delete data through Linq:
Protected void btn_Delete_Click (object sender, EventArgs e)
{
StudentDataClassesDataContext context = new StudentDataClassesDataContext ();
Var query = from student in context. student where student. xuehao = "001" select student;
Foreach (var student in query)
{
Context. student. DeleteOnSubmit (student );
}
Context. SubmitChanges ();
}