using System;
using System.Collections.Generic;
using System.Text;
using Db4objects.Db4o;//使用的是db4o7.12 自己添加dll引用
namespace DB4OHelper
{
public class dbHelper
{
string _dbFilePath = string.Empty;
public IObjectContainer db = null;
#region
// 1IList <Pilot> result = db.Query<Pilot> (delegate(Pilot pilot) {
//2 return pilot.Points > 99
//3 && pilot.Points < 199
//4 || pilot.Name == "Rubens Barrichello";
//5});
#endregion
public dbHelper(string dbFilePath)
{
_dbFilePath = dbFilePath;
}
private void openDatabase()
{
db = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), _dbFilePath);
}
private void closeDatabase()
{
db.Close();
}
public void save<EntityType>(EntityType EntityObj)
{
openDatabase();
try
{
db.Store(EntityObj);
}
finally
{
closeDatabase();
}
}
/// <summary>
/// 查询全部数据
/// </summary>
/// <typeparam name="ClassType"></typeparam>
/// <returns></returns>
public IList<ClassType> Query<ClassType>()
{
// IObjectContainer mydb = Db4oEmbedded.OpenFile(Db4oEmbedded.NewConfiguration(), _dbFilePath);
openDatabase();
try
{
IObjectSet resoult = db.QueryByExample(typeof(ClassType));
return convertDataSet<ClassType>(resoult);
}
finally
{
closeDatabase();
// mydb.Close();
}
}
/// <summary>
/// 条件查询
/// </summary>
/// <typeparam name="ClassType"></typeparam>
/// <param name="parmObj"></param>
/// <returns></returns>
public IList<ClassType> Query<ClassType>(ClassType parmObj)
{
openDatabase();
try
{
IObjectSet resoutl = db.QueryByExample(parmObj);
return convertDataSet<ClassType>(resoutl);
}
finally
{
closeDatabase();
}
}
public IList<T> convertDataSet<T>(IObjectSet ListSet)
{
List<T> resoult = new List<T>();
while (ListSet.HasNext())
{
T obj = (T)ListSet.Next();
resoult.Add(obj);
}
return resoult;
}
}
}