namespace LiteDBCwTest { internal class Program { static void Main(string[] args) { //创建或打开数据库文件 using (var db = new LiteDatabase(@"MyCustomerData.db")) { // 获取集合(相当于表) 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" } };
// 批量插入数据 collection.Insert(customers);
// 查询数据 var result = collection.FindAll();
foreach (var c in result) { Console.WriteLine($"Name: {c.Name}, Age: {c.Age}, Email: {c.Email}"); }
// 根据条件查询数据 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}"); }
// 删除数据 var deletedResult = collection.Delete(resultByAge.FirstOrDefault().Id); } } } }