细说Db4o数据库:查询
通过第一篇的介绍,相信大家也对Db4o有一定的了解,接下来就详细说一下有关查询的话题。
Db4o原生支持3中查询模式:
-
Query-By-Example: 简称 QBE ,根据模板类进行匹配查询,这是最简单的一种模式
-
Native Query: 简称 NQ ,Db4o推荐的查询模式
-
The SODA API :这是Db4o底层查询API ,官网文档解释,此API提供向后的兼容性,适用于动态生成的查询
此外,.Net平台还可以通过LINQ进行快速的查询。
准备测试数据
下面给出一个职工类
-
/// <summary>
-
/// 职工
-
/// </summary>
-
class Employee
-
{
-
/// <summary>
-
/// 姓名
-
/// </summary>
-
string _name;
-
public string Name
-
{
-
set { _name = value; }
-
get { return _name; }
-
}
-
-
/// <summary>
-
/// 职位
-
/// </summary>
-
public Position Position { get; set; }
-
-
public override string ToString()
-
{
-
return string.Format("姓名:{0},职位:{1}",this.Name,this.Position.PositionName);
-
}
-
}
-
-
/// <summary>
-
/// 职位
-
/// </summary>
-
class Position
-
{
-
/// <summary>
-
/// 职称
-
/// </summary>
-
public string PositionName { get; set; }
-
}
填充测试数据
-
static List<Employee> GetEmployee()
-
{
-
List<Employee> _Employees = new List<Employee>();
-
Employee _Employee = new Employee { Name = "Sunth", Position = new Position { PositionName = "CEO" } };
-
_Employees.Add(_Employee);
-
_Employee = new Employee { Name = "Jerry", Position = new Position { PositionName = "CTO" } };
-
_Employees.Add(_Employee);
-
_Employee = new Employee { Name = "Tom", Position = new Position { PositionName = "COO" } };
-
_Employees.Add(_Employee);
-
return _Employees;
-
}
Query-By-Example
一种非常简单直观的查询方式,通过模板进行对比查询,比如说查一个名为Sunth的职工信息
-
static void Main(string[] args)
-
{
-
string DbPath = "Pilot.yap";
-
if (File.Exists(DbPath))
-
File.Delete(DbPath);
-
using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))
-
{
-
//对数据库进行初始化,并存入测试数据
-
var Query = GetEmployee();
-
Query.ForEach
-
(
-
(a) => { Container.Store(a); }
-
);
-
-
//查询一个叫做Sunth的职工
-
Employee _Employee = new Employee();
-
_Employee.Name = "Sunth";
-
IObjectSet Result = Container.QueryByExample(_Employee);
-
//打印结果
-
PrintResult(Result);
-
}
-
Console.ReadKey();
-
}
大家是否明白了QueryByExample()这个方法名的含义,根据你所给出指定类型的模板,Db4o进行属性值匹配查询。如果模板中属性被没有赋值,Db4o自动取默认值当做条件。如果想查询所有匹配此类型的数据,只需要实例化一个Employee对象,当做参数,传入就OK。
这种方法虽然很简单,但是它有很大的局限性,比如你不能直接使用 and , or ,like 等操作
NativeQuery
这是Db4o推荐的查询方式,但在.Net平台还是比较推荐使用LINQ的。还是那个例子,查询一个名为Sunth的职工信息
-
static void Main(string[] args)
-
{
-
string DbPath = "Pilot.yap";
-
if (File.Exists(DbPath))
-
File.Delete(DbPath);
-
using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))
-
{
-
//对数据库进行初始化,并存入测试数据
-
var Query = GetEmployee();
-
Query.ForEach
-
(
-
(a) => { Container.Store(a); }
-
);
-
-
//查询一个叫做Sunth的职工
-
Employee _Employee = new Employee();
-
var Result = Container.Query<Employee>
-
(
-
delegate(Employee employee)
-
{
-
return employee.Name == "Sunth";
-
}
-
);
-
//打印结果
-
PrintResult(Result);
-
}
-
Console.ReadKey();
-
}
这样是不是灵活性更高点了呢。
The SODA API
Db4o底层的查询方式,使用便捷度肯定不如前两种,但是了解是必须的,当遇到不可解决的问题时,这可能就是一思路。
查询一个名为Sunth的职工信息,具体的注释请看代码
-
static void Main(string[] args)
-
{
-
string DbPath = "Pilot.yap";
-
if (File.Exists(DbPath))
-
File.Delete(DbPath);
-
using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))
-
{
-
//对数据库进行初始化,并存入测试数据
-
var Query = GetEmployee();
-
Query.ForEach
-
(
-
(a) => { Container.Store(a); }
-
);
-
-
//查询一个叫做Sunth的职工
-
IQuery _IQuery = Container.Query();
-
//强制约束Employee类型
-
_IQuery.Constrain(typeof(Employee));
-
//注意此“_name”为字段名称,非属性名称
-
_IQuery.Descend("_name").Constrain("Sunth");
-
//执行查询
-
IObjectSet Result = _IQuery.Execute();
-
//打印结果
-
PrintResult(Result);
-
}
-
Console.ReadKey();
-
}
说了Db4o原生支持的查询方式,而这些是最基本的,在以后的文章里,我们必定会用到更加繁琐的查询。
刚开始写系列文章,在语言组织方面还是一大缺陷,请大家多多原谅,祝大家生活幸福。
from:http://blog.csdn.net/sunthx/article/details/22694605