如何从mongodb中获取数据并在Windows窗体C#中以datagridform显示数据?

时间:2018-08-10 11:45:50

标签: c# mongodb winforms datagridview

任何人都可以帮助您如何从Mongodb获取数据并在Datagridview中显示该数据。在这里我尝试了这个。任何人都可以给出更好的答案

        var ConnectionString = "mongodb://localhost";
        var client = new MongoClient(connectionString);
       // var server = client.GetServer();
        var database = client.GetDatabase("testdb1");
        var collection = database.GetCollection<Entity>("tablo1");

        //var entity = collection.FindAll();

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

        foreach (var deger in collection.Find(doclist))

            doclist.Add(deger);
            //string[] row1 = new string[] { deger.deger1.ToString() };
            //dataGridView1.Rows.Add(row1);
            Application.DoEvents();
        }

1 个答案:

答案 0 :(得分:0)

尝试一下:

        var connectionString = "mongodb://localhost";
        var client = new MongoClient(connectionString);
        var server = client.GetServer();
        var database = server.GetDatabase("DB Name");
        var collection = database.GetCollection<Class Name>("Table Name");

        List<Class Name> list = await collection .Find(x => true).ToListAsync();

        dataGridView1.DataSource = list