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

    配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息。

    接下来,就可以进行Redis的数据写入了。Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具体代码如下:
     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() + "人!";
                    }
                }
            }
     
  • 相关阅读:
    使用网络服务
    Linux Socket 学习(九)
    Linux Socket学习(六)
    Linux Socket学习(八)
    Developing Software in Visual Studio .NET with NonAdministrative Privileges
    html5+css3实现一款注册表单
    linux编程下signal()函数
    深入理解Oracle索引(10):索引列字符类型统计信息的32位限制
    智能手机屏幕清晰度用户体现的分析:PPI与PPI2
    架设邮件服务器windows 2003 POP3服务,SMTP服务收发邮件
  • 原文地址:https://www.cnblogs.com/wuxl360/p/5434348.html
Copyright © 2011-2022 走看看