mongodb installation

In this Application, you have the functionality given below.

You can configure the Local and server based mongodb configuration. Insert the new record in to the database collection. You can select the particular record. After selecting the particular record, you can update the selected record. After selecting the particular record, you can delete the selected record. You can select the record by, using the WPF GridView control etc. It is immediately reflected in the GridView after any transaction on MongoDB by our Application.

You have to add MongoDB driver DLL for .NET.

To downlaod, .NET driver DLL, you can refer the link given below.

mongo-csharp-driver

I had already inserted it in to the debug folder but if you have any problem, you can get from there.

After the execution, the Application image is given below.

This is all the basic code, which I am using for simple CRUD operations in WPF C# and .NET, please go through the code.

C#


using MongoDB.Bson;   
using MongoDB.Driver;   
using MongoDB.Shared;   
using MongoDB.Bson.Serialization.Attributes;   
using MongoDB.Driver.Builders;   
   
MongoClient mongo = new MongoClient("mongodb://localhost");   
MongoServer server ;   
MongoDatabase database;   
MongoCollection<info> _infos;   
   
//This all the global variable in your project.   
   
//we will initiate this all in to the intialization method   
server = mongo.GetServer();   
server.Connect();    
database= server.GetDatabase("mydb");   
   
   
//"mydb" is the mongodb database name in my system //where database is there for curd opration.   
//for insert the record in db    
   
public void addinfo(info _info)   
        {   
                
            _info.Id = ObjectId.GenerateNewId();   
               
            _infos.Insert(_info);   
        }   
   
//For binding the gridview   
   
public void bindgrid()   
        {   
            // bind the existing info collection<table> record in grid   
            _infos = database.GetCollection<info>("info");   
            infogrid.DataContext = _infos.FindAll();   
            infogrid.ItemsSource = _infos.FindAll();   
        }   
   
   
//update the data    
   
public void updateInfo(info _info)   
       {   
           IMongoQuery query = Query.EQ("info_id", _info.info_id);   
           IMongoUpdate update = Update   
                  
               .Set("firstname", _info.firstname)   
   
              .Set("lastname", _info.lastname)   
              .Set("age", _info.age);   
           SafeModeResult result = _infos.Update(query, update);   
               
        }   
   
//delete the record   
   
IMongoQuery query = Query.EQ("info_id", _info.info_id);   
SafeModeResult result = _infos.Remove(query); bindgrid(); 

Source Code files

Model, which I have created for mapping of the info collection

publicclass info {  
    [BsonId]  
    publicBson.BsonObjectId Id {  
        get;  
        set;  
    }  
    publicint info_id {  
        get;  
        set;  
    }  
    publicstring firstname {  
        get;  
        set;  
    }  
    publicstring lastname {  
        get;  
        set;  
    }  
    publicint age {  
        get;  
        set;  
    }  
}