【sqlite】3.ADO.NET实体数据模型增删改查
2021/10/26 19:14:29
本文主要是介绍【sqlite】3.ADO.NET实体数据模型增删改查,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
mainEntities2 dbcontext = new mainEntities2(); private void btnAdd_Click(object sender, RoutedEventArgs e) { Students stu = new Students(); stu.StudentName_ = "李四"; stu.StudentSex_ = "男"; stu.StudentAge_ = 19; stu.StudentProvince_ = "上海"; stu.StudentPhone_ = "17468523001"; dbcontext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Added; int i = dbcontext.SaveChanges(); string str = i == 1 ? "添加成功" : "添加失败"; MessageBox.Show(str); } private void btnDel_Click(object sender, RoutedEventArgs e) { Students stu = new Students(); stu.StudentNO_ = 1; dbcontext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Deleted; int i = dbcontext.SaveChanges(); string str = i == 1 ? "删除成功" : "删除失败"; MessageBox.Show(str); } private void btnUpdate_Click(object sender, RoutedEventArgs e) { //修改整个 //Students stu = new Students(); //stu.StudentNO_ = 2; //stu.StudentName_ = "李四"; //stu.StudentSex_ = "男"; //stu.StudentAge_ = 19; //stu.StudentProvince_ = "上海"; //stu.StudentPhone_ = "17468523001"; //dbcontext.Entry<Students>(stu).State = System.Data.Entity.EntityState.Added; //int i = dbcontext.SaveChanges(); //string str = i == 1 ? "修改成功" : "修改失败"; //MessageBox.Show(str); //修改某个属性 Students stu = new Students(); stu.StudentNO_ = 2; stu.StudentName_ = "王霸26"; dbcontext.Students.Attach(stu);//必须使用attach //dbcontext.Entry<Students>(stu).Property<string>(s => s.StudentName_).IsModified = true; dbcontext.Entry<Students>(stu).Property("StudentName_").IsModified = true; int i = dbcontext.SaveChanges(); string str = i == 1 ? "修改成功" : "修改失败"; MessageBox.Show(str); } private void btnSelect_Click(object sender, RoutedEventArgs e) { //便利所有 foreach (var item in dbcontext.Students) { Console.WriteLine($"学号:{item.StudentNO_} 姓名:{item.StudentName_}"); } //查询整个表女生的信息 var temp = from s in dbcontext.Students where s.StudentSex_ == "女" select s; foreach (var item in temp) { Console.WriteLine($"学号:{item.StudentNO_} 姓名:{item.StudentName_} 性别:{item.StudentSex_}"); } }
这篇关于【sqlite】3.ADO.NET实体数据模型增删改查的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#