更改datagridview时更新mongodb数据库 [英] Update mongodb database when datagridview is changed

查看:384
本文介绍了更改datagridview时更新mongodb数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#开发.net桌面应用程序.我在此应用程序中使用的数据存储在mongodb集合中.我可以在datagridview中显示数据,也可以在此datagridview中添加行,但是我无法将此更改反映到我的mongodb集合中.

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.

我的收藏集:站点和文档列表中,我存储了该收藏集的文档.这是我的代码的一部分,在datagridview中显示数据:

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;

有人对此有想法吗?

推荐答案

我希望这可以为您提供帮助.这是一个有关如何在数据库中插入"值的基本示例.

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
        }
     }
}    

别忘了看 MongoDB手册

您会看到非常有趣的主题和示例,例如"MongoDB upsert-插入或更新".此处

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

这篇关于更改datagridview时更新mongodb数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆