- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
- [C#] Viết ứng dụng Auto Fill list Textbox from clipboard Winform
- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
[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!