mongoDB之C#and.net Driver
之前一直都是用NodeJS来连接操作mongoDB的,但是最近需要用C#操作mongoDB的需要,所以研究了下C#驱动。mongoDB官方在GitHub上提供了C#驱动源码https://github.com/mongodb/mongo-csharp-driver。源码下载好之后编译得到MongoDB.Bson.dll和MongoDB.Driver.dll两个文件,我已经编译好了http://yunpan.cn/QCFn3v86KaEST访问密码 f0ef。将两个文件添加到项目引用然后在程序中添加相应的using语句。然后对其进行简单的操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Bson;
using MongoDB.Driver;
namespace MongoDBDriverLearn
{
class Program
{
static void Main(string[] args)
{
var client = new MongoClient("mongodb://sa:sa@localhost:27017");
var server = client.GetServer();
var database = server.GetDatabase("demoDatabase");
var collection = database.GetCollection("demoCollection");
collection.Insert(new BsonDocument("Name", "Jack"));
foreach (var document in collection.FindAll())
{
Console.WriteLine(document["Name"]);
}
Console.Read();
}
}
}
页:
[1]