⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 baseitem.cs

📁 一个简单的Demo
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;
using Db4objects.Db4o;
using System.Collections;
using Db4objects.Db4o.Query;
using System.Xml.Serialization;
using System.IO;

namespace Surance.DB4ODAL
{
    [Serializable]
    public class BaseItem
    {
        private string title;
        private string content;
        private DateTime createTime;
        private int hit;
        private string category;
        [NonSerialized]
        private IObjectContainer db;


        public string Title
        {
            get
            {
                return title;
            }
            set
            {
                title = value;
            }
        }

        public string Content
        {
            get
            {
                return content;
            }
            set
            {
                content = value;
            }
        }
        public DateTime CreateTime
        {
            get
            {
                return createTime;
            }
            set
            {
                createTime = value;
            }
        }
        public int Hit
        {
            get
            {
                return hit;
            }
            set
            {
                hit = value;
            }
        }
        public string Category
        {
            get
            {
                return category;
            }
            set
            {
                category = value;
            }
        }
        [XmlIgnore]
        public IObjectContainer Db
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }


        #region Constructor
        public BaseItem(IObjectContainer db)
        {
            this.db = db;
        }
        public BaseItem()
        {
        }
      
        #endregion

        #region Public Method
        public void Add()
        {
           //db.Set(live);
            db.Set(this);
        }

        public void Update()
        {
            db.Set(this);
        }

        public void Del()
        {
            db.Delete(this);
        }

        public virtual  IObjectSet GetAll()
        {
           
            
            IQuery query = db.Query();
            query.Constrain(this.GetType());
            query.OrderDescending();
            IObjectSet result = query.Execute();


            return result;
        }

        public object GetOne(string title)
        {
            IQuery query = db.Query();
            query.Constrain(this.GetType());
            query.Descend("title").Constrain(title);
            IObjectSet result = query.Execute();

            if (result.Count > 0 && result != null)
                return result[0];
            else
                return null;

           
        }

        public IObjectSet GetByCategory(string CategoryName)
        {
            IQuery query = db.Query();
            query.Constrain(this.GetType());
            query.Descend("category").Constrain(CategoryName);
            IObjectSet result = query.Execute();

            return result;
        }

        public override string ToString()
        {
            return title + "\t" + content + "\t" + createTime + "\t" + hit.ToString() + "\t" + category + "\t";
        }

        public virtual bool  BackupToXml(string path)
        {
            FileStream fs = null;
            try
            {
               
                if (!File.Exists(path))
                    fs = File.Create(path);
                else
                    fs = File.OpenWrite(path);

                IObjectSet allData = this.GetAll();
                XmlSerializer xsl = new XmlSerializer(this.GetType());

                foreach (object o in allData)
                {
                    xsl.Serialize(fs, o);
                }
                return true;
            }
            finally
            {
                fs.Flush();
                fs.Close();
                fs.Dispose();
                
            }
            
        }

        public void HitAdd()
        {
            this.hit++;
            db.Set(this);

        }

        

     


        #endregion


    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -