- [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#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[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!