• 【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_}");
                }
                
            }
  • 相关阅读:
    主机名与域名
    Screen 状态栏配置
    linux 修改时间
    vsftpd2.3.2安装、配置详解
    mysql 二进制日志
    linux下mysql操作的命令
    Apache加载PHP.ini顺序
    页面异步加载javascript文件
    一段实现页面上的图片延时加载的js
    如何在MySql中记录SQL日志
  • 原文地址:https://www.cnblogs.com/tofight/p/15465086.html
Copyright © 2020-2023  润新知