DBMNG数据库管理与应用

科学是实事求是的学问,来不得半点虚假。
当前位置:首页 > 数据库基础 > DB4O

细说Db4o数据库:查询

通过第一篇的介绍,相信大家也对Db4o有一定的了解,接下来就详细说一下有关查询的话题。

Db4o原生支持3中查询模式:


  • Query-By-Example: 简称 QBE ,根据模板类进行匹配查询,这是最简单的一种模式
  • Native Query: 简称 NQ ,Db4o推荐的查询模式
  • The SODA API :这是Db4o底层查询API ,官网文档解释,此API提供向后的兼容性,适用于动态生成的查询
此外,.Net平台还可以通过LINQ进行快速的查询。

准备测试数据


下面给出一个职工类


  1. /// <summary>  
  2. /// 职工  
  3. /// </summary>  
  4. class Employee  
  5. {  
  6.     /// <summary>  
  7.     /// 姓名  
  8.     /// </summary>  
  9.     string _name;  
  10.     public string Name  
  11.     {  
  12.         set { _name = value; }  
  13.         get { return _name; }  
  14.     }  
  15.   
  16.     /// <summary>  
  17.     /// 职位  
  18.     /// </summary>  
  19.     public Position Position { get; set; }  
  20.   
  21.     public override string ToString()  
  22.     {  
  23.         return string.Format("姓名:{0},职位:{1}",this.Name,this.Position.PositionName);  
  24.     }  
  25. }  
  26.   
  27. /// <summary>  
  28. /// 职位  
  29. /// </summary>  
  30. class Position  
  31. {  
  32.     /// <summary>  
  33.     /// 职称  
  34.     /// </summary>  
  35.     public string PositionName { get; set; }  
  36. }  


填充测试数据



  1. static List<Employee> GetEmployee()  
  2. {  
  3.     List<Employee> _Employees = new List<Employee>();  
  4.     Employee _Employee = new Employee { Name = "Sunth", Position = new Position { PositionName = "CEO" } };  
  5.     _Employees.Add(_Employee);  
  6.     _Employee = new Employee { Name = "Jerry", Position = new Position { PositionName = "CTO" } };  
  7.     _Employees.Add(_Employee);  
  8.     _Employee = new Employee { Name = "Tom", Position = new Position { PositionName = "COO" } };  
  9.     _Employees.Add(_Employee);  
  10.     return _Employees;  
  11. }  


Query-By-Example


一种非常简单直观的查询方式,通过模板进行对比查询,比如说查一个名为Sunth的职工信息
  1. static void Main(string[] args)  
  2. {  
  3.     string DbPath = "Pilot.yap";  
  4.     if (File.Exists(DbPath))  
  5.         File.Delete(DbPath);  
  6.     using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))  
  7.     {  
  8.         //对数据库进行初始化,并存入测试数据  
  9.         var Query = GetEmployee();  
  10.         Query.ForEach  
  11.             (  
  12.                 (a) => { Container.Store(a); }  
  13.             );  
  14.   
  15.         //查询一个叫做Sunth的职工  
  16.         Employee _Employee = new Employee();  
  17.         _Employee.Name = "Sunth";  
  18.         IObjectSet Result = Container.QueryByExample(_Employee);  
  19.         //打印结果  
  20.         PrintResult(Result);  
  21.     }  
  22.     Console.ReadKey();  
  23. }  
大家是否明白了QueryByExample()这个方法名的含义,根据你所给出指定类型的模板,Db4o进行属性值匹配查询。如果模板中属性被没有赋值,Db4o自动取默认值当做条件。如果想查询所有匹配此类型的数据,只需要实例化一个Employee对象,当做参数,传入就OK。
这种方法虽然很简单,但是它有很大的局限性,比如你不能直接使用 and , or ,like 等操作

NativeQuery


这是Db4o推荐的查询方式,但在.Net平台还是比较推荐使用LINQ的。还是那个例子,查询一个名为Sunth的职工信息
  1. static void Main(string[] args)  
  2. {  
  3.     string DbPath = "Pilot.yap";  
  4.     if (File.Exists(DbPath))  
  5.         File.Delete(DbPath);  
  6.     using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))  
  7.     {  
  8.         //对数据库进行初始化,并存入测试数据  
  9.         var Query = GetEmployee();  
  10.         Query.ForEach  
  11.             (  
  12.                 (a) => { Container.Store(a); }  
  13.             );  
  14.   
  15.         //查询一个叫做Sunth的职工  
  16.         Employee _Employee = new Employee();  
  17.         var Result = Container.Query<Employee>  
  18.             (  
  19.                 delegate(Employee employee)  
  20.                 {  
  21.                     return employee.Name == "Sunth";  
  22.                 }  
  23.             );  
  24.         //打印结果  
  25.         PrintResult(Result);  
  26.     }  
  27.     Console.ReadKey();  
  28. }  
这样是不是灵活性更高点了呢。

The SODA API


Db4o底层的查询方式,使用便捷度肯定不如前两种,但是了解是必须的,当遇到不可解决的问题时,这可能就是一思路。
查询一个名为Sunth的职工信息,具体的注释请看代码
  1. static void Main(string[] args)  
  2. {  
  3.     string DbPath = "Pilot.yap";  
  4.     if (File.Exists(DbPath))  
  5.         File.Delete(DbPath);  
  6.     using (IObjectContainer Container = Db4oEmbedded.OpenFile(DbPath))  
  7.     {  
  8.         //对数据库进行初始化,并存入测试数据  
  9.         var Query = GetEmployee();  
  10.         Query.ForEach  
  11.             (  
  12.                 (a) => { Container.Store(a); }  
  13.             );  
  14.   
  15.         //查询一个叫做Sunth的职工  
  16.         IQuery _IQuery = Container.Query();  
  17.         //强制约束Employee类型  
  18.         _IQuery.Constrain(typeof(Employee));  
  19.         //注意此“_name”为字段名称,非属性名称  
  20.         _IQuery.Descend("_name").Constrain("Sunth");  
  21.         //执行查询  
  22.         IObjectSet Result = _IQuery.Execute();  
  23.         //打印结果  
  24.         PrintResult(Result);  
  25.     }  
  26.     Console.ReadKey();  
  27. }  

结束语


说了Db4o原生支持的查询方式,而这些是最基本的,在以后的文章里,我们必定会用到更加繁琐的查询。

刚开始写系列文章,在语言组织方面还是一大缺陷,请大家多多原谅,祝大家生活幸福。

from:http://blog.csdn.net/sunthx/article/details/22694605

本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号