You are here: Basics Operations & Concepts > Access The Database

The Basic Operations

The object container is the door to the database access. It's the starting point for all database operations.

Accessing a Database

The object container is the interface for accessing the database. To open the database you pass the file-name to the object container factory. Normally you should open an object container when the application starts and close it when it is shuts down.

using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o"))
{
    // use the object container
}
Db4oBasics.cs: Open the object container to use the database
' use the object container
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o")
End Using
Db4oBasics.vb: Open the object container to use the database

Storing Objects

Storing a object with db4o is extremely easy. Open the object container and pass your object to the store method and db4o will do the rest. There's no mapping required. db4o will read the class meta data, the read the object values with reflection and store the data.

using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o"))
{
    Pilot pilot = new Pilot("Joe");
    container.Store(pilot);
}
Db4oBasics.cs: Store a object
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o")
    Dim pilot As New Pilot("Joe")
    container.Store(pilot)
End Using
Db4oBasics.vb: Store a object

Queries

Querying for objects is also easy. There are different query interfaces available with different benefits. See "Querying"

The most natural query method is using LINQ.

using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o"))
{
    var pilots = from Pilot p in container
                 where p.Name == "Joe"
                 select p;
    foreach (var pilot in pilots)
    {
        Console.Out.WriteLine(pilot.Name);
    }
}
Db4oBasics.cs: Query for objects
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o")
    Dim pilots = From p As Pilot In container Where p.Name = "Joe"
    For Each pilot As Pilot In pilots
        Console.Out.WriteLine(pilot.Name)
    Next
End Using
Db4oBasics.vb: Query for objects

Update Objects

Updating objects is also easy. First you query for the object which you want to update. Then you change the object and store it again in the database.

using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o"))
{
    var pilot = (from Pilot p in container
                 where p.Name == "Joe"
                 select p).First();
    pilot.Name = "New Name";
    // update the pilot
    container.Store(pilot);
}
Db4oBasics.cs: Update a pilot
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o")
    Dim pilot = (From p As Pilot In container Where p.Name = "Joe").First()
    pilot.Name = "New Name"
    ' update the pilot
    container.Store(pilot)
End Using
Db4oBasics.vb: Update a pilot

Delete Objects

Use the delete-operation to delete objects.

using (IObjectContainer container = Db4oEmbedded.OpenFile("databaseFile.db4o"))
{
    var pilot = (from Pilot p in container
                 where p.Name == "Joe"
                 select p).First();
    container.Delete(pilot);
}
Db4oBasics.cs: Delete a object
Using container As IObjectContainer = Db4oEmbedded.OpenFile("databaseFile.db4o")
    Dim pilot = (From p As Pilot In container Where p.Name = "Joe").First()
    container.Delete(pilot)
End Using
Db4oBasics.vb: Delete a object