Db4o数据库:细说查询
生活随笔
收集整理的這篇文章主要介紹了
Db4o数据库:细说查询
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
通過第一篇的介紹,相信大家也對Db4o有一定的了解,接下來就詳細說一下有關查詢的話題。
Db4o原生支持3中查詢模式:
- Query-By-Example: 簡稱 QBE ,根據模板類進行匹配查詢,這是最簡單的一種模式
- Native Query: 簡稱 NQ ,Db4o推薦的查詢模式
- The SODA API :這是Db4o底層查詢API ,官網文檔解釋,此API提供向后的兼容性,適用于動態生成的查詢
準備測試數據
下面給出一個職工類
/// <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原生支持的查詢方式,而這些是最基本的,在以后的文章里,我們必定會用到更加繁瑣的查詢。 剛開始寫系列文章,在語言組織方面還是一大缺陷,請大家多多原諒,祝大家生活幸福。
總結
以上是生活随笔為你收集整理的Db4o数据库:细说查询的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 软件项目方案模板~!
- 下一篇: CocosBuilder学习之一:认识C