- [C#] Viết ứng dụng xem và dò kết quả xổ số kiến thiết miền nam
- [EBOOK] Chia sẽ giáo trình học lập trình C - Giáo sư Phạm Văn Ất
- [C#] Hướng dẫn viết ứng dụng theo dõi máy in bao nhiêu trang (Monitor Printer)
- [C#] Lấy thông tin cấu hình máy tính xuất ra text file winform
- [C#] Chia sẽ class Install, Uninstall, Start và Stop Services Winform
- [C#] Tìm kiếm tập tin file nhanh chóng trên Winform sử dụng thư viện FastSearchLibrary
- [C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
- [C#] Sử dụng thư viện Mini Profiler Integrations ghi log thực hiện các câu lệnh SQL
- [DEVEXPRESS] Thiết kế Dropdown ButtonBarItem trên Form Ribbon
- [C#] Lưu trạng thái các control trên Winform vào Registry Windows
- [C#] Ứng dụng ví dụ Simple Observer Pattern tăng giảm số lượng trên winform
- [C#] Hướng dẫn lấy thời gian thực server time trên winform
- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
[C#] Hướng dẫn thêm, lưu, xóa, sửa với Redis Database
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn thêm, lưu, xóa, sửa CRUD Redis Database trong lập trình C# winform.
[C#] How to CRUD Redis Database
Redis Database là gì?
Redis (REmote DIctionary Server) là một mã nguồn mở được dùng để lưu trữ dữ liệu có cấu trúc, có thể sử dụng như một database, bộ nhớ cache hay một message broker.
Redis lưu trữ data dưới dạng key-value và database này được lưu trữ trên RAM của máy tính, nên tốc độ truy xuất rất nhanh.
Chính vì ưu điểm lưu trữ dữ liệu trên RAM và xử lý rất nhanh, nên người thường sử dụng Redis làm bộ nhớ tạm (cache).
Trong bài viết này mình sẽ demo thêm lưu xóa sửa CRUD Redis Database C#:
Đầu tiên, để sử dụng Redis các bạn cần cài 2 phần mềm này vào nhé:
1. Redis-x64-3.0.504.msi
2. Redis Desktop Manager
Hai phần mềm này mình tích hợp ở source code bên dưới, các bạn có thể download và cài đặt.
Công việc cài đặt, các bạn chỉ cần Next, next là xong.
Redis default kết nối mặc định qua port 6379.
Nếu các bạn muốn cài đặt password, các từ giao diện console, các bạn thực hiện lệnh sau:
redis 127.0.0.1:6379> AUTH PASSWORD
(error) ERR Client sent AUTH, but no password is set
redis 127.0.0.1:6379> CONFIG SET requirepass "mypass"
OK
redis 127.0.0.1:6379> AUTH mypass
Ok
Dưới đây là giao diện demo ứng dụng CRUD Redis Database c#:
Trong bài viết sử dụng thư viện ServiceStack.Redis các bạn có thể download và cài đặt thư viện này về từ Nuget.
PM> Install-Package ServiceStack.Redis -Version 5.9.2
Đầu tiên các bạn tạo 1 class Student.cs
public class Student
{
public string ID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
Và source code thêm, xóa, sửa cho Form1.cs
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace RedisCRUD
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
void Edit(bool value)
{
txtID.ReadOnly = value;
txtName.ReadOnly = value;
txtAddress.ReadOnly = value;
}
private void Form1_Load(object sender, EventArgs e)
{
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "laptrinhvb"))
{
IRedisTypedClient<Student> phone = client.As<Student>();
studentBindingSource.DataSource = phone.GetAll();
Edit(true);
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
studentBindingSource.Add(new Student());
studentBindingSource.MoveLast();
Edit(false);//Allow edit
txtID.Focus();
}
private void btnEdit_Click(object sender, EventArgs e)
{
Edit(false);//Allow edit
txtID.Focus();
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (MessageBox.Show(this, "Are you sure want to delete this record ?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
var p = studentBindingSource.Current as Student;//Get current object
if (p != null)
{
//Connect to your redis cache
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "laptrinhvb"))
{
IRedisTypedClient<Student> student = client.As<Student>();
student.DeleteById(p.ID);//Delete data by id
studentBindingSource.RemoveCurrent();
}
}
}
}
private void btnSave_Click(object sender, EventArgs e)
{
using (RedisClient client = new RedisClient("127.0.0.1", 6379, "laptrinhvb"))
{
studentBindingSource.EndEdit();
IRedisTypedClient<Student> student = client.As<Student>();
//Get data from bindingsource, then save to redis cache
student.StoreAll(studentBindingSource.DataSource as List<Student>);
MessageBox.Show(this, "Your data has been successfully saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Edit(true);//Readonly
}
}
}
}
Tham khảo Foxlearn.com
Thanks for watching!