上一篇介紹在Redis如何進行Insert和Select,這篇介紹Update和Delete。
private static void Update(int id, string name, DateTime birthday,bool sex)
{
try
{
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var usersClient = redisClient.As<User>();
var users = usersClient.GetAll();
if (users != null)
{
var userdata = users.FirstOrDefault(x => x.ID == id);
if (userdata != null)
{
userdata.Name = name;
userdata.Birthday = birthday;
userdata.Sex = sex;
usersClient.Store(userdata);
Console.WriteLine("Update ID:{0}", id.ToString());
}
else
Console.WriteLine("No Data");
}
else
Console.WriteLine("No users Data");
Console.WriteLine(System.Environment.NewLine);
}
}
catch (Exception ex)
{
Console.WriteLine("ex {0}", ex.Message);
throw;
}
}
private static void Delete(int id)
{
try
{
using (RedisClient redisClient = new RedisClient("localhost", 6379))
{
var usersClient = redisClient.As<User>();
var userdata= usersClient.GetAll().FirstOrDefault(x => x.ID == id);
if (userdata != null)
{
usersClient.Delete(userdata);
Console.WriteLine("Delete ID:{0}", id.ToString());
}
else
Console.WriteLine("No Data");
Console.WriteLine(System.Environment.NewLine);
}
}
catch (Exception ex)
{
Console.WriteLine("ex {0}", ex.Message);
throw;
}
}
GetUser(70);//Get User By ID
Update(70, "new ricoisme", DateTime.Parse( "1982-01-15"),true);
GetUser(70);//Get User By ID
GetUser(73);//Get User By ID
Delete(73);
GetUser(73);//Get User By ID
資料庫基本的CRUD就是這樣簡單又快速
參考