Skip to content

Instantly share code, notes, and snippets.

View jpd21122012's full-sized avatar
:octocat:

Ing. Jorge Perales Díaz jpd21122012

:octocat:
View GitHub Profile
var sw = Stopwatch.StartNew();
for (int i=0;i<10000;i++) InsertItem(i);
sw.Stop();
Console.WriteLine($"Insert 10k: {sw.ElapsedMilliseconds}ms");
using SQLite;
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "data.db");
var conn = new SQLiteAsyncConnection(dbPath);
// Ensure table exists
await conn.CreateTableAsync<Product>();
// Insert
await conn.InsertAsync(new Product { Name = "Laptop", Price = 1200 });
using SQLite;
public class Product
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Name { get; set; } = "";
public double Price { get; set; }
}
dotnet add package sqlite-net-pcl
using LiteDB;
string path = Path.Combine(FileSystem.AppDataDirectory, "appdata.db");
using var db = new LiteDatabase($"Filename={path};");
// Get a collection (creates if missing)
var col = db.GetCollection<Note>("notes");
// Insert
var note = new Note { Title = "Hello", Content = "World" };
public class Note
{
public int Id { get; set; } // LiteDB uses int id by default
public string Title { get; set; } = "";
public string Content { get; set; } = "";
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}
dotnet add package LiteDB
using Realms;
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "realm.realm");
var config = new RealmConfiguration(dbPath);
var realm = Realm.GetInstance(config);
// Create (within a write transaction)
realm.Write(() =>
{
realm.Add(new User { Name = "Alice", Age = 30 });
using Realms;
public class User : RealmObject
{
[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; } = "";
public int Age { get; set; }
}
dotnet add package Realm