首先需要添加引用ServiceStack.Redis
using(IRedisClient client=new RedisClient("127.0.0.1",6379,"123456",2)) { #region 单个增加与读取 //删除当前数据所有key client.FlushDb(); Console.WriteLine("删除成功!"); //新增key,默认使用了序列化 client.Set<string>("myname", "Adu"); //读取key string res=client.Get<string>("myname"); //或者是 JsonConvert.DeserializeObject<string>(client.GetValue("name")) Console.WriteLine(res); #endregion #region 批量新增与读取 var dic = new Dictionary<string, string>(); dic.Add("name", "Adu"); dic.Add("Gender", "Men"); client.SetAll(dic); Console.WriteLine("批量插入成功!"); #endregion }
其中上面的RedisClient的参数分别是地址,端口号,密码以及数据库的号,因为Redis默认有16个数据库db0~db15,此时指的是db2