Mongodb存储读取Word文档
在为人事系统做操作日志功能时,为了保证已经打印的信函可以还原,需要在每次打印信函时记录Word信函的内容。 SQL Server只能记录信函的文字内容,那信函的页面布局、字体式等其他内容如何存储呢?此时Mongodb闪亮登场,由于MongoDB的文档结构为BJSON式(BJS
在为人事系统做操作日志功能时,为了保证已经打印的信函可以还原,需要在每次打印信函时记录Word信函的内容。
SQL Server只能记录信函的文字内容,那信函的页面布局、字体格式等其他内容如何存储呢?此时Mongodb闪亮登场,由于MongoDB的文档结构为BJSON格式(BJSON全称:Binary JSON),而BJSON格式本身就支持保存二进制格式的数据,因此可以把文件的二进制格式的数据直接保存到MongoDB的文档结构中。取的时候再以二进制格式取,这样文档就能实现无损保存。
下面是我已经验证成功,存储Word到Mongo,然后从Mongo读取Word的代码,在此和大家分享分享。
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() { InitializeComponent(); Init(); } //数据库连接字符串 const string strconn = "mongodb://127.0.0.1:27017"; //数据库名称 const string dbName = "test"; MongoServer server; MongoDatabase db; void Init() { //创建数据库链接 server = MongoDB.Driver.MongoServer.Create(strconn); //获得数据库 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> /// 保存Word到Mongo /// </summary> /// <param name="filename">需要保存的文件名</param> private void SaveDocToMongo(string filename) { byte[] byteDoc = File.ReadAllBytes(filename); BsonDocument doc = new BsonDocument(); doc["id"] = "1"; doc["content"] = byteDoc; MongoCollection col = db.GetCollection("doc"); col.Save(doc); } /// <summary> /// 将Mongo中的Word保存到本地 /// </summary> /// <param name="filename">保存到本地的文件名</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(); } } }

好了,时间不早了,我得小憩一下,准备下午的软考,谢谢大家观看。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

Recently, Huawei announced that it will launch a new smart wearable product equipped with Xuanji sensing system in September, which is expected to be Huawei's latest smart watch. This new product will integrate advanced emotional health monitoring functions. The Xuanji Perception System provides users with a comprehensive health assessment with its six characteristics - accuracy, comprehensiveness, speed, flexibility, openness and scalability. The system uses a super-sensing module and optimizes the multi-channel optical path architecture technology, which greatly improves the monitoring accuracy of basic indicators such as heart rate, blood oxygen and respiration rate. In addition, the Xuanji Sensing System has also expanded the research on emotional states based on heart rate data. It is not limited to physiological indicators, but can also evaluate the user's emotional state and stress level. It supports the monitoring of more than 60 sports health indicators, covering cardiovascular, respiratory, neurological, endocrine,

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

According to news on July 9, testers of Xiaomi.EU, a well-known official version of the system, recently discovered that Xiaomi has recently taken new measures to restrict devices sold in mainland China from installing the Xiaomi international version. If a user attempts to install the international version of the system on a Chinese version of the device, the device will display an unsupported message during boot and will be unable to enter the system. This mechanism can identify the market version to which the hardware belongs. For Xiaomi mobile phones sold in mainland China, if it is detected that the international version of the system is installed, it will not be able to start normally. Test results show that the flashed device will display "Unsupported software" (unsupported software) in the boot wizard and prompt that using this version may bring security risks. Currently, Xiaomi has

This article introduces how to configure MongoDB on Debian system to achieve automatic expansion. The main steps include setting up the MongoDB replica set and disk space monitoring. 1. MongoDB installation First, make sure that MongoDB is installed on the Debian system. Install using the following command: sudoaptupdatesudoaptinstall-ymongodb-org 2. Configuring MongoDB replica set MongoDB replica set ensures high availability and data redundancy, which is the basis for achieving automatic capacity expansion. Start MongoDB service: sudosystemctlstartmongodsudosys

This article describes how to build a highly available MongoDB database on a Debian system. We will explore multiple ways to ensure data security and services continue to operate. Key strategy: ReplicaSet: ReplicaSet: Use replicasets to achieve data redundancy and automatic failover. When a master node fails, the replica set will automatically elect a new master node to ensure the continuous availability of the service. Data backup and recovery: Regularly use the mongodump command to backup the database and formulate effective recovery strategies to deal with the risk of data loss. Monitoring and Alarms: Deploy monitoring tools (such as Prometheus, Grafana) to monitor the running status of MongoDB in real time, and

According to news from this website on July 23, technology media techradar published a blog post yesterday (July 22), reporting that Sony will abandon the 5.5TB cartridge storage solution. Introduction to the 5.5TB ink cartridge storage solution. Sony released the third-generation ink cartridge storage solution in 2020. The capacity of a single box reaches 5.5TB. It uses a double-sided optical disc with three layers on each side. Records can be written at one time and data can be saved. Not bad for 100 years. The full name of this cartridge storage solution is Optical Disc Archive Cartridges. Its goal is to preserve important data for a long time. The time promised by Sony is to ensure that it remains unchanged for 100 years. It has very good stability and is resistant to temperature, humidity, water, electrical, gray and electricity

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).
