C # Redis Combat (iv)

Source: Internet
Author: User

Iv. Writing datain C # Redis Combat (iii) We have configured the Web. config program and can read and manage the above configuration information through C # code. Next, you can write the data for Redis. In Redis, you can save single and multiple pieces of data with store and Storeall, and the code in C # is as follows:
1. Save more than one piece of data [CSharp]View Plain copy
  1. protected void Btnopendb_click (object sender, EventArgs e)
  2. {
  3. //system.diagnostics.process.start ("D:\\redis\\redis-server.exe");
  4. //lblshow.text = "Redis is open!"  ";
  5. using (var redisclient = redismanager.getclient ())
  6. {
  7. var user = redisclient.gettypedclient<user> ();
  8. if (user. GetAll (). Count > 0)
  9. User. DeleteAll ();
  10. var qiujialong = New User
  11. {
  12. Id = user. Getnextsequence (),
  13. Name = "Qiujialong",
  14. Job = New Job {Position = ". NET"}
  15. };
  16. var chenxingxing = New User
  17. {
  18. Id = user. Getnextsequence (),
  19. Name = "Chenxingxing",
  20. Job = New Job {Position = ". NET"}
  21. };
  22. var Luwei = New User
  23. {
  24. Id = user. Getnextsequence (),
  25. Name = "Luwei",
  26. Job = New Job {Position = ". NET"}
  27. };
  28. var Zhourui = New User
  29. {
  30. Id = user. Getnextsequence (),
  31. Name = "Zhourui",
  32. Job = New Job {Position = "Java"}
  33. };
  34. var usertostore = new List<user> {qiujialong, chenxingxing, Luwei, Zhourui};
  35. User. StoreAll (Usertostore);
  36. Lblshow.text = "Current total:" + user. GetAll (). Count.tostring () + "People!  ";
  37. }
  38. }

2 . Save A single piece of data [CSharp]View Plain copy
  1. protected void Btninsert_click (object sender, EventArgs e)
  2. {
  3. if (! String. IsNullOrEmpty (txtname.text) &&! string. IsNullOrEmpty (Txtposition.text))
  4. {
  5. using (var redisclient = redismanager.getclient ())
  6. {
  7. var user = redisclient.gettypedclient<user> ();
  8. var newuser = New User
  9. {
  10. Id = user. Getnextsequence (),
  11. Name = txtName.Text,
  12. Job = New Job {Position = Txtposition.text}
  13. };
  14. User. Store (NewUser);
  15. if (user. GetAll (). Count > 0)
  16. {
  17. var htmlstr = string.  Empty;
  18. foreach (Var u in user). GetAll ())
  19. {
  20. Htmlstr + = "<li>id=" + u.id + "Name:" + u.name + "department:" + u.job.position + "</li>";
  21. }
  22. Lblpeople.text = Htmlstr;
  23. }
  24. Lblshow.text = "Current total:" + user. GetAll (). Count.tostring () + "People!  ";
  25. }
  26. }
  27. }

:

C # Redis Combat (iv)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.