Getting Started with LiteDB database in C#
By FoxLearn 7/19/2024 2:18:38 AM 13K
LiteDB is lightweight and embedded database, making it suitable for small to medium-sized applications. You can download and install it from Nuget Manage Packages in your Visual Studio.
Here’s a simple guide on how to use LiteDB in C#.
Creating a new Windows Forms Application project, then create a Contact class as shown below.
We will create a simple demo by creating a simple Contact class, then add the Contact object to the LiteDB database.
public class Contact { public int Id { get; set; } public string FullName { get; set; } public string Email { get; set; } public string Phone { get; set; } public string Address { get; set; } }
Next, You need to rebuild your project, then open your form designer. Drag and drop the DataGridView control from the Visual Studio toolbox to your winform, then add a bindingsource to your DataGridView.
Opening your Visual Studio DataSource, then change your control fileds to the TextBox control. Next, Drag all controls from your DataSource to your winform.
You can design a simple UI that allows you to Insert Update Delete and Search data from LiteDB database in C# Windows Forms Application as shown above.
You need to install the LiteDB NuGet package into your C# project. You can do this via NuGet Package Manager Console or NuGet Package Manager in Visual Studio, or by adding a package reference in your project file.
To install LiteDB you can right-click on your project, then select Nuget Manage Packages => Search for 'LiteDB' and Install it.
You can also install the LiteDB via command line by clicking on Tools menu => NuGet Package Manager => Package Manager Console, then enter the command below.
Install-Package LiteDB
LiteDB is a library that helps you work with c# embedded database, you can also use litedb .net core.
Adding a Form_Load event handler, then declare variables as shown below.
LiteDatabase db; LiteCollection<Contact> list;
And don't forget to import the LiteDB namespace to your winform.
using LiteDB;
Moving to Form_Load event handler, then modify your code as shown below allows you to read data from the LiteDB database.
private void Form1_Load(object sender, EventArgs e) { db = new LiteDatabase($"{Application.StartupPath}\\mydb.db"); list = db.GetCollection<Contact>(); //list.EnsureIndex("Id", true); if (list.Count() == 0) contactBindingSource.DataSource = new List<Contact>(); else contactBindingSource.DataSource = list.FindAll(); }
We will store LiteDB in the Debug directory, by default when the application runs if the database is not found, it will automatically be created.
Adding a click event handler to the New button allows you to add new record to the bindingsource.
private void btnNew_Click(object sender, EventArgs e) { contactBindingSource.Add(new Contact()); contactBindingSource.MoveLast(); }
After adding a new record, you should move to the last row.
Adding a click event handler to the Insert button allows you to insert data to the LiteDB.
private void btnInsert_Click(object sender, EventArgs e) { if (idTextBox.Text == "0") { MessageBox.Show("Please enter your contact Id.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); idTextBox.Focus(); return; } if (string.IsNullOrEmpty(fullNameTextBox.Text)) { MessageBox.Show("Please enter your fullname.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning); fullNameTextBox.Focus(); return; } //Do similar for all controls //Contact contact = new Contact(); //contact.Id = Convert.ToInt32(idTextBox.Text); //contact.FullName = fullNameTextBox.Text; //contact.Email = emailTextBox.Text; //contact.Phone = phoneTextBox.Text; //contact.Address = addressTextBox.Text; Contact contact = contactBindingSource.Current as Contact; if (contact != null) { list.Insert(contact); //contactBindingSource.Add(contact); //contactBindingSource.ResetAllowNew(); } }
Calling the litedb insert method to insert a contact object to the contact collection in the LiteDB.
You can add new record directly to LiteDB database or add new record to bindingsource, then insert to the LiteDB database.
Adding a click event handler to the Update button allows you to update data to the LiteDB database.
private void btnUpdate_Click(object sender, EventArgs e) { Contact contact = contactBindingSource.Current as Contact; if (contact != null) { list.Update(contact); } }
Adding a click event handler to the Delete button allows you to delete data from the LiteDB database.
private void btnDelete_Click(object sender, EventArgs e) { Contact contact = contactBindingSource.Current as Contact; if (contact != null) { if (MessageBox.Show("Are you sure want to delete this record?", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { list.Delete(contact.Id); contactBindingSource.Remove(contact); } } }
You should get current object from the bindingsource, then delete data and remove data from your bindingsource.
Adding a click event handler to the Clear button allows you to clear search, and refetch data from the LiteDB database.
private void btnClear_Click(object sender, EventArgs e) { if (list.Count() == 0) contactBindingSource.DataSource = new List<Contact>(); else contactBindingSource.DataSource = list.FindAll(); }
Adding a keypress event handler to the search textbox control, allows you to filter data from the Contact collection.
private void txtSearch_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)13)//enter { var results = list.Find(p => p.FullName.Contains(txtSearch.Text)).ToList(); contactBindingSource.DataSource = results; } }
We will check the user press the enter key, Next, You need to find data from LiteDB, then add result search to the bindingsource control.
You can read more How to use LiteDB Manager to know how to use LiteDB Manager or LiteDB Studio tool to open the LiteDB database.
Through this c# example, i hope so you can learn about c# nosql example by using LiteDB for .NET.