Jákup 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
Jákup Wenningstedt Hansen Side 2 12-10-2009 Installation of the Database Just follow the basic steps, and everything will be ok.
Jákup 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.
Jákup 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.
Jákup 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.
Jákup 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;
Jákup 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.
Jákup 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();
Jákup 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