上一篇博文新建了一个项目以及数据模型文件,本次就接着上次的讲解继续往下深入,学习一下如何使用Linq进行一个基本的增删改查。
新建一个Web页面Action.aspx,在该页面中添加一些文本框以及按钮,具体操作步骤不再讲述,效果如下所示:
protected void btnSelect_Click(object sender, EventArgs e) //查询 { string information = ""; var result = from c in dbcontext.Student1 select c;//简单的无where的查询 foreach (var item in result) information = "姓名:" + item.Sname + "\n" + "学号:" + item.Sno + "\n" + "性别:" + item.Ssex + "\n"; Label1.Text = information; // var result1 = from c in dbcontext.Student1 where c.Sno = "110" select c; // var result1 = from c in dbcontext.Student1 where c.Sno = TextBox1.Text select c; }
使用LINQPad工具可以查看Linq查询语言对应的具体的数据库SQL脚本语言以及Lamada表达式(LINQPad工具对于学习Linq语言非常好用,具体使用方式可以查看我的另一篇博文)。
protected void btnAdd_Click(object sender, EventArgs e) //添加 { Student1 str = new Student1(); //新建实例 str.Sno = Txtno.Text; //为该实例的各个属性赋值 str.Sname = Txtname.Text; str.Ssex = Convert.ToInt16(Txtsex.Text); str.Sphone = Txtphone.Text; str.Saddress = Txtaddress.Text; dbcontext.Student1.AddObject(str); //添加到对象上下文中 dbcontext.SaveChanges(); //保存修改 //dbcontext.Student1.Attach(str); //dbcontext.ObjectStateManager.ChangeObjectState(str,EntityState.Added); //dbcontext.SaveChanges(); }
(2)效果图
protected void btnUpdate_Click(object sender, EventArgs e) //修改方法1 { Student1 str = new Student1();//首先创建一个实体类 str.Sno = Txtno.Text; str.Sname = Txtname.Text; str.Ssex = Convert.ToInt16(Txtsex.Text); str.Sphone = Txtphone.Text; str.Saddress = Txtaddress.Text; <span style="color:#ff0000;">dbcontext.Student1.Attach(str); //将该实体类附加到对象上下文中</span> dbcontext.ObjectStateManager.ChangeObjectState(str, EntityState.Modified);//修改对应实体的的状态 dbcontext.SaveChanges(); //保存修改
<span style="font-family: Arial, Helvetica, sans-serif;"> }</span>
<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"> protected void btnUpdate_Click(object sender, EventArgs e) //修改方法2 { Student1 str = new Student1();//首先创建一个实体类 str = (from c in dbcontext.Student1 where c.Sno == Txtno.Text select c).SingleOrDefault<Student1>(); str.Sno = Txtno.Text; str.Sname = Txtname.Text; str.Ssex = Convert.ToInt16(Txtsex.Text); str.Sphone = Txtphone.Text; str.Saddress = Txtaddress.Text; <span style="color:#ff0000;"> //dbcontext.Student1.Attach(str); //将该实体类附加到对象上下文中</span> dbcontext.ObjectStateManager.ChangeObjectState(str, EntityState.Modified);//修改对应实体的的状态 dbcontext.SaveChanges(); //保存修改 }
(2)效果图
protected void btnDelete_Click(object sender, EventArgs e) //删除 { //var result=from c in dbcontext.Student1 ; Student1 st1 = new Student1();//首先创建一个实体类 st1.Sno = Txtno.Text; <span style="color:#ff0000;">dbcontext.Student1.Attach(st1); //将该实体类附加到对象上下文中</span> dbcontext.ObjectStateManager.ChangeObjectState(st1, EntityState.Deleted);//修改对应实体的的状态 dbcontext.SaveChanges(); //保存修改 }
protected void btnDelete_Click(object sender, EventArgs e) //删除 { Student1 st1 = (from c in dbcontext.Student1 where c.Sno == Txtno.Text select c).SingleOrDefault<Student1>(); dbcontext.ObjectStateManager.ChangeObjectState(st1, EntityState.Deleted);//修改对应实体的的状态 dbcontext.SaveChanges(); }
(2)效果图
Linq语句不容易编写,想要编写正确的Linq语句可以借助LINQPad工具
源代码:EFDBFirst设计实例