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
- protected void Btnopendb_click (object sender, EventArgs e)
- {
- //system.diagnostics.process.start ("D:\\redis\\redis-server.exe");
- //lblshow.text = "Redis is open!" ";
- 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 = "Current total:" + user. GetAll (). Count.tostring () + "People! ";
- }
- }
2
. Save A single piece of data
[CSharp]View Plain copy
- 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 + "Name:" + u.name + "department:" + u.job.position + "</li>";
- }
- Lblpeople.text = Htmlstr;
- }
- Lblshow.text = "Current total:" + user. GetAll (). Count.tostring () + "People! ";
- }
- }
- }
:
C # Redis Combat (iv)