码迷,mamicode.com
首页 > 其他好文 > 详细

ORM简单增删改查

时间:2019-08-27 19:01:25      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:each   command   过程调用   remove   ati   names   isp   efault   com   

技术图片
  1 namespace ORM
  2 {
  3     class Program
  4     {
  5         static void Main(string[] args)
  6         {
  7             //AddPetStore();//添加
  8             UpdatePetStore(1);//修改
  9         }
 10         /// <summary>
 11         /// 添加
 12         /// </summary>
 13         private static void AddPetStore()
 14         {
 15             PetStore store = new PetStore()
 16             {
 17                 Name = "张三",
 18                 PassWord = "123",
 19                 Balance = 1
 20             };
 21             using (CIM_CWEntities entities = new CIM_CWEntities())
 22             {
 23                 entities.PetStore.Add(store);
 24                 if (entities.SaveChanges() > 0)
 25                 {
 26                     Console.WriteLine("添加-成功");
 27                 }
 28             }
 29         }
 30         /// <summary>
 31         /// 修改
 32         /// </summary>
 33         /// <param name="storeId"></param>
 34         private static void UpdatePetStore(int storeId)
 35         {
 36             using (CIM_CWEntities entities = new CIM_CWEntities())
 37             {
 38                 //第一种修改
 39                 //PetStore store = entities.PetStore.FirstOrDefault(i => i.Id == storeId);//lambda表达式:用于简化委托和实例
 40                 //store.Name = "李四1";
 41                 //store.PassWord = "456";
 42 
 43                 //第二种修改
 44                 IQueryable<PetStore> store = entities.PetStore.Where(i => i.Id == storeId);//可以根据多条件修改
 45                 foreach (var item in store)
 46                 {
 47                     item.Name = "王维";
 48                     item.PassWord = "456789";
 49                 }
 50                 if (entities.SaveChanges() > 0)
 51                 {
 52                     Console.WriteLine("修改成功....");
 53                 }
 54             }
 55         }
 56         /// <summary>
 57         /// 批量修改成功
 58         /// </summary>
 59         /// <param name="typeId"></param>
 60         private static void EditPetStore(int typeId)
 61         {
 62             using (CIM_CWEntities entities = new CIM_CWEntities())
 63             {
 64                 var list = entities.PetStore.Where<PetStore>(i => i.Id == typeId).OrderByDescending(p => p.Id).ToList<PetStore>();
 65                 list[0].Name = "李四";
 66                 list[1].PassWord = "456123";
 67                 if (entities.SaveChanges() > 0)
 68                 {
 69                     Console.WriteLine("批量修改成功...");
 70                 }
 71             }
 72         }
 73         /// <summary>
 74         /// 删除
 75         /// </summary>
 76         /// <param name="storeid"></param>
 77         private static void RemovePetSotre(int storeid)
 78         {
 79             using (CIM_CWEntities entities = new CIM_CWEntities())
 80             {
 81                 PetStore petStore = entities.PetStore.Single<PetStore>(i => i.Id == storeid);
 82                 entities.PetStore.Remove(petStore);
 83                 if (entities.SaveChanges() > 0)
 84                 {
 85                     Console.WriteLine("删除成功...");
 86                 }
 87             }
 88         }
 89         /// <summary>
 90         /// linq查询
 91         /// </summary>
 92         private static void GetPetSotre()
 93         {
 94             using (CIM_CWEntities entities = new CIM_CWEntities())
 95             {
 96                 var pr = from a in entities.PetStore
 97                          join b in entities.Pet
 98                          on a.Id equals b.Store_Id
 99                          select new { a.Name, b.Love };
100                 foreach (var item in pr)
101                 {
102                     Console.WriteLine($"名称:{item.Name}-爱好:{item.Love}");
103                 }
104             }
105         }
106         /// <summary>
107         /// 添加方法
108         /// </summary>
109         /// <param name="pet"></param>
110         private static void ExecuteNonQuery(PetStore pet)
111         {
112             using (CIM_CWEntities entities = new CIM_CWEntities())
113             {
114                 SqlParameter[] sqls = {
115                     new SqlParameter("@name",pet.Name),
116                     new SqlParameter ("@pwd",pet.PassWord),
117                     new SqlParameter ("@banl",pet.Balance)
118                 };
119                 int result = entities.Database.ExecuteSqlCommand("insert into PetStore(Name,PassWord,Balance) values(@name,@pwd,@banl)", sqls);
120                 if (result > 0)
121                 {
122                     Console.WriteLine("添加成功...");
123                 }
124             }
125         }
126         private static void ExecuteScalar()
127         {
128             int obj = 0;
129             using (CIM_CWEntities entities = new CIM_CWEntities())
130             {
131                 obj = entities.Database.SqlQuery<int>("select count(1) from PetStore").Single();//SQL所有查询,存储过程调用
132             }
133         }
134         private static void ExecuteReader()
135         {
136             using (CIM_CWEntities entities = new CIM_CWEntities())
137             {
138                 List<PetStore> store = entities.Database.SqlQuery<PetStore>("select * from PetStore").ToList();
139             }
140         }
141         private static void ExecuteProc()
142         {
143             using (CIM_CWEntities entities = new CIM_CWEntities())
144             {
145                 SqlParameter[] sqls =
146                     {
147                     new SqlParameter ("@typeId",1),
148                     new SqlParameter ("@count",System.Data.SqlDbType.Int)//输出参数int
149                 };
150                 sqls[1].Direction = ParameterDirection.Output;
151                 List<PetStore> stores = entities.Database.SqlQuery<PetStore>("proc_GetPetStore @typeId,@count output",sqls).ToList();
152 
153                 int count =(int)sqls[1].Value;
154             }
155         }
156     }
157 }
ORM

 

ORM简单增删改查

标签:each   command   过程调用   remove   ati   names   isp   efault   com   

原文地址:https://www.cnblogs.com/libo-2249/p/11419894.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!