Created
February 13, 2013 19:01
-
-
Save Dreistein/4947153 to your computer and use it in GitHub Desktop.
Simple db4o database example written in c#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication1 | |
{ | |
class Person | |
{ | |
public String firstName; | |
public String lastName; | |
public byte age; | |
public String title; | |
public Person(string firstName, string lastName, byte age) | |
{ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.age = age; | |
} | |
public Person(string firstName, string lastName, byte age, string title) | |
{ | |
this.firstName = firstName; | |
this.lastName = lastName; | |
this.age = age; | |
this.title = title; | |
} | |
public String ToString() | |
{ | |
return title + " " + firstName + " " + lastName + ": " + age; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Db4objects.Db4o; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//open database file "person.data" | |
using (IObjectContainer db = Db4oEmbedded.OpenFile("person.data")) | |
{ | |
/* //declare some persons | |
Person stefan = new Person("Max", "Mustermann", 17); | |
Person pohl = new Person("Alfred", "Adams", 33); | |
Person eckl = new Person("Florian", "Dietrich", 21); | |
//store the persons in the database | |
db.Store(stefan); | |
db.Store(pohl); | |
db.Store(eckl); | |
db.Commit(); | |
//*/ | |
//fetch all Persons from the database | |
IObjectSet result = db.QueryByExample(new Person(null, null, 0, null)); | |
//query through the results and print them out | |
while (result.HasNext()) | |
{ | |
Person next = (Person) result.Next(); | |
Console.WriteLine(next.ToString()); | |
} | |
Console.Read(); | |
//Close the database | |
db.Close(); | |
} | |
} | |
} | |
} |
helli Miliks i tried this code you can change ""Db4oEmbedded" by this "Db4oFactory" it works with me 👍
The sample data is a bit far fetched in my opinion:
One would never persist an age but the date of birth as we usually get older...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dear Dreistein, i was trying to use the code you publish, but i get an error in a Program class, "Db4oEmbedded" doesn't exist in current context. I found only db4o-7.2.msi which seems to be only for .NET 2.0 and i have 3.5 and 4.0 .NET on my PC and adding the Db4objects.Db4o doesn't resolve the error. Is there any way to use this DB. I tryed also to use it in VM as well, but with no luck.
Could you please help me?
Thanks a lot in advance!
Liudmila