/*功能:插入一条新记录 *参数: * */ public void EFInsertExam() { ustudent student = new ustudent(); student.sname = "张言成"; student.ssexy = "男"; student.sbdate = Convert.ToDateTime("1990-10-2").ToLongDateString(); student.gid = "02"; student.stele = "678909"; learndb.ustudent.Add(student); try { learndb.SaveChanges(); Console.WriteLine("新同学插入成功"); } catch (DBConcurrencyException dbe) { Console.WriteLine("错误信息:{0}", dbe.Message); } catch (Exception dbe) { Console.WriteLine("错误信息:{0}", dbe.Message); } }
// @see search "EF模型增删改查" private void button_delete_Click(object sender, EventArgs e) { //lqRecCustTransDataContext sail_heEntities db = new sail_heEntities(); ustudent delete = new ustudent(); delete.sid = int.Parse(studentS[listBox_main.SelectedIndex].getStuId()); db.Entry(delete).State = System.Data.Entity.EntityState.Deleted; studentS.RemoveAt(listBox_main.SelectedIndex); Clear(); studentS.ForEach(ele => WriteLine(ele.tabString())); db.SaveChanges(); /* * var q = (from c in db.ustudent * where c.sid == tb_UserInf.CreateUser && c.AutoId == iAid * select c).First(); * db.ustudent.DeleteOnSubmit(q); * db.ustudent.delete * * var q2 = db.ustudent.First(c => c.AutoId == iAid && c.CreateUser == tb_UserInf.CreateUser); * db.ustudent.DeleteOnSubmit(q2); * * @see https://docs.microsoft.com/zh-cn/dotnet/framework/data/adonet/sql/linq/how-to-delete-rows-from-the-database * // Query the database for the rows to be deleted. * var deleteOrderDetails = * from details in db.ustudent * where details.sid == 11000 * select details; * * foreach (var detail in deleteOrderDetails) * { * db.ustudent.DeleteOnSubmit(detail); * } * * try * { * db.SubmitChanges(); * } * catch (Exception e) * { * Console.WriteLine(e); * // Provide for exceptions. * }*/ }
void saveAll(List <StudentInfo> studentS) { studentS.ForEach(ele => { if (ele.getStuId() == null) { using (var context = new sail_heEntities()) { ustudent temp = new ustudent(); temp.gid = ele.ClassId.ToString(); //temp.sid = int.Parse(ele.getStuId()); temp.sname = ele.getName().ToString(); temp.sbdate = ele.BirthDay.ToString(); temp.ssexy = (ele.Gender == EnumGender.MALE.ToString() ? "男" : "女"); temp.stele = ele.Phone.ToString(); context.ustudent.Add(temp); context.SaveChanges(); } } else { using (var context = new sail_heEntities()) { int sid = int.Parse(ele.getStuId()); var stuBuffer = context.ustudent.Where(e => e.sid == sid).First(); ustudent temp = stuBuffer; temp.gid = ele.ClassId.ToString(); temp.sid = int.Parse(ele.getStuId()); temp.sname = ele.getName().ToString(); temp.sbdate = ele.BirthDay.ToString(); temp.ssexy = (ele.Gender == EnumGender.MALE.ToString() ? "男" : "女"); temp.stele = ele.Phone.ToString(); //stuBuffer = temp; context.SaveChanges(); } } }); }