二进制与文件的互相转换,以及在数据库中保存

------------------------将文件转换为二进制,并保存到数据库中-----------------------------------------------------

            string strResult = strPath + @"/result.doc";

            System.IO.FileStream fs = new System.IO.FileStream(@strResult, System.IO.FileMode.Open);
            fs.Position = 0;
            byte[] content = new byte[fs.Length];
            fs.Read(content, 0, (int)fs.Length);
            fs.Close();
            fluidDesignDoc.qhse1 = content;//其中qhse1在数据库中保存的类型为OLE对象


            IFluidDesignDocManager manager = new FluidDesignDocManager();
            manager.UpdateFluidDesignDoc(fluidDesignDoc);

 

 

-------------------------将二进制数据转化为指定位置文件----------------------------------------

 

private void getFile(byte[] content, string filePath)
        {
            string fileName = filePath;

            if (System.IO.File.Exists(fileName))
            {
                System.IO.File.Delete(fileName);
            }


            //FileStream sw = new FileStream(@fileName, FileMode.Create);
            //StreamWriter fs = new StreamWriter(sw, Encoding.UTF8);
            //fs.Write(entity.Content);
            System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
            fs.Write(content, 0, content.Length);
            fs.Flush();
            fs.Close();
            fileName = System.IO.Path.Combine(Application.StartupPath, fileName);
        }

 

 

你可能感兴趣的:(二进制与文件的互相转换,以及在数据库中保存)