.net db4o数据库初探

第一次使用db4o数据库,感觉很好用。写了个demo。代码有点乱,看个思路吧。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Db4objects.Db4o;
namespace db4oTest
{
    class db4oTest
    {
        /// <summary>
        /// 插入对象
        /// </summary>
        public void InsertObject()
        {
            IObjectContainer db = Db4oFactory.OpenFile("test.yap");
            var p = new Person("李四妹", SexTypeEnum.女,188);
            var s = new Student();
            s.Name = "张学";
            s.sex = SexTypeEnum.女;
            s.School = "第一中学";
            db.Store(p);
            db.Store(s);
            db.Close();
        }
        /// <summary>
        /// 查询对象
        /// </summary>
        public void QueryObject()
        {
            IObjectContainer db = Db4oFactory.OpenFile("test.yap");
            var personList = db.Query<Person>(o => o.sex == SexTypeEnum.女);
            foreach(var p in personList)
            {
                if(p is Student)
                {
                    Console.WriteLine(p.Name + "是个学生");
                }

                Console.WriteLine(string.Format("{0} {1} {2}", p.Name, p.sex.ToString(), p.height));
            }
            db.Close();
        }
        /// <summary>
        /// 修改值,没有专门的update语句,先查询出来 修改后直接Store提交
        /// </summary>
        public void ChangeValue()
        {
            IObjectContainer db = Db4oFactory.OpenFile("test.yap");
            var personList = db.Query<Person>(o => o.Name=="李四妹");
            foreach (var p in personList)
            {
                p.Name = "李四妹修改";
                db.Store(p);
            }
            db.Close();
        }
        /// <summary>
        /// 删除 有专门的delete语句,先查询出来然后delete
        /// </summary>
        public void DeleteObject()
        {
            IObjectContainer db = Db4oFactory.OpenFile("test.yap");
            var personList = db.Query<Person>(o => o.Name.IndexOf("")>=0);
            foreach (var p in personList)
            {
                db.Delete(p);
            }
            db.Close();
        }
    }
    class Person
    {
        public string Name { get; set; }
        public SexTypeEnum sex { get; set; }
        public int height { get; set; }
        public Person()
        { }
        public Person(string name, SexTypeEnum sex, int height)
        {
            this.Name = name;
            this.sex = sex;
            this.height = height;
        }
    }
    class Student : Person
    {
        public Student()
        { }
        public string School { get; set; }
    }
    enum SexTypeEnum
    {
        男, 女
    }
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。