DB4object.net的应用示例(面向对象数据库)

DB4Object也并没有太多的神奇之处,希望大家能同我一样看了之后能有个大体的了解,本文亦同样是从园子里看一些朋友的文章总了最简单的调用示例而成,并没有太多的别的意图,只希望大家看了我的示例后能快速上手而已。
示例如下:
 1
 2        /// <summary>
 3        /// 加载db4object褓数据库
 4        /// </summary>
 5        /// <param name="filename"></param>

 6        public void LoadDb4o(string filename)
 7        {
 8            if (!filename.EndsWith(".yap")) filename = filename + ".yap";
 9            if (!System.IO.File.Exists(filename)) throw new Exception("要载入的文件不存在 !");
10            IObjectContainer db = Db4oFactory.OpenFile(filename);
11            try
12            {
13                IObjectSet result = db.Get(new ExportRule());
14                Set(result.Next() as ExportRule);
15                System.Console.WriteLine("ExportRule LoadDb4o from " + filename);
16            }

17            catch (Exception ex)
18            {
19                System.Console.WriteLine("Error: 取ExportRule对象失败! " + ex.Message);
20            }

21            finally
22            {
23                db.Close();
24            }

25        }

26
27        /// <summary>
28        /// 保存到db4object实体数据库中
29        /// </summary>
30        /// <param name="filename"></param>

31        public void SaveDb4o(string filename)
32        {
33            if (!filename.EndsWith(".yap")) filename = filename + ".yap";
34            if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
35            Db4oFactory.Configure().ObjectClass(typeof(ExportRule)).CascadeOnUpdate(true);
36            IObjectContainer db = Db4oFactory.OpenFile(filename);
37            try
38            {
39                db.Set(this);
40                db.Commit();
41                System.Console.WriteLine("ExportRule SaveDb4o to " + filename);
42            }

43            catch (Exception ex)
44            {
45                System.Console.WriteLine("Error: 存ExportRule对象失败! " + ex.Message);
46            }

47            finally
48            {
49                db.Close();
50            }

51        }

52        #endregion
53    }
posted @ 2008-05-10 21:01  我想去长安  阅读(590)  评论(0编辑  收藏  举报