c#连接Redis---(2)Redis存读取数据

来源:互联网 发布:手机分班软件 编辑:程序博客网 时间:2024/06/07 00:00

 http://blog.csdn.net/zx13525079024/article/details/8127104

这一节演示下载.NET中怎样使用Redis存储数据.在.net中比较常用的客户端类库是ServiceStack,看下通过servicestack怎样存储数据。

   DLL下载:https://github.com/ServiceStack/ServiceStack.Redis

   下载完成后,DLL中包括四个DLL文件,然后把这四个文件添加到自己的项目中。

     

    Redis中包括四种数据类型,Strings, Lists,  Sets, Sorted Sets

    接下来我们一一看这四种类型的用法

   1.连接redis服务器

[csharp] view plaincopy
  1. RedisClient client;  
  2. private void button1_Click(object sender, EventArgs e)  
  3. {  
  4.     //连接服务器  
  5.      client = new RedisClient("127.0.0.1",6379);  
  6. }  

    2.Strings类型
         Strings能存储字符串,数字等类型,浮点型等.NET中的基本类型

[csharp] view plaincopy
  1. private void button2_Click(object sender, EventArgs e)  
  2.      {  
  3.          //存储用户名和密码  
  4.          client.Set<string>("username","524300045@qq.com");  
  5.          client.Set<int>("pwd", 123456);  
  6.          string username = client.Get<string>("username");  
  7.          int pwd=client.Get<int>("pwd");  
  8.   
  9.          client.Set<decimal>("price", 12.10M);  
  10.          decimal price = client.Get<decimal>("price");  
  11.          MessageBox.Show(price.ToString());  
  12.   
  13.          MessageBox.Show("用户名:"+username+",密码:"+pwd.ToString());  
  14.      }  
0 0
原创粉丝点击