Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Database \ Use MongoDB C # MongoDB official driving operation     - SME Linux network security policy server security (Linux)

- Linux system network security tools sudo Introduction (Linux)

- How to use the Vault secure password storage and API key (Linux)

- Getting jQuery - progress bar (Programming)

- impdp error ORA-31631, ORA-39122 (Database)

- iOS developers - a simple factory pattern and OC reflection (Programming)

- MySQL EXPLAIN SQL output description (Database)

- Linux virtual machine settings network, hostname ssh access (Linux)

- Java static code analysis tool Infer (Programming)

- How to install Git on CentOS 7 (Linux)

- Java framework for parallel study - ForkJoin (Programming)

- Security of data to create a safe .mdb database (Linux)

- Linux initialization init systems - Systemd (Linux)

- Let the router be your security barrier against external attack (Linux)

- Ubuntu 14.04 kernel after the restart boot black screen to solve (Linux)

- To install PostgreSQL 9.4 (Database)

- Improve WordPress performance (Server)

- VMware virtual machine to use bridged mode fast Internet access (Linux)

- Java multi-threaded communications pipeline flow (Programming)

- Win7 + Ubuntu Kylin + CentOS 6.5 installed three systems (Linux)

 
         
  Use MongoDB C # MongoDB official driving operation
     
  Add Date : 2018-11-21      
         
         
         
  Want to use MongoDB in C #, you must first have a MongoDB supports C # version of the driver. C # version of the driver there are many, such as provided by the official, samus. Realization of ideas most similar. Here we will start with the official mongo-csharp-driver, the current version is 1.7.0.4714

download link:

http://github.com/mongodb/mongo-csharp-driver/downloads

Get two dll after compiling

 MongoDB.Driver.dll: As the name suggests, the driver

 MongoDB.Bson.dll: serialization, Json related

 Then reference these two dll in our program.

 The following section briefly demonstrates how to use C # for MongoDB CRUD operations.

Program.cs

using System;
using MongoDB.Driver;
using MongoDB.Bson;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main (string [] args)
        {
            // Database connection string
            string conn = "mongodb: //127.0.0.1: 27017";
            // Database name
            string database = "RsdfDb";
            string collection = "Act_User";

            MongoServer mongodb = MongoServer.Create (conn); // connect to the database
            MongoDatabase mongoDataBase = mongodb.GetDatabase (database); // Select the database name
            MongoCollection mongoCollection = mongoDataBase.GetCollection (collection); // select the collection, the equivalent of table
            mongodb.Connect ();

            // Insert common
            var o = new {UserID = 0, UserName = "admin", Password = "1"};
            mongoCollection.Insert (o);

            // Insert objects
            User user = new User {UserID = 1, UserName = "chenqp", Password = "1"};
            mongoCollection.Insert (user);

            // BsonDocument insert
            BsonDocument bd = new BsonDocument ();
            bd.Add ( "UserID", 2);
            bd.Add ( "UserName", "yangh");
            bd.Add ( "Password", "1");
            mongoCollection.Insert (bd);

            Console.ReadLine ();

        }
    }
}

User.cs

using MongoDB.Bson;

namespace ConsoleApplication1
{
    class User
    {
        // _ Id attribute must be, otherwise the update data being given: "Element '_id' does not match any field or property of class".
        public ObjectId _id; //BsonType.ObjectId this corresponds MongoDB.Bson.ObjectId
        public int UserID {get; set;}
        public string UserName {get; set;}
        public string Password {get; set;}
    }
}
     
         
         
         
  More:      
 
- Ubuntu 14.04 forget solution root password (Linux)
- Linux dual physical network card set to a virtual NIC (Linux)
- vector C ++ sequence containers (Programming)
- Nine tips to protect the security of Linux desktop (Linux)
- To install the latest version of the EPEL on CentOS 5.x or 6.x (Linux)
- Let CentOS6 yum upgrade to support more source rpm package (Linux)
- File permissions under Linux (Linux)
- Elasticsearch 2.20 Beginners: aggregation (Server)
- Phoenix agents use P2P WebRTC development (Programming)
- Java memory model subclasses (Programming)
- Linux hard drive failure Case Studies (Linux)
- Ubuntu development Nodejs (Linux)
- ARP Linux-related operations (Linux)
- Linux Programming memory mapping (Programming)
- Redis-- persistence articles (Database)
- Classical sorting algorithm - Selection Sort (Linux)
- Set multiple IP addresses for a single network card on Ubuntu 15.10 (Linux)
- VSFTPD Security (Linux)
- Common data structures and functions of Linux process scheduling (Programming)
- After installing minimize RHEL / CentOS 7 we need to do (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.