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

被折叠的 条评论
为什么被折叠?



