HomeFeaturedAboutContact
.NET
Using NoSQL Database with DotNET Example | C# | LiteDB | Tutorial
Shehryar Khan
Shehryar Khan
January 16, 2022
2 min

Table Of Contents

01
Introduction
02
Types of NoSQL Db
03
Creating a Project
04
Installing LiteDB
05
Creating Models
06
View Data in Database

Introduction

One of the very first choices to make when start developing an application is whether to use a SQL or NoSQL Database. Organizing data is a very hard task. When we say organise, we are actually categorising our data according to its type.

NoSQL systems don’t provide the level of data consistency as SQL databases. The fundamental difference between SQL and NoSQL is how data is stored and retrieved.

A conventional database like MySQL, Microsoft SQL Server or Oracle Database uses a schema.

Schema-less or NoSQL data storage is useful in the following scenarios:

  • Small Local/Desktop Applications
  • Small Web Applications
  • One database per user/account data store
  • Few parallel write operations
  • Application file format

Types of NoSQL Db

There are four common types of NoSQL technologies are available.

  • Document databases
  • Wide column stores
  • Key-value stores
  • Graph databases

In this Article, we’re going to setup & use a document database LiteDB, which is an open source MongoDB-like database with zero configuration.

LiteDB Features

  • Similar to MongoDB
  • Link Supported
  • Document Store
  • Thread & Process safe
  • Pure C# Code
  • Data recovery after write operation failure
  • Single Data file like SQLite
  • DES(AES) cryptography supported for data file
  • Store files & stream data
  • Index document fields for fast search
  • Open Source & also available at NuGet
  • Shell command line

Creating a Project

I’m going to use the VS Code for creating a project to store & retrieve Data from a NoSQL Database.

Let’s start by creating a console application using dotnet cli

dotnet new console

Installing LiteDB

LiteDB is available at NuGet. you can download from NuGet package manager if using VS or

run this command to install the library from NuGet

using package manager

Install-Package LiteDB

or using .NET CLI

dotnet add package LiteDB

Creating Models

Let’s create a simple User Model for creating User collection & inserting data.

So, create a new File as User.cs at the root of your Project & put the below Code.

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

Now add the below Code in your Program.cs File.

using System;
using System.Linq;
using LiteDB;
class Program
{
    static void Main(string[] args)
    {
        // Open database (or create if not exits)
        using(var db = new LiteDatabase(@"MyData.db"))
        {
            // Get user collection
            var users = db.GetCollection<User>("users");

            // Create your new user instance
            var user = new User
            { 
                Name = "Shehryar Khan", 
                Email = "[email protected]"
            };

            // Insert new user document (Id will be auto-incremented)
            users.Insert(user);

            var result = users.Find(x => x.Email == "[email protected]").FirstOrDefault();
            Console.WriteLine(result.Name);

            // Update a document inside a collection
            user.Name = "S Khan";
            users.Update(user);

            // Index document using a document property
            users.EnsureIndex(x => x.Name);
        }
    }
}

In the above Code, first of all we have created a Database as MyData.db then a User Collection.

We’re using Linq to retrieve data from DB & also Updating a record in our NoSQL DB.

Now Run your Project, you’ll see Name (Shehryar Khan) in your Console.

You can also download & run this example from Github.

View Data in Database

There are some tools available to View your LiteDB NoSQL Database Records.

So, Download this LiteDB Manager Tool, unrar the File & run the LiteDBManager.exe

Capture 768x458

From the File Menu, click on Open Database & choose your MyData.db File from your Project Root Directory.

It will look like this

Capture 1 768x460

You can see your records are stored in the Database.

This is a very basic operation using NoSQL database, you can find some more examples at GitHub.

Comment if you find any difficulty, I’ll love to solve your problem.

Related Articles:

Generate QR Code using ASP.NET

How to Create Soap Web Services in DotNet Core


Tags

#LiteDB#NoSQL
Shehryar Khan

Shehryar Khan

Full-Stack .NET Developer

I'm passionate about learning new technologies as well as mentoring and helping others get started with their programming career. This blog is my way of giving back to the Community.

Expertise

.NET
ASP.NET
React

Social Media

instagramtwitterwebsite

Related Posts

dropthedot
Top 15 Memes of Twitter DropTheDot Campaign Hashtag
August 19, 2022
1 min
© 2023, All Rights Reserved.

Quick Links

Advertise with usContact Us

Social Media