db4o是一个开源的面向对象数据库,他实现了把对象持久化到数据库。db4o支持Java和.Net,这里介绍的是db4o for net。
和关系数据库一样,db4o也有数据文件,db4o数据文件扩展名为'yap'。db4o数据引擎由Db4objects.Db4o.dll组成.
db4o是怎么把对象保存到数据库的,举个简单的例子:
首先在项目里引用Db4objects.Db4o.dll
//
定义一个学生类
public
class
Student
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private string name;
private int age;
private School myschool;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Student()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public Student(string name,int age,School school)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.name=name;
this.age=age;
this.myschool=school;
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public string Name
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return name;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
name=value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public int Age
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return age;
}
set
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
age=value;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public School MySchool
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return myschool;
}
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return string.Format("name:{0};age:{1};school:({2})",name,age,myschool);
}
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
保存学生对象到数据库
//
打开数据库文件Student.yap,如果不存在,会自动创建一个Student.yap文件
IObjectContainer db
=
Db4oFactory.OpenFile(
"
Student.yap
"
);
School school
=
new
School(
"
ustc
"
,
"
china
"
);
Student student1
=
new
Student(
"
Tom
"
,
20
,school);
try
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
db.Set(student1);
}
finally
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
db.Close();
}
db4o有一个管理工具叫Object Manager,是用Java写的,你可以通过Object Manager查看Student对象是否保存到数据库里。
和关系数据库一样,db4o也有自己的查询语言。分别是QBE(Query By Example),NQ(Native Query)和SODA(Simple Object Database Access).
QBE(Query By Example)
QBE查询方式先创建一个Template对象,通过和Template比较返回匹配的全部对象,例如:
//
创建Template对象
Student template
=
new
Student(
"
Tom
"
,
0
,
null
);
//
查询和Template匹配的全部对象
IObjectSet result
=
db.Get(template);
ListResult(result);
NQ(Native Query)
NQ查询方式可以用你熟悉的编程语言进行数据库查询,例如:
//
定义一个继承自Predicate的类,Predicate是Native Query的基类
public
class
ComplexQuery:Predicate
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//定义一个Math方法,里面是返回bool值的查询表达式
public bool Match(Student student)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return student.Age>26;
}
}
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
//
查询对象
IObjectSet result
=
db.Query(
new
ComplexQuery());
ListResult(result);
SODA(Simple Object Database Access)
SODA ,简单对象数据库访问。SODA 是 db4o 最底层的查询 API
//
构造查询对象
IQuery query
=
db.Query();
//
设置约束实例
query.Constrain(
typeof
(Student));
//
设置约束实例的字段和约束条件
query.Descend(
"
name
"
).Constrain(
"
Tom
"
);
//
查询对象
IObjectSet result
=
query.Execute();
ListResult(result);