C#LiteDB基本使用

LiteDB基本使用

1.创建实体类

创建一个实体类

{
    public int Id { get; set; }
    public int Age { get; set; }
    public string Name { get; set; } = string.Empty;
    public string[] Phone { get; set; }
    public bool IsActive { get; set; }

}

2.连接数据库以及一些CRUD

在NuGet中添加LiteDB

    // 打开数据库,如果不存在就会自动创建
    var db = new LiteDatabase(@"MyData.db");

    // 增删改查案例
    // 获取Student集合对象
    var col = db.GetCollection<Student>("student");
    for(int i = 1; i < 10; i++) 
    {
        var student = new Student()
        {
            Id = i,
            Age = 18+i,
            Name = "Test",
            Phone = new string[] { "8000-1000"+i, "1001-8080"+i },
            IsActive = true, 
         };
        // 数据插入
        col.Insert(student);
    }
    // 在id字段上创建唯一索引
    col.EnsureIndex(x => x.Id, true);
    // 数据查询
    List<Student> list = col.Find(x => x.Age > 20).ToList();
    Student user = col.FindOne(x => x.Id == 1);
    Console.WriteLine($"Lite数据库中共有{list.Count}人年龄大于20的人");
    foreach (Student stu in list)
    {
        ShowInfo(stu);
    }
    Console.WriteLine("Lite数据库中Id为1的人");
    ShowInfo(user);

	// 删除所有数据
    col.DeleteAll();
}

static void ShowInfo(Student student)
{
    Console.WriteLine("姓名:"+student.Name + "年龄:"+student.Age);
}

相关推荐

  1. 【Android】WebView 基本使用

    2024-03-19 10:52:03       44 阅读
  2. postgresql的基本使用

    2024-03-19 10:52:03       53 阅读
  3. SQLite基本使用

    2024-03-19 10:52:03       58 阅读
  4. Gazebo基本使用

    2024-03-19 10:52:03       62 阅读
  5. Redisson的基本使用

    2024-03-19 10:52:03       55 阅读
  6. lua基本语法使用

    2024-03-19 10:52:03       58 阅读
  7. SpringDataRedis 基本使用

    2024-03-19 10:52:03       40 阅读
  8. 【Docker 的基本使用

    2024-03-19 10:52:03       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-19 10:52:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-19 10:52:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-19 10:52:03       82 阅读
  4. Python语言-面向对象

    2024-03-19 10:52:03       91 阅读

热门阅读

  1. 【云开发笔记No.5】DevOps的价值

    2024-03-19 10:52:03       38 阅读
  2. 深入理解 C# Unity 中的事件和委托

    2024-03-19 10:52:03       41 阅读
  3. web集群(haproxy负载均衡+keepalived高可用)

    2024-03-19 10:52:03       42 阅读
  4. 描述一下使用过的后端框架及其特点

    2024-03-19 10:52:03       41 阅读
  5. 网络安全等保测评指标一览表

    2024-03-19 10:52:03       39 阅读
  6. P8722 [蓝桥杯 2020 省 AB3] 日期识别 Python字典

    2024-03-19 10:52:03       37 阅读
  7. nginx相关内容的安装

    2024-03-19 10:52:03       38 阅读
  8. webpack和vite的区别是什么

    2024-03-19 10:52:03       34 阅读
  9. ArcGIS Pro 和 ArcMap 10个不同

    2024-03-19 10:52:03       64 阅读