Mongo DB Crud operation in MVC 3 C# in webapp.you can use same query and .dll files for aspx project.
Introduction mongo DB Crud operation in MVC 3 C# in webapp.you can use same query and .dll files for aspx project.
Building the Sample You have to installed and configure the mongodb in your windows system.Please make sure though mongodb command prompt its works .
for mongodb installation and configuration on windows sytem you can go through
Description
befor start please check your mongodb service its running or not
following this steps
after installation mongodb in your system lets test some db query .
in to the mongo db environment
Use mydb
switch to my db
others command please prefer the Image
This application shows the how to does the basic crud opration on mongo db by using wpf c#.net with wpf gridview control.
in this application you have followig functionality :-
1.you can configure the Local and server based mongodb configuration.
2.Insert the new record in to the database collection.
- You can select the perticular record.
4.after select the perticular record you can update selected record .
5.after select the perticular record you can delete the selected record.
6.you can select the record by using the wpf grid view control.
etc.
you have to add mongodb driver dll for .net .
below the controller code which i have done for transaction with Mongodb on local system.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MongoDB.Driver;
using MVCMongodbCrudOpration.Models;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
namespace MVCMongodbCrudOpration.Controllers
{
public class HomeController : Controller
{
MongoClient mongo = new MongoClient("mongodb://localhost");
MongoServer server;
MongoDatabase database;
MongoCollection<info> _infos;
info _info;
public ActionResult Index()
{
dbcon();
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
// For stablish the database connection after avery request
public void dbcon()
{
server = mongo.GetServer();
server.Connect();
database = server.GetDatabase("mydb");
_infos = database.GetCollection<info>("info");
}
public ActionResult About()
{
return View();
}
// get the list al the data of 'mydb' database
public ActionResult MongoDBHome()
{
dbcon();
return View(_infos.FindAll());
}
// add new record in to the 'mydb' database table
public void addinfo(info _info)
{
_info.Id = ObjectId.GenerateNewId();
_infos.Insert(_info);
//return item;
}
public ActionResult Create()
{ return View(); }
[HttpPost]
public ActionResult Create(MVCMongodbCrudOpration.Models.info model)
{
dbcon();
_info = new info { info_id = (int)database.GetCollection("info").Count() + 1, firstname = model.firstname, lastname = model.lastname, age = Convert.ToInt16(model.age) };
addinfo(_info);
return View("MongoDBHome",_infos.FindAll());
}
// update the existing record from database
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);
// return result.UpdatedExisting;
}
public ActionResult Edit(int id)
{
dbcon();
IMongoQuery query = Query.EQ("info_id", id);
_info = _infos.Find(query).FirstOrDefault();
return View(_info);
}
[HttpPost]
public ActionResult Edit(Models.info model)
{
dbcon();
IMongoQuery query = Query.EQ("info_id", model.info_id);
_info = _infos.Find(query).FirstOrDefault();
_info.firstname = model.firstname;
_info.lastname = model.lastname;
_info.age = Convert.ToInt16(model.age);
updateInfo(_info);
return View("MongoDBHome", _infos.FindAll());
}
[HttpPost]
public ActionResult Delete(MVCMongodbCrudOpration.Models.info model)
{
dbcon();
IMongoQuery query = Query.EQ("info_id", model.info_id);
SafeModeResult result = _infos.Remove(query);
return View("MongoDBHome", _infos.FindAll());
}
public ActionResult Delete(int id)
{
dbcon();
info _info = new info();
IMongoQuery query = Query.EQ("info_id", id);
_info = _infos.Find(query).FirstOrDefault();
return View(_info);
}
public ActionResult Details(int id)
{
dbcon();
IMongoQuery query = Query.EQ("info_id", id);
_info = _infos.Find(query).FirstOrDefault();
return View(_info);
}
}
}
Source Code Files
model for this sample
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson.Serialization.Attributes;
namespace MVCMongodbCrudOpration.Models
{
public class info
{
[BsonId]
public MongoDB.Bson.BsonObjectId Id { get; set; }
public int info_id { get; set; }
public string firstname { get; set; }
public string lastname { get; set; }
public int age { get; set; }
}
}