1. 添加dll
MongoDB.Bson.dll
MongoDB.Driver.dll
MongoDB.Driver.TestHelpers.dll
2. 引用命名空间
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.GridFS;
3.使用如下代码:
MongoServerAddress address = new MongoServerAddress("localhost", 27017);
MongoServerSettings setting = new MongoServerSettings();
setting.Server = address;
MongoServer server = new MongoServer(setting);
MongoDatabase mydb = server.GetDatabase("dxl");
string localFileName = @"C:\MongoDB\data\C#高级编程(第7版).pdf";
string mongoDBFileName = "C#高级编程(第7版).pdf";
MongoGridFSSettings fsSetting = new MongoGridFSSettings(){Root = "dxl"};
MongoGridFS gridfs = new MongoGridFS(mydb, fsSetting);
gridfs.Upload(localFileName, mongoDBFileName);
效果:
原文:http://blog.csdn.net/dingxl555/article/details/44679557