zoukankan      html  css  js  c++  java
  • C# Redis实战(四)

    四、写入数据

    C# Redis实战(三)中我们已经配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息。
    接下来,就可以进行Redis的数据写入了。Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具体代码如下:
     
    1、保存多条数据
     protected void btnOpenDB_Click(object sender, EventArgs e)
            {
                //System.Diagnostics.Process.Start("D:\redis\redis-server.exe");
                //lblShow.Text = "Redis已经打开!";
    
                using (var redisClient = RedisManager.GetClient())
                {
                    var user = redisClient.GetTypedClient<User>();
    
                    if (user.GetAll().Count > 0)
                        user.DeleteAll();
    
                    var qiujialong = new User
                    {
                        Id = user.GetNextSequence(),
                        Name = "qiujialong",
                        Job = new Job { Position = ".NET" }
                    };
                    var chenxingxing = new User
                    {
                        Id = user.GetNextSequence(),
                        Name = "chenxingxing",
                        Job = new Job { Position = ".NET" }
                    };
                    var luwei = new User
                    {
                        Id = user.GetNextSequence(),
                        Name = "luwei",
                        Job = new Job { Position = ".NET" }
                    };
                    var zhourui = new User
                    {
                        Id = user.GetNextSequence(),
                        Name = "zhourui",
                        Job = new Job { Position = "Java" }
                    };
    
                    var userToStore = new List<User> { qiujialong, chenxingxing, luwei, zhourui };
                    user.StoreAll(userToStore);              
    
                    lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";
                }
            }

    2、保存单条数据

    protected void btnInsert_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtPosition.Text))
                {
                    using (var redisClient = RedisManager.GetClient())
                    {
                        var user = redisClient.GetTypedClient<User>();
    
                        var newUser = new User
                        {
                            Id = user.GetNextSequence(),
                            Name = txtName.Text,
                            Job = new Job { Position = txtPosition.Text }
                        };                  
                        user.Store(newUser);
                       
                        if (user.GetAll().Count > 0)
                        {
                            var htmlStr = string.Empty;
                            foreach (var u in user.GetAll())
                            {
                                htmlStr += "<li>ID=" + u.Id + "&nbsp;&nbsp;姓名:" + u.Name + "&nbsp;&nbsp;所在部门:" + u.Job.Position + "</li>";
                            }
                            lblPeople.Text = htmlStr;
                        }
                        lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";
                    }
                }
            }
    效果图:

    如需转载,请注明出处,本系列博文示例程序下载地址
     http://blog.csdn.net/qiujialongjjj/article/details/17221631
  • 相关阅读:
    (九十三)蓝牙的基本使用
    (九十二)加速计的用法(过期方法+新方法)
    (九十一)距离传感器的使用
    1060. Are They Equal (25)
    (九十)使用多个storyboard+代码实现控制器的分开管理
    (八十九)用AutoLayout实现动画和Label根据内容自动调整
    HDU 2013:蟠桃记
    HDU 2050:折线分割平面
    HDU 2042:不容易系列之二
    HDU 1465:不容易系列之一
  • 原文地址:https://www.cnblogs.com/tianciliangen/p/4949388.html
Copyright © 2011-2022 走看看