Step 3: Data Access Operations

SDK sample samples/csharp/Operations illustrates how to use the Connection and Cursor classes to perform basic database CRUD operations. Please build and run the sample application and examine the source code. Note that the Connection con is instantiated after the database is opened:

 
    Database db = new Database();
    ...
    db.Open("operations-db", params, DATABASE_SIZE);
     
    Connection con = new Connection(db);
         

Then persistent Record objects are instantiated and inserted into the database using the Connection method Insert(). Note that all database operations are performed between Connection method StartTransaction() and CommitTransaction():

 
    con.StartTransaction(Database.TransactionType.ReadWrite); // start RW transaction
    rec = new Record(); // create Java object
    // fill data
    rec.id = i;
    rec.str = "String " + i;
 
    con.Insert(rec); // Insert object into eXtremeDB database
    con.CommitTransaction(); // Commit changes
     

To update records a Cursor object is instantiated and its method Update() is used to modify the object contents:

 
    con.StartTransaction(Database.TransactionType.ReadWrite);
    // Perform simple index search: locate Record by id
    cursor = new Cursor<Record>(con,  "id");
    // Find record to update
    rec = cursor.Find(2);
    // Update object
    rec.str = "Updated string";
    cursor.Update(); // Update current object (pointed by the cursor) in eXtremeDB database
    cursor.Close(); // Release cursor
    con.CommitTransaction(); // commit changes