Quick Start with Embedded eXtremeSQL in C#

The principle classes for most eXtremeSQL C# applications are Database, SqlLocalConnection and SqlResultSet. The SqlLocalConnection is normally instantiated by calling the Database ConnectSql() method. Then SqlLocalConnection method ExecuteStatement() is called to execute SQL insert, update and delete statements. Alternatively SqlLocalConnection method Insert() can be called to insert objects of database classes and CurrentCursor.Update()and CurrentCursor.Remove() can be called to update or delete objects. Method ExecuteQuery() is called to execute a select statement returning a SqlResultSet. Then various SqlResultSet methods can be used to process result sets. The following sections will walk through the steps for building a simple C# eXtremeSQL application. In addition, note that there are C# SDK samples that demonstrate specific eXtremeSQL features.

Database definition

For C# applications the database is defined using native C# class syntax applying special attributes (See here for a detailed explanation). For this example application we will use a schema defined as follows:

     
    [Persistent(AutoID = true)]
    class Person
    {
        [Indexable(Type = Database.IndexType.BTree, Unique = true)]
        public String name;
         
        public unsigned int ordinal;
    }
     

Open the database

To open the database we first instantiate a Database object db in order to call its Open() method. There are a number of constructors for the Database class. For this sample we use the default constructor. The Database class Open() method has three overloads; we will call the version which allows us to specify the following properties for an in-memory database:

The DatabaseParameters class has a number of properties. The property Classes must be initialized with an array of classes to be stored in the database. For this example there is simply the class Person. In the following code snippet we instantiate the Database object, define the DatabaseParameters Classes and the memory page size (the basic unit of memory access) properties, then call the Open() method.

To complete this step we will call the db.Close() method to terminate the database instance. The sample code for Program.cs so far looks like this:

 
    public static void Main(String[] args)
    {
        const int PAGE_SIZE = 128;
        const int DATABASE_SIZE = 16*1024*1024;
         
        Database db = new Database();
        Database.Parameters parameters = new Database.Parameters();
     
        parameters.MemPageSize = PAGE_SIZE;
        parameters.Classes = new Type[]{typeof(Person)};
     
        db.Open("PersonDb", parameters, DATABASE_SIZE);
     
        // Do database processing
     
        db.Close();
    }
     

Populate and query the database using SQL

To populate the database using SQL instantiate a SqlLocalConnection object and call its ExecuteStatement() method as follows:

         
        SqlLocalConnection con = db.ConnectSql();
        con.ExecuteStatement("insert into Person values('Luke Skywalker', 0)");
        con.ExecuteStatement("insert into Person values('Han Solo', 1)");
         

Then to query the database we call the SqlLocalConnection ExecuteQuery() method to instantiate a SqlResultSet object result:

         
        SqlResultSet result = con.ExecuteQuery("select * from Person") );
         

Processing query results

The SqlResultSet object returned from ExecuteQuery() is a dataset that has numerous methods for extracting the result rows and information about the columns, etc. But presenting these details is beyond the scope of this quick start demonstration. Please see QueryResult processing for details and the C# SDK samples for several examples.