MongoDB文件操作(支持大于4M数据)
生活随笔
收集整理的這篇文章主要介紹了
MongoDB文件操作(支持大于4M数据)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// MongoDB連接串,以[mongodb://]開頭。這里,我們連接的是本機的服務 string connectionString = "mongodb://localhost"; // 連接到一個MongoServer上 MongoServer server = MongoServer.Create(connectionString); // 打開數據庫testdb MongoDatabase db = server.GetDatabase("testdb"); //文件操作,支持大于4M的數據 MongoGridFS fs = new MongoGridFS(db); //寫入 using (MongoGridFSStream sw = fs.Create("tmp.xml")) { byte[] fileByte = File.ReadAllBytes(AppDomain.CurrentDomain.BaseDirectory + "MongoDB.Bson.xml"); sw.Write(fileByte, 0, fileByte.Length); } //讀取 using (MongoGridFSStream sr = fs.OpenRead("tmp.xml")) { byte[] fileByte=new byte[sr.Length]; sr.Read(fileByte, 0, (int)sr.Length); File.WriteAllBytes("tmp.xml", fileByte); } //刪除文件 fs.Delete("tmp.xml");
總結
以上是生活随笔為你收集整理的MongoDB文件操作(支持大于4M数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tomcat、Servlet、Servl
- 下一篇: Axure5.1不能输入中文问题.