v1.10

Document

How to Start SequoiaDB

Basic Operation

Data Model

Database Management

Aggregation

Index

Replication

Cluster

Connector

Reference

Development

Release Note

FAQ

  • 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.

Namespace

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 = "127.0.0.1:11810";
    Sequoiadb sdb = new Sequoiadb(addr);
    try
    {
       sdb.Connect();
    }
    catch (BaseException e)
    {
       Console.WriteLine("ErrorCode:{0}, ErrorType:{1}", e.ErrorCode, e.ErrorType);
       Console.WriteLine(e.Message);
    }
    catch (System.Exception e)
    {
       Console.WriteLine(e.StackTrace);
    }

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

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

    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
    sdb.Disconnect();
  • 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
    {
       {"FirstName","John"},
       {"LastName","Smith"},
       {"Age",50},
       {"id",i},
       {"Address",
          new BsonDocument
          {
             {"StreetAddress","212ndStreet"},
             {"City","new York"},
             {"State","NY"},
             {"PostalCode","10021"}
          }
       },
       {"PhoneNumber",
          new BsonDocument
          {
             {"Type","Home"},
             {"Number","212555-1234"}
          }
       }
    };
    Insert the number of data:
    //bulkinsert
    List<bsondocument> insertor=new List <bsondocument> ();
    for(inti=0;i<10;i++)
    {
       BsonDocumentobj=new BsonDocument();
       obj.Add("operation","BulkInsert");
       obj.Add("date",DateTime.Now.ToString());
       insertor.Add(obj);
    }
    dbc.BulkInsert(insertor,0);
  • Index-related operations

    Create an index:

    //createindexkey,indexonattribute'Id'byASC(1)/DESC(-1)
    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";
    dbc.DropIndex(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;
    else
       hint = new BsonDocument();
    DBCursor cursor = dbc.Query(matcher, selector, orderBy, hint, 0, -1); 

    Use DBCursor cursor to traverse:

    while (cursor.Next() != null)
       Console.WriteLine(cursor.Current());
  • Delete operation

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

    //createthedeletecondition
    BsonDocument drop = new BsonDocument();
    drop.Add("Last Name", "Smith");
    coll.Delete(drop);
  • 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;
    dbc.Update(query); 

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

    dbc.Upsert(query);
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.

Beijing:
Tower R, No.8 North Star East Road, Chaoyang District, Beijing,China
Guangzhou:
Tower A, No.22 Qinglan Street, Panyu District, Guangzhou,China
Shenzhen:
Tsing Hua Tech Park, Nanshan District, Shenzhen,China
Tel:400-8038-339
E-mail:contact@sequoiadb.com