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
- Go to http://www.mongodb.org/downloads Download 32bit zip file.
- After download, extract the file to c::/.
- Change <MongoDB 2.6 Standard> folder name to <MongoDB>
- Go to C:\MongoDb\ and create tow folder.
- Data
- Log
- mongodb.log(Text File)
- Go back int to the C:\mongodb \
Create config file for MongoDB.
<Mongo.config> create in the mongodb folder
- Open mongo.config file and paste this.
dbpath=C:\mongodb\Data
logpath=C:\mongodb\Log\mongodb.log
diaglog=3
Save the file.
- Go to the Wind+R or run and type cmd and open with an administrator.
or
Open DOS as an administrator.
- Change Directory c:\mongodb\bin\>
- c:\mongodb\bin\>mongod.exe --config="c:\mongodb\mongo.config" press enter
- Open new command prompt with an administrator.
- Go to c::\mongodb\bin\> mongo.
- 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.
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.
- 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.
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.
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;
-
-
-
-
- server = mongo.GetServer();
- server.Connect();
- database= server.GetDatabase("mydb");
-
-
-
-
-
- public void addinfo(info _info)
- {
-
- _info.Id = ObjectId.GenerateNewId();
-
- _infos.Insert(_info);
- }
-
-
-
- public void bindgrid()
- {
-
- _infos = database.GetCollection<info>("info");
- infogrid.DataContext = _infos.FindAll();
- infogrid.ItemsSource = _infos.FindAll();
- }
-
-
-
-
- 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);
-
- }
-
-
-
- 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 <table>
C#
- 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;
- }
- }