Modern featured with C#, which is async/await, will be used, and Entity framework with SQLite DB handling user adding, fetching like operation done using simple .Net Core application. Let's create a .NET Core simple application in Visual Studio 2022 and install the SQLite Database package and Entity framework package from Manage NuGet Packages
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite
Microsoft.EntityFrameworkCore.Tools
Asynchronous methods can help to perform tasks without blocking the running threads. Asynchronous functions generally contain the await expression and Task keyword Task<T> with returns.
Example
static async Task Main(string[] args)
{
Console.WriteLine("Fetching data...");
//
}
static async Task<string> FetchDataAsync(string url)
{
using (HttpClient client = new HttpClient())
{
string response = await client.GetStringAsync(url);
return response;
}
}
Create a model Class file for table structure and execute application SQLite DB file will create
Example Model Class
public class ItemContext : DbContext
{
public ItemContext() { }
public DbSet<Item> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=Item.db");
}
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
public static async Task ClassDBExample()
{
using (var db = new ItemContext())
{
await db.Database.EnsureCreatedAsync();
var product = new Item { Name = "PC", Price = 899.99m};
db.Products.Add(product);
await db.SaveChangesAsync();
var products = await db.Products.ToListAsync();
foreach (var item in products)
{
Console.WriteLine($"ID: {item.Id}, Name: {item.Name}, Price: {item.Price}");
}
product.Price = 899.99m;
await db.SaveChangesAsync();
db.Products.Remove(product);
await db.SaveChangesAsync();
}
}
Output