namespace LiteDBCwTest { internal class Program { static void Main(string[] args) { //創(chuàng)建或打開數(shù)據(jù)庫文件 using (var db = new LiteDatabase(@"MyCustomerData.db")) { // 獲取集合(相當(dāng)于表) var collection = db.GetCollection<Customer>("customers");
var customers = new List<Customer>() { new Customer { Name = "追逐時光者", Age = 18, Email = "17854545@gmail.com" }, new Customer { Name = "大姚", Age = 30, Email = "16556545@gmail.com" } };
// 批量插入數(shù)據(jù) collection.Insert(customers);
// 查詢數(shù)據(jù) var result = collection.FindAll();
foreach (var c in result) { Console.WriteLine($"Name: {c.Name}, Age: {c.Age}, Email: {c.Email}"); }
// 根據(jù)條件查詢數(shù)據(jù) var query = Query.EQ("Age", 30); var resultByAge = collection.Find(query);
foreach (var c in resultByAge) { Console.WriteLine($"Name: {c.Name}, Age: {c.Age}, Email: {c.Email}"); }
// 刪除數(shù)據(jù) var deletedResult = collection.Delete(resultByAge.FirstOrDefault().Id); } } } }