码迷,mamicode.com
首页 > Windows程序 > 详细

C# Redis实战(五)

时间:2016-11-20 19:19:45      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:new   using   art   white   https   target   pac   long   user   

五、删除数据

C# Redis实战(四)中讲述了如何在Redis中写入key-value型数据,本篇将讲述如何删除Redis中数据。

 
1、void Delete(T entity);删除函数的运用
[csharp] view plain copy 技术分享技术分享
  1. using (var redisClient = RedisManager.GetClient())  
  2. {  
  3.     var user = redisClient.GetTypedClient<User>();  
  4.     var newUser = new User  
  5.     {  
  6.         Id = user.GetAll().Count,  
  7.         Name = txtName.Text,  
  8.         Job = new Job { Position = txtPosition.Text }  
  9.     };  
  10.     user.Delete(newUser);  
  11.   
  12. }  

以上代码直接删除了最后一条数据,效果图如下:
技术分享

2、void DeleteById(object id);删除数据函数
[csharp] view plain copy 技术分享技术分享
  1. using (var redisClient = RedisManager.GetClient())  
  2.                 {  
  3.                     var user = redisClient.GetTypedClient<User>();  
  4.                     //var newUser = new User  
  5.                     //{  
  6.                     //    Id = user.GetAll().Count,  
  7.                     //    Name = txtName.Text,  
  8.                     //    Job = new Job { Position = txtPosition.Text }  
  9.                     //};  
  10.                     //user.Delete(newUser);  
  11.                     user.DeleteById(txtRedisId.Text);//txtRedisId.Text中为ID值  
  12. }  

如下图,删除了ID等于3的一条数据。
技术分享

3、void DeleteByIds(IEnumerable ids);批量删除函数
[csharp] view plain copy 技术分享技术分享
  1. using (var redisClient = RedisManager.GetClient())  
  2. {  
  3.     var user = redisClient.GetTypedClient<User>();  
  4.     user.DeleteByIds((txtRedisId.Text).ToList());//txtRedisId.Text中为ID值  
  5.   
  6. }  

如下图,代码删除了ID分别为:1、2、6的三条数据。
技术分享

4、void DeleteAll();删除全部数据
[csharp] view plain copy 技术分享技术分享
    1. var user = redisClient.GetTypedClient<User>();  
    2.                     user.DeleteAll();//删除全部数据 

C# Redis实战(五)

标签:new   using   art   white   https   target   pac   long   user   

原文地址:http://www.cnblogs.com/liuguanghai/p/6082976.html

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