今天下午开始接触db4o,遇到了一个很诡异的问题,下面是具体代码:
=====================
using System;
using System.IO;
using Db4objects.Db4o;
namespace Db4object
{
public class StuInfo
{
public string StuName { get; set; }
public int StuAge { get; set; }
override public string ToString()
{
return string.Format("{0} / {1}" , StuName , StuAge);
}
}
public class FirstStepsExample
{
public static void Main(string[] args)
{
IObjectContainer db = Db4oFactory.OpenFile("c:\\data.dbf");
InsertManyData(db);
RetrieveAllData(db);
db.Dispose();
Console.ReadKey();
}
public static void InsertManyData(IObjectContainer db)
{
for (int i = 0 ; i < 1000 ; i++)
{
StuInfo info = new StuInfo() { StuName = "name :" + i , StuAge = i };
db.Store(info);
}
}
public static void RetrieveAllData(IObjectContainer db)
{
IObjectSet result = db.Query(typeof(StuInfo));
ListResult(result);
}
private static void ListResult(IObjectSet result)
{
Console.WriteLine("total count :{0}" , result.Count);
foreach (StuInfo item in result)
Console.WriteLine(item);
}
}
}
=====================
第一次运行,InsertManyData()插入数据成功,RetrieveAllData()成功将全部数据读出,但是当去掉InsertManyData()再次运行时(此时data.dbf已有了数据),但是RetrieveAllData()就不能读取任何数据了(total count :0).
ps:db4o是最新的官方版本
Stable or developer 版本