如何保存在SQL Server数据库文件,如果有文件的路径? [英] How to save file in SQL Server database if have file path?

查看:339
本文介绍了如何保存在SQL Server数据库文件,如果有文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一些C#桌面应用程序,我需要的文件保存到数据库中。我想出了这给我的文件正确路径一些文件选择。现在我有问题,如何使用它的路径来保存文件到数据库中。


解决方案

 的FileStream FS =新的FileStream(文件名,FileMode.Open,FileAccess.Read) ; 
BinaryReader BR =新BinaryReader(FS);
INT的numBytes =新的FileInfo(文件名)。长度;
字节[] = BUFF br.ReadBytes(的numBytes);



然后你把它上传到DB其他任何事情一样,我假设你正在使用varbinary列(BLOB)


I am building some C# desktop application and I need to save file into database. I have come up with some file chooser which give me correct path of the file. Now I have question how to save that file into database by using its path.

解决方案

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
int numBytes = new FileInfo(fileName).Length;
byte[] buff = br.ReadBytes(numBytes);

Then you upload it to the DB like anything else, I'm assume you are using a varbinary column (BLOB)

这篇关于如何保存在SQL Server数据库文件,如果有文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆