此文原创,仅供参考。转载请注明出处!
Program类代码如下:

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
using System.IO;
6
using Db4objects.Db4o;
7
using Db4objects.Db4o.Query;
8
9
namespace DB40DataBase
10

{
11
class Program
12
{
13
static void Main(string[] args)
14
{
15
16
AccessDb4o();
17
18
}
19
public static void AccessDb4o()
20
{
21
22
IObjectContainer db = Db4oFactory.OpenFile("Db4oDemo.cod");
23
{
24
try
25
{
26
//StoreSecondPilot(db); //存储数值
27
if (File.Exists("Db4oDemo.cod") == false)
28
{
29
Console.WriteLine("没有找到数据文件!");
30
return;
31
32
}
33
else
34
{
35
RetrieveAllPilots(db); //查找数值
36
37
}
38
39
40
}
41
catch (Exception ex)
42
{
43
Console.WriteLine(ex.Message);
44
}
45
finally
46
{
47
db.Close();
48
}
49
50
}
51
52
53
}
54
55
//存储数值
56
public static void StoreSecondPilot(IObjectContainer db)
57
{
58
Pilot pilot1 = new Pilot("Barrillo ", 99);
59
Pilot pilot2 = new Pilot("Rubens ", 90);
60
Pilot pilot3 = new Pilot("Runs ", 100);
61
62
db.Store(pilot1);
63
db.Store(pilot2);
64
db.Store(pilot3);
65
66
Console.WriteLine("Stored {0}", pilot1);
67
Console.WriteLine("Stored {0}", pilot2);
68
Console.WriteLine("Stored {0}", pilot3);
69
}
70
71
//下面是查找数值的实现
72
public static void RetrieveAllPilots(IObjectContainer db)
73
{
74
IObjectSet results = db.QueryByExample(typeof(Pilot));
75
ListResult(results);
76
}
77
78
//遍历IObjectSet中Polot中的各个对象
79
public static void ListResult(IObjectSet result)
80
{
81
if (result.Count>0)
82
{
83
Console.WriteLine("共有{0}条数据,分别是", result.Count);
84
foreach (object item in result)
85
{
86
Console.WriteLine(item);
87
}
88
89
}
90
else
91
{
92
Console.WriteLine("没有数据");
93
94
}
95
96
}
97
98
}
99
}
100
Pilot类代码如下:

Code
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
5
namespace DB40DataBase
6

{
7
class Pilot
8
{
9
string _name;
10
int _points;
11
public Pilot(string name, int points)
12
{
13
_name = name;
14
_points = points;
15
}
16
public string Name
17
{
18
get
19
{
20
return _name;
21
}
22
}
23
24
public int Points
25
{
26
get
27
{
28
return _points;
29
}
30
}
31
32
public void AddPoints(int points)
33
{
34
_points += points;
35
}
36
37
override public string ToString()
38
{
39
return string.Format("{0}/{1}", _name, _points);
40
}
41
}
42
}
43
Program类代码如下:
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
using System.IO;6
using Db4objects.Db4o;7
using Db4objects.Db4o.Query;8

9
namespace DB40DataBase10


{11
class Program12

{13
static void Main(string[] args)14

{15
16
AccessDb4o();17
18
}19
public static void AccessDb4o()20

{21
22
IObjectContainer db = Db4oFactory.OpenFile("Db4oDemo.cod");23

{24
try25

{26
//StoreSecondPilot(db); //存储数值27
if (File.Exists("Db4oDemo.cod") == false)28

{29
Console.WriteLine("没有找到数据文件!");30
return;31

32
}33
else34

{35
RetrieveAllPilots(db); //查找数值36

37
}38
39
40
}41
catch (Exception ex)42

{43
Console.WriteLine(ex.Message);44
}45
finally46

{47
db.Close();48
}49

50
}51
52
53
}54

55
//存储数值56
public static void StoreSecondPilot(IObjectContainer db)57

{58
Pilot pilot1 = new Pilot("Barrillo ", 99);59
Pilot pilot2 = new Pilot("Rubens ", 90);60
Pilot pilot3 = new Pilot("Runs ", 100);61

62
db.Store(pilot1);63
db.Store(pilot2);64
db.Store(pilot3);65

66
Console.WriteLine("Stored {0}", pilot1);67
Console.WriteLine("Stored {0}", pilot2);68
Console.WriteLine("Stored {0}", pilot3);69
}70

71
//下面是查找数值的实现72
public static void RetrieveAllPilots(IObjectContainer db)73

{74
IObjectSet results = db.QueryByExample(typeof(Pilot));75
ListResult(results);76
}77

78
//遍历IObjectSet中Polot中的各个对象79
public static void ListResult(IObjectSet result)80

{81
if (result.Count>0)82

{83
Console.WriteLine("共有{0}条数据,分别是", result.Count);84
foreach (object item in result)85

{86
Console.WriteLine(item);87
}88

89
}90
else91

{92
Console.WriteLine("没有数据");93

94
}95
96
}97

98
}99
}100

Pilot类代码如下:
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace DB40DataBase6


{7
class Pilot8

{9
string _name;10
int _points;11
public Pilot(string name, int points)12

{13
_name = name;14
_points = points;15
}16
public string Name17

{18
get19

{20
return _name;21
}22
}23

24
public int Points25

{26
get27

{28
return _points;29
}30
}31
32
public void AddPoints(int points)33

{34
_points += points;35
}36

37
override public string ToString()38

{39
return string.Format("{0}/{1}", _name, _points);40
}41
}42
}43

浙公网安备 33010602011771号