ef5 数据库操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace efdemo
{
    public class UserDal
    {
        static ZDEntities zz = new ZDEntities();
        /// <summary>
        /// lq查询说有
        /// </summary>
        public static void QueryListLq()
        {
            //var info = from p in zz.tests where p.ID >= 2 orderby p.ID descending select p;
            var info = from p in zz.tests orderby p.ID descending select p;
            foreach (var item in info)
            {
                Console.WriteLine("ID:" + item.ID + " " + "名称:" + item.name + " " + "年里:" + item.age);
            }
        }
        /// <summary>
        /// lmda查询
        /// </summary>
        public static void QueryListLmad()
        {
            //var info2 = zz.tests.Where<test>(p => p.ID >= 10).OrderByDescending(p => p.ID);
            var info2 = zz.tests.ToList();
            foreach (var item in info2)
            {
                Console.WriteLine("ID:" + item.ID + " " + "名称:" + item.name + " " + "年里:" + item.age);
            }
        }
        /// <summary>
        /// 查询所有
        /// </summary>
        public static void QuerylistSql()
        {
            string sql = "select * from test ORDER BY ID DESC";
            var list = zz.Database.SqlQuery<test>(sql);
            foreach (var item in list)
            {
                Console.WriteLine("ID:" + item.ID + " " + "名称:" + item.name + " " + "年里:" + item.age);
            }
        }
        public static void  Query()
        {
           List<test> list =zz.tests.Where(u => u.ID == 2).ToList();
           string b = null;
           string c = null;
           string d = null;
           foreach(var a in list)
           {
               b = a.ID.ToString();
               c = a.name.ToString();
               d = a.age.ToString();
           }
           Console.WriteLine("id="+b);
           Console.WriteLine("name="+c);
           Console.WriteLine("age="+d);
        }
        /// <summary>
        /// 修改
        /// </summary>
        public static void edit()
        {
            var  test1 = new test()
            {
                ID = 5,
                name = "zhangsan"
            };
            zz.tests.Attach(test1);
            zz.Entry<test>(test1).Property<string>(v => v.name).IsModified = true;            
            //Query();
            //QuerylistSql();
            //QueryListLq();
            QueryListLmad();
        }
        /// <summary>
        /// 删除
        /// </summary>
        public static void delte()
        {
            test stu = new test()
            {
               ID = 2
            };
            var list = zz.tests.Where(u => u.ID == 1);
            if (list.Count() > 0 && list != null)
            {
                foreach (var ll in list)
                {
                    zz.tests.Remove(ll);
                }
            }
            zz.SaveChanges();
        }
        public static void Add()
        {        
            try
            {
                test user = new test()
                {                   
                    name="111",
                    age=10,
                };
                //ZDEntities zz = new ZDEntities();                
                zz.tests.Add(user);
                zz.SaveChanges();//保存到数据库               
            }
            catch(Exception ex)
            {             
                throw new Exception("错误", ex);
            }
        }
    }
}
class Program
    {
        static void Main(string[] args)
        {
            //UserDal.Add();
            //Console.WriteLine("保存成功");
            //UserDal.Query();
            UserDal.edit();
            Console.WriteLine("修改成功");
            Console.Read();
           
        }
    }

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值