Creating and Modifying Database Objects in C#

C# applications use the new operator to instantiate an object and the assignment operator to modify the objects values. To create the database store of the object the Connection method Insert() is called within a transaction. For example:

 
    [Persistent] // Class Record will be stored in eXtremeDB database
    class Record
    {
        [Indexable(Unique=true)] // Create unique (tree) eXtremeDB index by "id" field
        public int id;
 
        public String str;
    }
    ...
    Connection con = new Connection(db);
    con.StartTransaction(Database.TransactionType.ReadWrite); // Start RW transaction
    Record rec = new Record();
    rec.id = 3;
    rec.str = "Record 3";
    con.Insert(rec);
    con.CommitTransaction();
     

To update an object, it is first found with a Cursor navigation, then its fields are modified and the Cursor method Update() is called with the object handle. For example:

 
    cursor = new Cursor<Record>(con, "id");
    // find record with id == 3
    rec = cursor.Find(3);
    rec.str = "Updated string"; 
    cursor.Update();
    cursor.Close(); //release cursor
    con.CommitTransaction();
     

To delete an object from the database, the object is first found with a Cursor navigation, then the Cursor method Remove() is called with the object handle. For example:

 
    cursor = new Cursor<Record>(con, "id");
    // find record with id == 3
    rec = cursor.Find(3);
    cursor.Remove(); // remove current object (pointed to by the cursor)
    cursor.Close(); //release cursor
    con.CommitTransaction();
     

Nullable Fields

A C# class field can be explicitly declared nullable with the [Nullable] attribute, or using with the standard question mark ( eg. int? i;) syntax. For example:

 
    class Record
    {
        [Indexable(Unique=true)] // Create unique (tree) eXtremeDB index by "id" field
        public int id;
 
        public int? age; 
     
        [Nullable]
        public int weight;
    }
 

Here both fields age and weight may have null values.

Please see the Nullable Fields and Indexes page for an explanation of the behavior of nullable fields included in indexes.

Strings and Arrays

The Length property of String fields can be used to determine their actual size in bytes. Also, since vectors (aka dynamic arrays) in eXtremeDB are simply declared with the native language array syntax, the field's Length property gives the number of vector elements.

In C# applications, the elements of vector (array) fields are accessed using the normal array element operator [] and specifying the index of the desired element.

Blob Support

C# applications use the [Blob] field attribute to define a BLOB field.

Date, Time and Datetime Fields

Please refer to the Datetime FIelds page for a detailed description of the eXtremeDB date, time and datetime database field types. The C# APIs for determining precision and accessing date, time and datetime fields are described in the Managing Datetime Fields in C# page.