Building the sample

You have to install and configure it in your Windows system. Please make sure though MongoDB command prompt that it works.

For MongoDB installation and configuration on Windows sytem, you can go through

  • http://social.technet.microsoft.com/wiki/contents/articles/26542.mongo-db-installation-in-windows-7.aspx
  • http://prmdpandit.jbko.in/Archive/2014/9/mongo%20db%20installation%20in%20windows

or

Follow the steps given below.

MongoDB installation

  1. Go to http://www.mongodb.org/downloads Download 32bit zip file.
  2. After download, extract the file to c::/.
  3. Change <MongoDB 2.6 Standard> folder name to <MongoDB>
  4. Go to C:\MongoDb\ and create tow folder.

    1. Data
    2. Log

      1. mongodb.log(Text File)

  5. Go back int to the C:\mongodb \
    1. Create config file for MongoDB.

      <Mongo.config> create in the mongodb folder

  6. Open mongo.config file and paste this.

    dbpath=C:\mongodb\Data
    logpath=C:\mongodb\Log\mongodb.log
    diaglog=3

    Save the file.

  7. Go to the Wind+R or run and type cmd and open with an administrator.

    or

    Open DOS as an administrator.

  8. Change Directory c:\mongodb\bin\>
  9. c:\mongodb\bin\>mongod.exe --config="c:\mongodb\mongo.config" press enter

    WPF

  10. Open new command prompt with an administrator.
  11. Go to c::\mongodb\bin\> mongo.

    WPF

  12. Now, the Server will start and you are in MongoDB prompt.

    > use test
    >db

Description

Befor starting, please check your MongoDB Service, if it's running or not.

Follow the steps given below.

After installing MongoDB in your system, let's test some DB query in to MongoDB environment.

>Use mydb <press enter>

switch to my db

For other commands, please refer to the image given below.

WPF

This Application shows how the basic CRUD opration on MongoDB runs by using wpf C# and .NET with WPF GridView control.

In this Application, you have the functionality given below.

  1. You can configure the Local and server based mongodb configuration.
  2. Insert the new record in to the database collection.
  3. You can select the particular record.
  4. After selecting the particular record, you can update the selected record.
  5. After selecting the particular record, you can delete the selected record.
  6. 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.

WPF

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

https://github.com/mongodb/mongo-csharp-driver/releases

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.

WPF

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#

  1. using MongoDB.Bson;
  2. using MongoDB.Driver;
  3. using MongoDB.Shared;
  4. using MongoDB.Bson.Serialization.Attributes;
  5. using MongoDB.Driver.Builders;
  6. MongoClient mongo = new MongoClient("mongodb://localhost");
  7. MongoServer server ;
  8. MongoDatabase database;
  9. MongoCollection<info> _infos;
  10. //This all the global variable in your project.
  11. //we will initiate this all in to the intialization method
  12. server = mongo.GetServer();
  13. server.Connect();
  14. database= server.GetDatabase("mydb");
  15. //"mydb" is the mongodb database name in my system //where database is there for curd opration.
  16. //for insert the record in db
  17. public void addinfo(info _info)
  18. {
  19. _info.Id = ObjectId.GenerateNewId();
  20. _infos.Insert(_info);
  21. }
  22. //For binding the gridview
  23. public void bindgrid()
  24. {
  25. // bind the existing info collection<table> record in grid
  26. _infos = database.GetCollection<info>("info");
  27. infogrid.DataContext = _infos.FindAll();
  28. infogrid.ItemsSource = _infos.FindAll();
  29. }
  30. //update the data
  31. public void updateInfo(info _info)
  32. {
  33. IMongoQuery query = Query.EQ("info_id", _info.info_id);
  34. IMongoUpdate update = Update
  35. .Set("firstname", _info.firstname)
  36. .Set("lastname", _info.lastname)
  37. .Set("age", _info.age);
  38. SafeModeResult result = _infos.Update(query, update);
  39. }
  40. //delete the record
  41. IMongoQuery query = Query.EQ("info_id", _info.info_id);
  42. SafeModeResult result = _infos.Remove(query); bindgrid();

Source Code files

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

C#
  1. publicclass info {
  2. [BsonId]
  3. publicBson.BsonObjectId Id {
  4. get;
  5. set;
  6. }
  7. publicint info_id {
  8. get;
  9. set;
  10. }
  11. publicstring firstname {
  12. get;
  13. set;
  14. }
  15. publicstring lastname {
  16. get;
  17. set;
  18. }
  19. publicint age {
  20. get;
  21. set;
  22. }
  23. }