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); }