How to use MongoDB with C#
C Programming
Hello,
in this article we will examine how MongoDB is integrated with C#.
NoSQL systems are grouped into three groups based on document-based, key/value-based, and graphic-based. MongoDB is document-based and is the easiest to use.
The current version of mongodb is 2.4.6. http://www.mongodb.org/downloads you can download it free of charge from.
After downloading what is appropriate to our system C: under the folder called mongodb create a zip file into this folder.
Keeping our database files C:datadb our folder needs to be found. ( Note : This folder is fixed, and should not be in a different job. )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | try 02 { 03 var conn = new MongoDB.Mongo(); 04 conn.Connect(); 05 MessageBox.Show("Connected"); 06 } 07 catch (Exception ex) 08 { 09 MessageBox.Show(ex.Message); 10 } |
the above code will test our connection to our database.
Create a class called Personel
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Personel { public string personelAdi { set; get; } public int numarasi {set; get; } public Personel() { } public override string ToString() { return this.personelAdi; } } |
Insert
1 2 3 4 5 6 7 8 9 10 11 12 13 | try { var mongo = new MongoDB.Mongo(); mongo.Connect(); var db = mongo.GetDatabase("DemoDB"); var coll = db.GetCollection<Personel>(); coll.Save(p); MessageBox.Show("kaydedildi"); } catch (Exception ex) { MessageBox.Show(ex.Message); } |
Update
1 2 3 4 5 6 7 8 9 10 | public void update(Personel p){ var mongo = new MongoDB.Mongo(); mongo.Connect(); var db = mongo.GetDatabase("Demo"); var coll = db.GetCollection<Personel>(); p.personelAdi = "Duran"; coll.Save(p); } |
Select
1 2 3 4 5 6 7 8 9 | public List<Personel> getPersolonels() { var mongo = new MongoDB.Mongo(); mongo.Connect(); var db = mongo.GetDatabase("DemoDB"); var personels = db.GetCollection<Personel>().FindAll().Documents; return personels.ToList() ; } |
As you can see, with MongoDB, database operations can be easily done, such systems in large projects save us from writing long codes.
I share a link to some of the MongoDB management systems :