MONGO DB for C #

Source: Internet
Author: User

(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 #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.