Linq无聊练习系列7----Insert,delete,update,at

系统 1518 0

/*********************Insert,delete,update,attach操作练习**********************************/
            //简单插入形式
            var student = new T_Student
            {
                stuNumber = "20091777",
                stuName = "张航",
                stuAge = 23,
                stuSex = "男"
            };

            ctx.T_Student.InsertOnSubmit(student);
            ctx.SubmitChanges();
            //一对多形式,或者多对多形式,需要按照主外健关系依次插入

            var student1 = new T_Student
            {
                stuNumber = "2009177",
                stuName = "谢亮",
                stuAge = 23,
                stuSex = "男"
            };
            var score1 = new T_Score
            {
                stuNumber = student1.stuNumber,
                courceNumber = "001",
                score = 78
            };
            var score2 = new T_Score
            {
                stuNumber = student1.stuNumber,
                courceNumber = "002",
                score = 78
            };

       ctx.T_Student.InsertOnSubmit(student1);
            ctx.T_Score.InsertOnSubmit(score1);
            ctx.T_Score.InsertOnSubmit(score2);
            ctx.SubmitChanges();

            //更爱张航的年龄为21,这种涉及到单项或者更改项很少的,可以用下面的方式更改

            var list = ctx.T_Student.First(s=>s.stuName=="张航");
            list.stuAge = 21;
            ctx.T_Student.InsertOnSubmit(list);
            ctx.SubmitChanges();

            //让学生表中各个学生的年龄加一岁
            var list1 = from t in ctx.T_Student
                         select t;
            foreach (var item in list1)
            {
                item.stuAge = item.stuAge + 1;
            }
            ctx.T_Student.InsertAllOnSubmit(list1);
            ctx.SubmitChanges();

            //删除学号为2091721,课程编号为002的学生
            var list2 = ctx.T_Score.First(s => s.stuNumber == "2091721"&&s.courceNumber=="002");
            ctx.T_Score.DeleteOnSubmit(list2);
            ctx.SubmitChanges();
            //删除学号为黄阳的个人信息及分数信息
            var list3 = ctx.T_Student.First(s=>s.stuName=="黄阳");
            var list4 = from a in ctx.T_Score
                        where a.T_Student.stuName == "黄阳"
                        select a;
            foreach (var item in list4)
            {
                ctx.T_Score.DeleteOnSubmit(item);
            }
            ctx.T_Student.DeleteOnSubmit(list3);
            ctx.SubmitChanges();
            //还可以推理删除,推理删除需要用Remove方法。由于表的设计不是很完善,这里就不给演示了
            
            //对于在不同的上下文间,需要使用attach方法进行更新,如在一个上下文中查出一个叫黄阳的学生和它的各科成绩,在另一个上下文中将其年龄改为19,将其所有学科科目成绩改为33分
            T_Student student2;
            List<T_Score> listScore = new List<T_Score>();
            using (DB_StudentDataContext ctx1 = new DB_StudentDataContext())
            {
                var stu = ctx1.T_Student.First(s=>s.stuName=="黄阳");
                student2 = new T_Student {
                    stuNumber=stu.stuNumber,
                    stuName=stu.stuName,
                    stuAge=stu.stuAge,
                    stuSex=stu.stuSex
                };

                var scores = from k in ctx1.T_Score
                             where k.T_Student.stuName == "黄阳"
                             select k;
                foreach (var item in scores)
                {
                    listScore.Add(item);
                }
            }

            ctx.T_Student.Attach(student2);
            student.stuSex = "19";

            ctx.T_Score.AttachAll(listScore);
            foreach (var item in listScore)
            {
                item.score = 33;
            }

            ctx.SubmitChanges();

            //使用attach进行删除,更改,插入

            T_Student student3 = null;
            using (DB_StudentDataContext ctx2 = new DB_StudentDataContext())
            {
                DataLoadOptions dlo = new DataLoadOptions();
                dlo.LoadWith<T_Student>(s => s.T_Score);
                ctx2.DeferredLoadingEnabled = false;
                student3 = ctx2.T_Student.First(s=>s.stuName=="黄阳");
            }
            T_Score scores1 = student3.T_Score.First();
            T_Score scores2 = student3.T_Score.First(s => Convert.ToInt32(s.stuNumber) > Convert.ToInt32(scores1.stuNumber));

            using (DB_StudentDataContext ctx3 = new DB_StudentDataContext())
            {
                ctx3.T_Student.Attach(student3);
                ctx3.T_Score.AttachAll(student3.T_Score.ToList());
                scores1.score = 80;
                student3.T_Score.Remove(scores2);

                T_Score score3 = new T_Score {
                    stuNumber = "2009177",
                    courceNumber = "003",
                    score=88
                };
                student3.T_Score.Add(score3);

                ctx3.SubmitChanges();

Linq无聊练习系列7----Insert,delete,update,attach操作练习


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论