C#简单操作MongoDB
生活随笔
收集整理的這篇文章主要介紹了
C#简单操作MongoDB
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
新建一個窗體程序;使用Nuget安裝mongodb.driver;或者直接引用dll如下;
代碼;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using MongoDB.Bson; using MongoDB.Driver;namespace mgtest {public partial class Form1 : Form{protected static IMongoDatabase _database;protected static IMongoClient _client;public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){const string collectionName = "村子";string strCon = "mongodb://127.0.0.1:27017/mymgtest";var mongoUrl = new MongoUrlBuilder(strCon);string databaseName = mongoUrl.DatabaseName;_client = new MongoClient(mongoUrl.ToMongoUrl());_database = _client.GetDatabase(databaseName);var collection = _database.GetCollection<BsonDocument>(collectionName);var filter = new BsonDocument();var list = Task.Run(async () => await collection.Find(filter).ToListAsync()).Result;list.ForEach(p =>{textBox1.Text = textBox1.Text + p["name"].ToString() + "," + p["人數"].ToString() + "," + p["位置"].ToString() + Environment.NewLine;});}} }輸出如下;?
?
這是前文創建的mongodb數據庫;
?
總結
以上是生活随笔為你收集整理的C#简单操作MongoDB的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: centos查看模块信息和模块路径
- 下一篇: VC++ 单文档和多文档应用程序