2

I'm trying to develop .net desktop application in C#. The data that I'm using in this app is stored in a mongodb collection. I can display data in a datagridview, I can add rows in this datagridview but, I'm not able to reflect this change to my mongodb collection.

My collection : Stations and in doclist I store the documents from this collection.Here is a part of my code where I show data in the datagridview:

Mongo mongo = new Mongo();
mongo.Connect();
var database = mongo.GetDatabase("test");
var collection2 = database.GetCollection<Stations>("Stations");

BindingList<Stations> doclist = new BindingList<Stations>();

foreach (Stations stat in collection2.FindAll().Documents)
{
     doclist.Add(stat);
}
dataGridView1.DataSource = doclist;

Does anyone have an idea about this?

Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44
jack sparrow
  • 87
  • 1
  • 9

1 Answers1

0

I hope this can help you. This is a little and basic example about how to "Insert" values in your database.

MongoServer server = MongoServer.Create("mongodb://localhost");  //Creating your server
server.Connect(); //Connect your server
MongoDatabase db = server.GetDatabase("Company"); //Connect your database
using (server.RequestStart(db))
{
    for ( a = 0; a < 1; a++)
    {
        for (int i = 0; i < 5000; i++)
        {
            var col = db.GetCollection("Workers"); //This is your Collection
            col.Insert(new BsonDocument { { "Values", "123456" } }); //You need to use your values here
        }
     }
}    

Don't forget take a look to the MongoDB manual

And you can see very interesting topics and examples like "MongoDB upsert - insert or update" here

slavoo
  • 5,798
  • 64
  • 37
  • 39
Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44