How to Start SequoiaDB

Basic Operation

Data Model

Database Management








Release Note


  • Content
  • Comments
  • History

C# Development Foundation

Here's how to use the driver interface written in C# using SequoiaDB database program. This document describes a simple C# sample SequoiaDB database-driver, detailed specification can refer to the official use of C# API documentation.


Before using C# driver related API, you must add the following using declaration in source code:

using SequoiaDB;

using SequoiaDB.Bson;

Data Manipulation

  • Connect to the database and authentication

    If the database does not create a user, you can anonymously connect to the database:

    string addr = "";
    Sequoiadb sdb = new Sequoiadb(addr);
    catch (BaseException e)
       Console.WriteLine("ErrorCode:{0}, ErrorType:{1}", e.ErrorCode, e.ErrorType);
    catch (System.Exception e)

    Otherwise, when the connection must specify a user name and password:

    string addr = "";
    Sequoiadb sdb = new Sequoiadb(addr);
       sdb.Connect("testusr", "testpwd");
    catch (BaseException e)
       Console.WriteLine("ErrorCode:{0}, ErrorType:{1}", e.ErrorCode, e.ErrorType);
    catch (System.Exception e)

    Here are the exception information try and catch blocks, all operations following will throw the same exception information, it is no longer relevant given the try and catch blocks.

  • Disconnect from the database
    // do not forget to disconnect from sdb
  • Get or create a collection space and collections

    By name, to give the corresponding CollectionSpace, if not, create:

    // create collectionspace, if collectionspace exists get it
    string csName = "TestCS";
    CollectionSpace cs = sdb.GetCollecitonSpace(csName);
    if (cs == null)
       cs = sdb.CreateCollectionSpace(csName);
    // or sdb.CreateCollectionSpace(csName, pageSize), need to specify the pageSize

    By name, to give the corresponding Collection, and if not, create:

    // create collection, if collection exists get it
    string clName = "TestCL";
    DBCollection dbc = cs.GetCollection(clName);
    if (dbc == null)
       dbc = cs.CreateCollection(clName);
    //or cs.createCollection(collectionName, options), create collection with some options
  • Insert operation for Collection

    Need to insert data BsonDocument create and insert:

    BsonDocument insertor = new BsonDocument();
    string date = DateTime.Now.ToString();
    insertor.Add("operation", "Insert");
    insertor.Add("date", date);
    ObjectId id = dbc.Insert(insertor);

    Of course, BsonDocument BsonDocument can also be nested object, And you can also directly a complete new BsonDocument, without going through the Add method:

    BsonDocumentinsertor = new BsonDocument
          new BsonDocument
             {"City","new York"},
          new BsonDocument
    Insert the number of data:
    List<bsondocument> insertor=new List <bsondocument> ();
       BsonDocumentobj=new BsonDocument();
  • Index-related operations

    Create an index:

    BsonDocument key = new BsonDocument();
    key.Add("id", 1);
    string name = "index name";
    bool isUnique = true;
    bool isEnforced = true;
    dbc.CreateIndex(name, key, isUnique, isEnforced);

    Delete Index:

    string name = "index name";
  • Query operation

    Query, you need to use a cursor to traverse the query results, but can first get the index of the current Collection, if not empty, as the development of an access plan (hint) for inquiries:

    DBCursor icursor = dbc.GetIndex(name);
    BsonDocument index = icursor.Current(); 

    BsonDocument objects are used to construct the corresponding queries, including: Query matching rules (matcher, include the appropriate query), domain selection (selector), sorting rules (oredrBy, in increasing order or descending), to develop access plans (hint), skip record number (0), the number of records returned (-1:return all data). After the inquiry, to obtain the corresponding Cursor, for the results obtained by traversing the query:

    BsonDocument matcher = new BsonDocument();
    BsonDocument conditon = new BsonDocument();
    conditon.Add("$gte", 0);
    conditon.Add("$lte", 9);
    matcher.Add("id", conditon);
    BsonDocument selector = new BsonDocument();
    selector.Add("id", null);
    selector.Add("Age", null);
    BsonDocument orderBy = new BsonDocument();
    orderBy.Add("id", -1);
    BsonDocument hint = null;
    if (index != null)
       hint = index;
       hint = new BsonDocument();
    DBCursor cursor = dbc.Query(matcher, selector, orderBy, hint, 0, -1); 

    Use DBCursor cursor to traverse:

    while (cursor.Next() != null)
  • Delete operation

    Construct the corresponding BsonDocument object, set the conditions for deletion:

    BsonDocument drop = new BsonDocument();
    drop.Add("Last Name", "Smith");
  • Update operation

    Construct the corresponding BsonDocument object used to set the update condition, you can also create DBQuery object encapsulates all of the query or update rules:

    DBQuery query = new DBQuery();
    BsonDocument updater = new BsonDocument();
    BsonDocument matcher = new BsonDocument();
    BsonDocument modifier = new BsonDocument();
    updater.Add("Age", 25);
    modifier.Add("$set", updater);
    matcher.Add("First Name", "John");
    query.Matcher = matcher;
    query.Modifier = modifier;

    Update operation, if the conditions are not met matcher, then insert this record:

please login to comment.
Latest Comment
  • 2015-03-14

About Us

SequoiaDB is a financial-level distributed database vendor and is the first Chinese database listed in Gartner’s Magic Quadrant OPDBMS report. SequoiaDB has recently released version 3.0.
SequoiaDB is now penetrating the vertical sector Financial Industry quickly and had more than 50 banking clients and hundreds of enterprise customers in industries including government, telecommunication, Internet and IoT.

Tower R, No.8 North Star East Road, Chaoyang District, Beijing,China
Tower A, No.22 Qinglan Street, Panyu District, Guangzhou,China
Tsing Hua Tech Park, Nanshan District, Shenzhen,China