using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.Objects; using System.Data.EntityClient; using System.Collections; namespace EFDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ArrayList lst = new ArrayList(); using (NhibernateDBEntities ndb = new NhibernateDBEntities()) { Teacher teachs = ndb.Teacher.Where(it=>it.Name=="Anby"); ndb.DeleteObject(teachs); ndb.SaveChanges(); Teacher teach2 = ndb.Teacher.Where(it => it.Name == "Anby"); } } private static void Updateo() { ArrayList lst = new ArrayList(); using (NhibernateDBEntities ndb = new NhibernateDBEntities()) { Teacher teachs = ndb.Teacher.FirstOrDefault(); teachs.Age = 22; ndb.SaveChanges(); Teacher teach2 = ndb.Teacher.FirstOrDefault(); } } private static void Add() { ArrayList lst = new ArrayList(); Teacher teacher = new Teacher() { Name = "Anby", Age = 18, School_ID = 1, Sex = "男" }; using (NhibernateDBEntities ndb = new NhibernateDBEntities()) { ndb.AddToTeacher(teacher); ndb.SaveChanges(); ObjectResult<Teacher> teachs = ndb.CreateQuery<Teacher>("Select VALUE c from NhibernateDBEntities.Teacher as c").Execute(MergeOption.NoTracking); foreach (Teacher item in teachs) { lst.Add(item); } } } private static void ParamEsql(ArrayList lst, NhibernateDBEntities ndb) { //带参数的查询 string strsql = "Select VALUE c from NhibernateDBEntities.School as c " + " where c.id=@schoolid"; ObjectQuery<School> query = ndb.CreateQuery<School>(strsql); //增加查询参数 query.Parameters.Add(new ObjectParameter("schoolid", 1)); ObjectResult<School> result = query.Execute(MergeOption.NoTracking); foreach (School c in result) lst.Add(c); } private void BaseQuery() { List<string> lst = new List<string>(); using (NhibernateDBEntities ndb = new NhibernateDBEntities()) { //查询所有 foreach (var teacher in ndb.Teacher) { lst.Add(teacher.Name); } comboBox1.DataSource = lst; //单个数据查询 Teacher tea1 = ndb.Teacher.Where("it.Name='Jessica'").First(); Teacher tea2 = ndb.Teacher.First<Teacher>(it => it.Name == "Jessica"); Teacher tea3 = (from c in ndb.Teacher where c.Name == "Jessica" select c).First(); //多表连接查询 School sch = ndb.School.First<School>(s => s.id == 1); foreach (Teacher item in sch.Teacher) { lst.Add(item.Name); } } } } }