EF简单查询

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


            }
        }
    }

   
}
View Code

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值