CSharp开发基础

    在使用 CSharp 驱动的相关 API 之前,你必须在源代码中添加如下的 using 申明:

    数据操作

    • 连接数据库和身份验证

    若数据库没有创建用户,则可以匿名连接到数据库:

    1. string addr = "127.0.0.1:11810";
    2. Sequoiadb sdb = new Sequoiadb(addr);
    3. try
    4. {
    5. sdb.Connect();
    6. }
    7. catch (BaseException e)
    8. {
    9. Console.WriteLine("ErrorCode:{0}, ErrorType:{1}", e.ErrorCode, e.ErrorType);
    10. Console.WriteLine(e.Message);
    11. }
    12. catch (System.Exception e)
    13. {
    14. Console.WriteLine(e.StackTrace);
    15. }

    否则,连接的时候必须指定用户名和密码:

    1. string addr = "127.0.0.1:11810";
    2. Sequoiadb sdb = new Sequoiadb(addr);
    3. try
    4. {
    5. sdb.Connect("testusr", "testpwd");
    6. }
    7. catch (BaseException e)
    8. {
    9. Console.WriteLine("ErrorCode:{0}, ErrorType:{1}", e.ErrorCode, e.ErrorType);
    10. Console.WriteLine(e.Message);
    11. }
    12. catch (System.Exception e)
    13. {
    14. Console.WriteLine(e.StackTrace);
    15. }

    这里给出了异常信息的 try 和 catch 块,下面的所有操作都会抛出同样的异常信息,因此不再给出相关的 try 和 catch 块。

    • 断开与数据库连接
    1. // do not forget to disconnect from sdb
    2. sdb.Disconnect();
    • 得到或创建集合空间和集合
    1. // create collectionspace, if collectionspace exists get it
    2. string csName = "TestCS";
    3. if (cs == null)
    4. cs = sdb.CreateCollectionSpace(csName);
    5. // or sdb.CreateCollectionSpace(csName, pageSize), need to specify the pageSize

    根据名字,得到对应的 Collection,如果不存在,则创建:

    • 对 Collection 进行插入操作

    创建需要插入的数据 BsonDocument 并插入:

    1. BsonDocument insertor = new BsonDocument();
    2. string date = DateTime.Now.ToString();
    3. insertor.Add("operation", "Insert");
    4. insertor.Add("date", date);
    5. dbc.Insert(insertor);

    当然,BsonDocument 中还可以嵌套 BsonDocument 对象;而且你还可以直接 new 一个完整的 BsonDocument,而不需要通过 Add 方法:

    1. BsonDocumentinsertor = new BsonDocument
    2. {
    3. {"FirstName","John"},
    4. {"LastName","Smith"},
    5. {"Age",50},
    6. {"id",i},
    7. {"Address",
    8. new BsonDocument
    9. {
    10. {"StreetAddress","212ndStreet"},
    11. {"City","NewYork"},
    12. {"State","NY"},
    13. {"PostalCode","10021"}
    14. }
    15. },
    16. {"PhoneNumber",
    17. new BsonDocument
    18. {
    19. {"Type","Home"},
    20. {"Number","212555-1234"}
    21. }
    22. }
    23. };

    插入多条数据:

    1. //bulkinsert
    2. List< BsonDocument > insertor=new List < BsonDocument > ();
    3. for(int i=0;i<10;i++)
    4. {
    5. BsonDocument obj=new BsonDocument();
    6. obj.Add("operation","BulkInsert");
    7. insertor.Add(obj);
    8. }
    9. dbc.BulkInsert(insertor,0);
    • 索引的相关操作

    创建索引:

    1. //createindexkey,indexonattribute'Id'byASC(1)/DESC(-1)
    2. BsonDocument key = new BsonDocument();
    3. key.Add("id", 1);
    4. string name = "index name";
    5. bool isUnique = true;
    6. bool isEnforced = true;
    7. dbc.CreateIndex(name, key, isUnique, isEnforced);
    • 查询操作

    进行查询操作,需要使用游标对查询结果进行遍历,而且可以先得到当前 Collection 的索引,如果不为空,可作为制定访问计划(hint)用于查询:

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

    构建相应的 BsonDocument 对象用于查询,包括:查询匹配规则(matcher,包含相应的查询条件),域选择(selector),排序规则(orderBy,增序或降序),制定访问计划(hint),跳过记录个数(0),返回记录个数(-1:返回所有数据)。查询后,得到对应的 Cursor,用于遍历查询得到的结果:

    1. BsonDocument matcher = new BsonDocument();
    2. BsonDocument conditon = new BsonDocument();
    3. conditon.Add("$gte", 0);
    4. conditon.Add("$lte", 9);
    5. matcher.Add("id", conditon);
    6. BsonDocument selector = new BsonDocument();
    7. selector.Add("id", null);
    8. selector.Add("Age", null);
    9. BsonDocument orderBy = new BsonDocument();
    10. orderBy.Add("id", -1);
    11. BsonDocument hint = null;
    12. if (index != null)
    13. hint = index;
    14. else
    15. hint = new BsonDocument();
    16. DBCursor cursor = dbc.Query(matcher, selector, orderBy, hint, 0, -1);

    使用 DBCursor 游标进行遍历:

    1. while (cursor.Next() != null)
    2. Console.WriteLine(cursor.Current());
    • 删除操作

    构建相应的 BsonDocument 对象,用于设置删除的条件:

    1. //createthedeletecondition
    2. BsonDocument drop = new BsonDocument();
    3. drop.Add("Last Name", "Smith");
    4. coll.Delete(drop);
    • 更新操作

    构建相应的 BsonDocument 对象,用于设置更新条件,你还可以创建 DBQuery 对象封装所有的查询或更新规则: