Hero is coming back

风吹呀吹

风会指引你前进的方向

c# LINQ CMDB(SQL)

如果数据库字段发生更改需要重新保存最新表到dbml

新增

            //1.Create DB context
            AlexDBDataContext db = new AlexDBDataContext();
            //2.Create table object
            myUser myUser = new myUser
            {
                ID = 5,
                Name = "Robin",
                Password = "143"
            };
            //Add new object to myuser collection
            db.myUser.InsertOnSubmit(myUser);
            //submit change to database
            try
            {
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

更新

 

            //1.Create DB context
            AlexDBDataContext db = new AlexDBDataContext();
            var q = from user in db.myUser
                    where user.ID == 5
                    select user;
            //you want to change
            foreach (var item in q)
            {
                item.Name = "SuperRonbin";
            }
            try
            {
                //submit to database
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

 

删除

 

            //1.Create DB context
            AlexDBDataContext db = new AlexDBDataContext();
            var q = from user in db.myUser
                    where user.ID == 5
                    select user;
            //you want to delete
            foreach (var item in q)
            {
                db.myUser.DeleteOnSubmit(item);
            }
            try
            {
                //submit to database
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

 

posted @ 2017-11-28 07:55  风吹呀吹  阅读(249)  评论(0编辑  收藏  举报