zoukankan      html  css  js  c++  java
  • db4o助手 do4oHelper

    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;
    }

    }



    }
  • 相关阅读:
    ASP.NET 2.0 中的新增服务、控件与功能概述作者:Jeff Prosise
    js学习笔记(二)
    js学习笔记(四)表达式和运算符
    js技巧1(判断浏览器)
    css点滴
    after effect 学习笔记(一)
    js学习笔记(六)函数
    js学习笔记(八)数组
    js学习笔记(三)变量
    js学习笔记(一)
  • 原文地址:https://www.cnblogs.com/5tao/p/2110767.html
Copyright © 2011-2022 走看看