MongoDB Store reads Word documents

Source: Internet
Author: User

in order to ensure that the letters you have printed can be restored, you will need to record the contents of your Word letter each time you print your letter to the personnel system.

SQL Server can only record the letter's text content, the letter's page layout, font format and other content how to store it? MongoDB is now shining, because MongoDB's document structure is Bjson format (bjson full name: Binary JSON), and the Bjson format natively supports the preservation of binary format data, Therefore, the data in the binary format of the file can be saved directly into the document structure of MongoDB. It is then taken in binary format, so that the document can be lossless saved.

Below is the code I have verified successfully, store word to MONGO, and then read word from MONGO, here and share it with everyone.

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.io;using MongoDB.Bson;using Mongodb.driver;namespace mongodb{public partial class Form1:form {public Form1 () {Init            Ializecomponent ();        Init ();        }//Database connection string Const string strconn = "mongodb://127.0.0.1:27017";        Database name const string dbName = "Test";        Mongoserver server;        Mongodatabase DB;            void Init () {//CREATE DATABASE link server = MongoDB.Driver.MongoServer.Create (strconn); Obtain database db = Server.        Getdatabase (DbName);        } private void Btnsave_click (object sender, EventArgs e) {Savedoctomongo (@ "D:\quwenzhe.docx"); } private void Btnshow_click (object sender, EventArgs e) {Getdocfrommongo (@ "E:\newquwenzhe . doc "); }///<summary>//Save Word to MONGO///</summary>/<param name= "filename" > Required File name to save </param> private void Savedoctomongo (string filename) {byte[] Bytedoc = File.readal            Lbytes (filename);            Bsondocument doc = new bsondocument ();            doc["id"] = "1";            doc["content"] = Bytedoc; Mongocollection col = db.            GetCollection ("Doc"); Col.        Save (DOC); }///<summary>//Save word in MONGO to local///</summary>/<param name= "filename"  > Save to local file name </param> private void Getdocfrommongo (string filename) {mongocollection col = Db.            GetCollection ("Doc");            var query = new Querydocument {{"id", "1"}}; var result = Col.            findas<bsondocument> (query); byte[] Buff = (byte[]) ((bsondocument) result. ToList () [0]).            GetValue ("content");            FileStream FS; FileInfo fi = new FileInfo (filename); fs = fi.            OpenWrite (); Fs. Write (buff, 0, buff.            Length); Fs.        Close (); }    }}
Once the storage operation is done, you can view the stored binary data in Mongovue as shown in:

To this, weak and weak on the source: Http://pan.baidu.com/s/1pJ5DTer.

Well, the time is not early, I have to take a nap, prepare for the afternoon soft test, thank you for watching.

MongoDB Store reads Word documents

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.