2025-11-09
LiteDB
00

目录

简介
主要特点
环境准备
数据模型设计
数据访问层实现
使用示例
高级查询示例
性能优化建议
总结

简介

本文将详细介绍如何使用C#和LiteDB实现一个电气设备参数管理工具。LiteDB是一个轻量级的、基于文档的.NET NoSQL嵌入式数据库,非常适合小型应用程序的快速开发。

主要特点

  • 无需安装,单文件数据库
  • 支持LINQ查询
  • 支持文档索引
  • 支持自动ID生成
  • 跨平台支持

环境准备

首先需要通过NuGet安装LiteDB包:

Bash
Install-Package LiteDB

image.png

数据模型设计

首先,我们定义电气设备的数据模型:

C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using LiteDB; namespace App16 { /// <summary> /// 电气设备类 /// </summary> public class ElectricalDevice { [BsonId] // 主键标记 public int Id { get; set; } /// <summary> /// 设备编号 /// </summary> public string DeviceCode { get; set; } /// <summary> /// 设备名称 /// </summary> public string Name { get; set; } /// <summary> /// 设备类型 /// </summary> public DeviceType Type { get; set; } /// <summary> /// 额定电压 (V) /// </summary> public double RatedVoltage { get; set; } /// <summary> /// 额定电流 (A) /// </summary> public double RatedCurrent { get; set; } /// <summary> /// 安装位置 /// </summary> public string Location { get; set; } /// <summary> /// 生产日期 /// </summary> public DateTime ManufactureDate { get; set; } /// <summary> /// 最后检修日期 /// </summary> public DateTime LastMaintenanceDate { get; set; } } /// <summary> /// 设备类型枚举 /// </summary> public enum DeviceType { Transformer, // 变压器 Circuit_Breaker, // 断路器 Switch_Gear, // 开关设备 Capacitor, // 电容器 Generator // 发电机 } }

数据访问层实现

创建一个数据访问类来处理与LiteDB的交互:

C#
public class ElectricalDeviceRepository { private readonly string _dbPath; public ElectricalDeviceRepository(string dbPath) { _dbPath = dbPath; } /// <summary> /// 添加新设备 /// </summary> public void AddDevice(ElectricalDevice device) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); collection.Insert(device); } } /// <summary> /// 更新设备信息 /// </summary> public bool UpdateDevice(ElectricalDevice device) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.Update(device); } } /// <summary> /// 删除设备 /// </summary> public bool DeleteDevice(int id) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.Delete(id); } } /// <summary> /// 根据ID查询设备 /// </summary> public ElectricalDevice GetDeviceById(int id) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.FindById(id); } } /// <summary> /// 根据设备编号查询设备 /// </summary> public ElectricalDevice GetDeviceByCode(string deviceCode) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.FindOne(x => x.DeviceCode == deviceCode); } } /// <summary> /// 获取所有设备列表 /// </summary> public List<ElectricalDevice> GetAllDevices() { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.FindAll().ToList(); } } /// <summary> /// 按设备类型查询设备列表 /// </summary> public List<ElectricalDevice> GetDevicesByType(DeviceType type) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.Find(x => x.Type == type).ToList(); } } /// <summary> /// 查询需要维护的设备(距离上次维护超过180天) /// </summary> public List<ElectricalDevice> GetDevicesNeedMaintenance() { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); var checkDate = DateTime.Now.AddDays(-180); return collection.Find(x => x.LastMaintenanceDate <= checkDate).ToList(); } } }

使用示例

下面是一个完整的使用示例:

C#
class Program { static void Main(string[] args) { // 初始化数据库 var dbPath = "ElectricalDevices.db"; var repository = new ElectricalDeviceRepository(dbPath); // 添加新设备 var newDevice = new ElectricalDevice { DeviceCode = "TR-2024-001", Name = "主变压器", Type = DeviceType.Transformer, RatedVoltage = 10000, RatedCurrent = 100, Location = "变电站A区", ManufactureDate = new DateTime(2024, 1, 1), LastMaintenanceDate = DateTime.Now }; repository.AddDevice(newDevice); Console.WriteLine("设备添加成功!"); // 查询所有设备 var allDevices = repository.GetAllDevices(); Console.WriteLine($"当前共有{allDevices.Count}台设备"); // 按类型查询设备 var transformers = repository.GetDevicesByType(DeviceType.Transformer); Console.WriteLine($"变压器设备数量:{transformers.Count}"); // 查询需要维护的设备 var needMaintenance = repository.GetDevicesNeedMaintenance(); Console.WriteLine($"需要维护的设备数量:{needMaintenance.Count}"); // 更新设备信息 var deviceToUpdate = repository.GetDeviceByCode("TR-2024-001"); if (deviceToUpdate != null) { deviceToUpdate.Location = "变电站B区"; repository.UpdateDevice(deviceToUpdate); Console.WriteLine("设备信息更新成功!"); } } }

image.png

高级查询示例

在ElectricalDeviceRepository 中增加功能

C#
/// <summary> /// 获取特定电压等级的设备 /// </summary> public List<ElectricalDevice> GetDevicesByVoltageRange(double minVoltage, double maxVoltage) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.Find(x => x.RatedVoltage >= minVoltage && x.RatedVoltage <= maxVoltage) .ToList(); } } /// <summary> /// 统计各类型设备数量 /// </summary> public Dictionary<DeviceType, int> GetDeviceTypeStatistics() { var allDevices = GetAllDevices(); return allDevices.GroupBy(x => x.Type) .ToDictionary(g => g.Key, g => g.Count()); } /// <summary> /// 获取最近添加的N台设备 /// </summary> public List<ElectricalDevice> GetRecentDevices(int count) { using (var db = new LiteDatabase(_dbPath)) { var collection = db.GetCollection<ElectricalDevice>("devices"); return collection.Find(Query.All(Query.Descending)) .Take(count) .ToList(); } }

image.png

性能优化建议

  • 索引使用
  • 为经常查询的字段添加索引
  • 避免过多索引,可能影响写入性能
  • 连接管理
  • 使用using语句确保正确释放数据库连接
  • 避免长时间持有数据库连接
  • 批量操作
  • 使用批量插入而不是单条插入
  • 使用事务处理批量更新

总结

本文详细介绍了一个基于LiteDB实现的电气设备参数管理工具的开发过程。首先,从基础数据模型的设计开始,确保了系统能够有效地组织和存储相关信息;接着,通过构建数据访问层,为应用程序提供了一种与数据库交互的方式,实现了对数据的基本CRUD(创建、读取、更新、删除)操作。此外,文章还探讨了一些高级查询技术的应用实例,帮助用户更灵活地检索所需信息。针对性能优化方面,也给出了一些建议,以提升系统的响应速度和整体效率。LiteDB作为一款轻量级且易于集成的嵌入式数据库解决方案,在这类小型项目中表现尤为出色,不仅满足了功能需求,同时也保证了使用的便捷性。最后,文中提到,根据实际应用场景的不同,该管理系统还可以进一步扩展,比如增加设备维护记录管理、故障记录跟踪、支持数据导入导出以及自动生成报表等功能,从而更好地服务于用户的多样化需求。

本文作者:技术老小子

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!