First: Drive
If ASP. NET wants to use a link class that mongodb,.net does not own. Third-party or official link classes.
Of course there are many kinds of drivers, and I will not introduce them.
Today I'll introduce a driver-----MongoDB that I use more often.
Next, we're going to download MongoDB's C # driver, which allows us to use MongoDB in C #. : Https://github.com/samus/mongodb-csharp
The driver we need to access MongoDB in C # is the project MongoDB. Compile this project to get it, file name: MongoDB.dll
Using MongoDB in ASP.
First start MongoDB, I have already introduced in the previous article here does not introduce.
Create an Entity class user table
[CSharp]View Plaincopy
- Public partial class user{
- [MongoId]
- public string uid{ get; set;}
- public string name{ get; set;}
- public string sex{ get; set;}
- public int age{ get; set;}
- }
Description: This is a simple class, and [MongoId] in the code is also not possible, if it is written, he will map the "_id" field.
MongoDB Database First build a database called "dbmy", Build a collection (table) "User", in the creation of documents (data).
For example, we need to introduce MongoDB.dll
[CSharp]View Plaincopy
- Private String _connectionstring = "server=127.0.0.1"; //Database server IP or address
- Private String _dbname = "dbmy";
Add data
[CSharp]View Plaincopy
- Public void Insert (user user)
- {
- User. UID = Guid.NewGuid (). ToString ("N");
- //Create a connection first
- using (Mongo Mongo = new Mongo (_connectionstring)) {
- //Open Connection
- Mongo. Connect ();
- //Switch to the specified database
- var db = Mongo. Getdatabase (_dbname);
- //Get the appropriate collection by type
- var collection = db. Getcollection<user> ();
- //Insert an object into the collection
- Collection. Insert (customer);
- }
- }
Delete data
[CSharp]View Plaincopy
- Public void Delete (string UId)
- {
- using (Mongo Mongo = new Mongo (_connectionstring)) {
- Mongo. Connect ();
- var db = Mongo. Getdatabase (_dbname);
- var collection = db. Getcollection<customer> ();
- //Delete the specified object from the collection
- Collection. Remove (x = X.uid = = UID);
- }
- }
modifying data
[CSharp]View Plaincopy
- Public void Update (user user)
- {
- using (Mongo Mongo = new Mongo (_connectionstring)) {
- Mongo. Connect ();
- var db = Mongo. Getdatabase (_dbname);
- var collection = db. Getcollection<user> ();
- //Update Objects
- Collection. Update (user, (x = X.uid = = user. UID));
- }
- }
Get Data
[CSharp]View Plaincopy
- Public User GetById (string UId)
- {
- using (Mongo Mongo = new Mongo (_connectionstring)) {
- Mongo. Connect ();
- var db = Mongo. Getdatabase (_dbname);
- var collection = db. Getcollection<user> ();
- //Querying a single object
- return collection. FindOne (x = X.uid = = UID);
- }
- }
Call
If the operation method is encapsulated in a class called Test.cs.
[CSharp]View Plaincopy
- Test t=new test ();
- Inserting data
- T.insert (User);
- Update data
ASP. NET uses MongoDB first experience