(1) Download the MongoDB C # driver.
http://www.nuget.org/packages/mongocsharpdriver/.
(2) Add Reference to C # driver DLLs
1mongodb.bson.dll2mongodb.driver.dll3mongodb.driver.core.dll
or Nuget download
VS 2012/13
TOOLS---Library package manager–> Manage NuGet Package for solution, then select Online, Search MongoDB, and then download driver.
(3) Connecting MONGO DB
1 /// <summary>2 ///Database Connection3 /// </summary>4 Private Const stringConnurl ="mongodb://127.0.0.1:27017";5 /// <summary>6 ///the specified database7 /// </summary>8 Private Const stringDbName ="TestDB";9 Ten protected Staticimongoclient _client; One protected Staticimongodatabase _database; A - voidLoad () { - the varURL =NewMongourl (connurl); - varSettings =mongoclientsettings.fromurl (URL); -Settings. Credentials =NewList<mongocredential>() - { +Mongocredential.createcredential (DbName,"Admin","123456") - }; + //Create a data connection A_client =NewMongoclient (); _database =_client. Getdatabase (dbName); at -}
Get Data
Async voidGetData () {Try{imongocollection<T_Role> collection = _database. Getcollection<t_role> ("dbo. T_role"); varFilter =Newbsondocument (); vardata =awaitcollection. Find (filter). Tolistasync (); if(data. Any ()) {gridcontrol1.beginupdate (); Gridcontrol1.datasource=data; Gridcontrol1.endupdate (); } } Catch(FormatException e) {//Record Log}}
Note If this is formatted with a conversion error to the image t_role. Then, add _id in the entity table
MONGO DB for C #