下面是我已经验证成功,存储Word到Mongo,然后从Mongo读取Word的代码,在此和大家分享分享。
using System;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
using ;
namespace Mongodb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Init();
}
//数据库连接字符串
const string strconn = "mongodb://127.0.0.1:27017";
//数据库名称
const string dbName = "test";
MongoServer server;
MongoDatabase db;
void Init()
{
//创建数据库链接
server = (strconn);
//获得数据库
db = (dbName);
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveDocToMongo(@"d:\");
}
private void btnShow_Click(object sender, EventArgs e)
{
GetDocFromMongo(@"E:\");
}
/// <summary>
/// 保存Word到Mongo
/// </summary>
/// <param name="filename">需要保存的文件名</param>
private void SaveDocToMongo(string filename)
{
byte[] byteDoc = (filename);
BsonDocument doc = new BsonDocument();
doc["id"] = "1";
doc["content"] = byteDoc;
MongoCollection col = ("doc");
(doc);
}
/// <summary>
/// 将Mongo中的Word保存到本地
/// </summary>
/// <param name="filename">保存到本地的文件名</param>
private void GetDocFromMongo(string filename)
{
MongoCollection col = ("doc");
var query = new QueryDocument { { "id", "1" } };
var result = <BsonDocument>(query);
byte[] buff = (byte[])((BsonDocument)()[0]).GetValue("content");
FileStream fs;
FileInfo fi = new FileInfo(filename);
fs = ();
(buff, 0, );
();
}
}
}
执行完存储操作后,大家可以在MongoVUE中查看存储的二进制数据,如下图所示