Vous êtes sur la page 1sur 9

Jkup Wenningstedt Hansen

Side 1

12-10-2009

2009 Tutorial (DB4O and Visual Studio 2008 Express) ...............................................1 Download the Database .............................................................................................1 Installation of the Database........................................................................................2 Creating the project in VS..........................................................................................3 Pointing VS to the DB4O ..........................................................................................4 Creating the Database ................................................................................................5 Create two Objects and put them in the Database .....................................................6 The big code example ................................................................................................8

2009 Tutorial (DB4O and Visual Studio 2008 Express)


The tutorial that comes along with DB4O, is not working that well so therefore I created this tutorial.

Download the Database

Jkup Wenningstedt Hansen

Side 2

12-10-2009

Installation of the Database

Just follow the basic steps, and everything will be ok.

Jkup Wenningstedt Hansen

Side 3

12-10-2009

Creating the project in VS


Let start by creating a console project in Visual Studio(VS). The name is not important, but I named it DB4O_Connection.

Jkup Wenningstedt Hansen

Side 4

12-10-2009

Pointing VS to the DB4O


Then we come to the most important part. We shall point VS to the DB4O. This is done simply by right clicking on the project and then clicking Add Reference.

And then find the Db4objects.Db4o.dll file from the net-3.5 folder, and double click. Now this project can use using Db4objects.Db4o.

Jkup Wenningstedt Hansen

Side 5

12-10-2009

Creating the Database


So let us create some code.

It is important that you Save the project first. Than run it by taping Ctrl and F5. After this you should be able to find a file called DB4Ofile in the same folder that the project is stored. If not, there is something wrong. I shall assume that all went fine, and continue.

Jkup Wenningstedt Hansen

Side 6

12-10-2009

Create two Objects and put them in the Database


using System; using System.Collections.Generic; using System.IO; using System.Web; using Db4objects.Db4o.Query; using Db4objects.Db4o; namespace formul1 { public class FirstStepsExample { public static void Main(string[] args) { IObjectContainer db = Db4oFactory.OpenFile("DB4Ofile"); try { // storeFirstPilot Pilot pilot1 = new Pilot("Michael Schumacher", 100); db.Store(pilot1); // storeSecondPilot Pilot pilot2 = new Pilot("Rubens Barrichello", 99); db.Store(pilot2); Pilot proto = new Pilot(null, 0); IObjectSet result = db.QueryByExample(proto); //ListResult(result); Console.WriteLine(result.Count); foreach (Pilot item in result) { Console.WriteLine(item.getName()); } } finally { db.Close(); } } } public class Pilot { private String name; private int trips; public Pilot(String s, int i) { name = s; trips = i; } public String getName() { return name; } } }

Jkup Wenningstedt Hansen

Side 7

12-10-2009

If you run this code it will produce this output.

The first output is 2 object, and if you run it again it will show 4 objects in the database two Michael and 2 Rubens. So for every time you run it add 2 objects.

Jkup Wenningstedt Hansen

Side 8

12-10-2009

The big code example


using System; using System.Collections.Generic; using System.IO; using System.Web; using Db4objects.Db4o.Query; using Db4objects.Db4o; namespace formul1 { public class FirstStepsExample { public static void Main(string[] args) { IObjectContainer db = Db4oFactory.OpenFile("filen3"); try { //------run this only first time ---START----Pilot pilot1 = new Pilot("Ann", 100); db.Store(pilot1); Pilot pilot2 = new Pilot("Ben", 200); db.Store(pilot2); Pilot pilot3 = new Pilot("Conny", 300); db.Store(pilot3); Pilot pilot4 = new Pilot("Danny", 400); db.Store(pilot4); //------run this only first time ---STOP----Pilot proto = new Pilot(null, 0); IObjectSet result = db.QueryByExample(proto); Team t = new Team("Hyundai"); Console.WriteLine(result.Count); foreach (Pilot item in result) { //------run this only first time ---START----if (item.getName().Equals("Conny")) { item.setTeam( t); db.Store(item); } //------run this only first time ---START----Console.WriteLine(item.getName() + " : " + item.getTrips()); } foreach (Pilot item in result) { if (item.getName().Equals("Bo Bosen")) { Console.WriteLine("Vi er inde i Bo Boesen: " + item.getName()); item.getTeam().printTeamName(); Console.WriteLine("Har lige printet Bo Boesens Team"); } else { Console.WriteLine("This Driver has got no Team: " + item.getName()); } } Team proto2 = new Team(null); IObjectSet result2 = db.QueryByExample(proto2); foreach (Team item in result2) { item.printTeamName(); } //---- Her har vi Native Quarie ----------------------IList<Pilot> result3 = db.Query<Pilot>(delegate(Pilot pilot) { return pilot.trips > 300; //&& pilot.trips < 199 //|| pilot.name == "Rubens Barrichello"; }); foreach (Pilot item in result3) { Console.WriteLine("Her har vi nativ quary"); item.printName(); } //----------native slut -------------------------------} finally { db.Close(); } } }

Jkup Wenningstedt Hansen

Side 9

12-10-2009

public class Pilot { public String name; public int trips; private Team team; public Pilot(String s, int i) { name = s; trips = i; } public void setTeam(Team t) { team = t; } public Team getTeam() { return team; } public String getName() { return name; } public int getTrips() { return trips; } public void printName() { Console.WriteLine("Mit navn er: " + name); } } public class Team { private String TeamName; public Team(String s) { TeamName = s; } public String getTeamName() { return TeamName; } public void printTeamName() { Console.WriteLine("This is team: " + TeamName + " test"); } } }

After you run this example it shall produce this output

I hope that this was a simple and quick tutorial to get started with DB4O from VS. Thanks. www.jakupwhansen.dk

Vous aimerez peut-être aussi