LiteDB实践使用

本文展示了如何在.NET环境中使用LiteDB库进行数据操作,包括创建数据库、定义数据模型、数据转换以及实现增删查改功能。通过实例演示了LiteDB的便捷性。

本文是使用LiteDB编写一个词条的增加、显示和删除的功能。主要是想体现一下使用LiteDB的奇妙。实现界面如下:


实现代码:

1、首先封装好操作LiteDB的底层,下面给出仅供参考的几个方法:

  • 获取数据库路径类;
    public class DB
    {
        // Get a unique database name in TestResults folder
        public static string Path()
        {
            var path = System.IO.Path.GetFullPath(
                System.AppDomain.CurrentDomain.BaseDirectory +
                string.Format("../Data/litedb-{0}.db", "litedb-2f6d864e-7f00-412d-aa5d-58593ad2f15d"));

            return path;
        }
    }

  • 封装数据模型实体;
    public class FilterWordInfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Addtime { get; set; }
        public string UserName { get; set; }
    }
  • 封装数据转换类;
/// <summary>
    /// 数据转换
    /// </summary>
    public class BsonToObject
    {
        public static T ConvertTo<T>(BsonDocument bd)
            where T:new()
        {
            T model = new T();
            PropertyInfo[] propertyInfos = model.GetType().GetProperties();
            foreach (var property in propertyInfos)
            {
                if (property != null && bd.ContainsKey(property.Name))
                {
                    property.SetValue(model, bd[property.Name], null);
                }
            }
            return model;
        }
        public static IList<T> ConvertToList<T>(List<BsonDocument> dbList)
            where T:new()
        {
            IList<T> _List = new List<T>();
            foreach(var bd in dbList)
            {
                T _t=ConvertTo<T>(bd);
                if(_t!=null)
                {
                    _List.Add(_t);
                }
            }
            return _List;
        }
    }
  • 编写LiteDB数据操作类;
public static class LiteDBOP
    {
        public static int Save<T>(T obj)
            where T:new()
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection<T>("filterword");
                // Insert new customer document
                var value = col.Insert(obj);
                return value.AsInt32;
            }
        }
        public static bool Update<T>(T obj)
            where T : new()
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection<T>("filterword");
                // Update a document inside a collection
                var success = col.Update(obj);
                return success;
            }
        }
        public static bool Delete(int docId)
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection("filterword");
                var success = col.Delete(docId);
                return success;
            }
        }
        public static BsonDocument FindById(int docId)
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection("filterword");
                var doc = col.FindById(1);
                return doc;
            }
        }
        public static T FindById<T>(int docId)
            where T:new()
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection("filterword");
                var doc = col.FindById(1);
                return BsonToObject.ConvertTo<T>(doc);
            }
        }
        public static IList<BsonDocument> FindAll()
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection("filterword");
                var doc = col.FindAll().ToList();
                return doc;
            }
        }
        public static IList<T> FindAll<T>()
            where T:new()
        {
            // Open data file (or create if not exits)
            using (var db = new LiteDatabase(DB.DB.Path()))
            {
                // Get a collection (or create, if not exits)
                var col = db.GetCollection<T>("filterword");
                var docs = col.FindAll();
                return docs.ToList();
            }
        }
    }

界面调用数据实现就不在此多说了,其他的和我们日常编写的C#代码一样。这样就完成了一个LiteDB的一个demo。



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值