.Net Tutorial

Here is a simple tutorial on how to use NeoDatis with .Net:

How to insert an object

public void Step1()
        {
            // Create instance
            Sport sport = new Sport("volley-ball");
 
            ODB odb = null;
 
            try
            {
                // Open the database
                odb = ODBFactory.Open(ODB_NAME);
 
                // Store the object
                odb.Store(sport);
            }
            finally
            {
                if (odb != null)
                {
                    // Close the database
                    odb.Close();
                }
            }
        }

How to insert a complex object in NeoDatis Database

public void Step2()
        {
 
            // Create instance
            Sport volleyball = new Sport("volley-ball");
 
            // Create 4 players
            Player player1 = new Player("olivier", new DateTime(), volleyball);
            Player player2 = new Player("pierre", new DateTime(), volleyball);
            Player player3 = new Player("elohim", new DateTime(), volleyball);
            Player player4 = new Player("minh", new DateTime(), volleyball);
 
            // Create two teams
            Team team1 = new Team("Paris");
            Team team2 = new Team("Montpellier");
 
            // Set players for team1
            team1.AddPlayer(player1);
            team1.AddPlayer(player2);
 
            // Set players for team2
            team2.AddPlayer(player3);
            team2.AddPlayer(player4);
 
            // Then create a volley ball game for the two teams
            Game game = new Game(new DateTime(), volleyball, team1, team2);
 
            ODB odb = null;
 
            try
            {
                // Open the database
                odb = ODBFactory.Open(ODB_NAME);
 
                // Store the object
                odb.Store(game);
            }
            finally
            {
                if (odb != null)
                {
                    // Close the database
                    odb.Close();
                }
            }
        }

How to retrieve objects using Criteria queries

public void Step3()  {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery query = new CriteriaQuery(Where.Equal("name", "olivier"));
            Objects<Player> players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 3 : Players with name olivier");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

How to retrieve objects using Criteria queries traversing relations

public void Step4()  {
 
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            // Let's insert a tennis player
            Player agassi = new Player("Andr\u00E9 Agassi", new DateTime(), new Sport("Tennis"));
            odb.Store(agassi);
 
            IQuery query = new CriteriaQuery(typeof(Player), Where.Equal("favoriteSport.name", "volley-ball"));
 
            Objects<Player> players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 4 : Players of Voller-ball");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

How to retrieve objects using Criteria queries with Or

public void Step6()
    {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery query = new CriteriaQuery(typeof(Player), Where.Or().Add(Where.Equal("favoriteSport.name", "volley-ball")).Add(
                    Where.Like("favoriteSport.name", "%nnis")));
 
            Objects<Player> players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 6 : Volley-ball and Tennis Players");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

How to retrieve objects using Criteria queries with Not

ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery query = new CriteriaQuery(typeof(Player), Where.Not(Where.Equal("favoriteSport.name", "volley-ball")));
 
            Objects<Player> players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 7 : Players that don't play Volley-ball");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
 
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }

How to retrieve objects using order by

public void Step10()  {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery query = new CriteriaQuery(typeof(Player));
            query.OrderByAsc("name");
 
            Objects<Player> players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 10: Players ordered by name asc");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
 
            query.OrderByDesc("name");
 
            players = odb.GetObjects<Player>(query);
 
            Console.WriteLine("\nStep 10: Players ordered by name desc");
 
            i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
 
        } finally {
            if (odb != null) {
                odb.Close();
            }
        }
    }

How to update objects

public void Step12()  {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery query = new CriteriaQuery(typeof(Sport), Where.Equal("name", "volley-ball"));
 
            Objects<Sport> sports = odb.GetObjects<Sport>(query);
 
            // Gets the first sport (there is only one!)
            Sport volley = sports.GetFirst();
 
            // Changes the name
            volley.SetName("Beach-Volley");
 
            // Actually updates the object
            odb.Store(volley);
 
            // Commits the changes
            odb.Close();
 
            odb = ODBFactory.Open(ODB_NAME);
            // Now query the database to check the change
            sports = odb.GetObjects<Sport>();
 
            Console.WriteLine("\nStep 12 : Updating sport");
 
            int i = 1;
            // display each object
            while (sports.HasNext()) {
                Console.WriteLine((i++) + "\t: " + sports.Next());
            }
 
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

How to delete Objects

public void Step13()  {
 
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME);
            IQuery queryAll = new CriteriaQuery(typeof(Player));
            IQuery query = new CriteriaQuery(typeof(Player), Where.Like("name", "%Agassi"));
 
            Objects<Player> players = odb.GetObjects<Player>(queryAll);
            players = odb.GetObjects<Player>(query);
 
            // Gets the first player (there is only one!)
            Player agassi = players.GetFirst();
 
            odb.Delete(agassi);
 
            odb.Close();
 
            odb = ODBFactory.Open(ODB_NAME);
            // Now query the databas eto check the change
            players = odb.GetObjects<Player>();
 
            Console.WriteLine("\nStep 13 : Deleting Agassi");
 
            int i = 1;
            // display each object
            while (players.HasNext()) {
                Console.WriteLine((i++) + "\t: " + players.Next());
            }
 
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

Database protected by user and password

public void Step17()  {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME_2, "user", "password");
            odb.Store(new Sport("Tennis"));
            // Commits the changes
            odb.Close();
 
            try {
                // try to Open the database without user/password
                odb = ODBFactory.Open(ODB_NAME_2);
            } catch (ODBAuthenticationRuntimeException e) {
                Console.WriteLine("\nStep 17 : invalid user/password : database could not be Opened");
            }
            // then Open the database with correct user/password
            odb = ODBFactory.Open(ODB_NAME_2, "user", "password");
            Console.WriteLine("\nStep 17 : user/password : database Opened");
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }

Retrieving Object using Native queries

public class VolleyBallPlayerQueries : SimpleNativeQuery
{
    public bool Match(Player p)
    {
        return player.GetFavoriteSport().GetName().ToLower().StartsWith("volley");
    }
}
 
public void NQ()  {
        ODB odb = null;
 
        try {
            // Open the database
            odb = ODBFactory.Open(ODB_NAME_2, "user", "password");
            // Retrieve players that play volley ball
            Objects<Player> players = odb.GetObjects<Player>(new VolleyBallPlayerQueries());
 
            odb.Close();
 
        } finally {
            if (odb != null) {
                // Close the database
                odb.Close();
            }
        }
    }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License