db4o是面向对象的数据库,复杂应用就不说了,说点简单的。
数据库嘛,简单的操作就是添加,删除,更新和查询。
1、添加
添加非常简单:
先建一个需要存储的对象
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
public override string ToString()
{
return string.Format("ID:{0} , Name:{1}", ID, Name);
}
}
在进行存储操作
using (IObjectContainer db = Db4oFactory.OpenFile("d:\\d.dat"))
{
db.Store(new Model() { ID = 1, Name = "Test" });
}
或者
IObjectContainer db = Db4oFactory.OpenFile("d:\\d.dat");
db.Store(new Model() { ID = 1, Name = "Test" });
db.Dispose();
2、更新
更新操作就有一个引用的概念。怎么确定是删除的对象,在db4o中是通过ObjectRefence来操作的。对象的指向一定要正确啦。所以虽然也是用Store方法进行更新操作,但是,不能直接更新。比如对于上面添加的一条记录使用
IObjectContainer db = Db4oFactory.OpenFile("d:\\d.dat");
db.Store(new Model() { ID = 1, Name = "Test" });
db.Dispose();
db.Store(new Model() { ID = 1, Name = "Test" });
db.Dispose();
并不会更新,而是一个添加操作。而下面的操作也无法更新:
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
db.Store(new Model() { ID = 1, Name = "Test2" });
}
}
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
db.Store(new Model() { ID = 1, Name = "Test2" });
}
}
需要更新引用才可以。因此要写成:
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).Name = "Test2";
db.Store(list[i]);
}
}
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).Name = "Test2";
db.Store(list[i]);
}
}
在循环中list[i]是无法直接赋值的。不能像下面那样用:
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
list[i] = new Model() { Name = "tttttt", ID = 2 };
db.Store(list[i]);
}
}
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
list[i] = new Model() { Name = "tttttt", ID = 2 };
db.Store(list[i]);
}
}
可以给Model对象添加一个方法:
public class Model
{
public int ID { get; set; }
public string Name { get; set; }
{
public int ID { get; set; }
public string Name { get; set; }
public void SetValue(Model model)
{
this.ID = model.ID;
this.Name = model.Name;
}
{
this.ID = model.ID;
this.Name = model.Name;
}
public override string ToString()
{
return string.Format("ID:{0} , Name:{1}", ID, Name);
}
}
{
return string.Format("ID:{0} , Name:{1}", ID, Name);
}
}
然后使用:
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).SetValue(new Model() { Name = "tttttt", ID = 2 });
db.Store(list[i]);
}
}
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).SetValue(new Model() { Name = "tttttt", ID = 2 });
db.Store(list[i]);
}
}
当然,也可以像下面一样用:
IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName);
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).SetValue(new Model() { Name = "tttttt", ID = 2 });
db.Store(list[i]);
}
db.Dispose();
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
((Model)list[i]).SetValue(new Model() { Name = "tttttt", ID = 2 });
db.Store(list[i]);
}
db.Dispose();
3、删除
删除和更新差不多,也需要删除引用,可以有下面两种用法。
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
db.Delete(list[i]);
}
}
或者
IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName);
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
db.Delete(list[i]);
}
db.Dispose();
4、查询
更新和删除实际上已经用了查询,查询语句可以写成:
using (IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName))
{
IObjectSet list = db.QueryByExample(new Model() { ID = 2 });
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(((Model)list[i]).ToString());
}
}
或者
IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName);
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i].ToString());
}
db.Dispose();
上面是根据对象查的。下面根据是按类型,用到的方法是Query。
IObjectContainer db = Db4oFactory.OpenFile(Util.YapFileName);
IList<Model> list = db.Query<Model>(typeof(Model));
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i].ToString());
}
db.Dispose();
5、简单讲解
查询的时候,比如
IObjectSet list = db.QueryByExample(new Model() { ID = 1 });
是查找所有ID为1的Model类型的数据。
如果要查找Name为“123”就写成:
IObjectSet list = db.QueryByExample(new Model() { Name = "123" });
6、封装
下面给两个操作的封装类:
对象封装
或者
静态方法封装
注意,以上两个封装并没有封装更新的方法,更新的时候需要新查询数据然后再更新。