如何使用简单的插入语句将二进制文件数据插入到二进制 SQL 字段中?
我有一个 SQL Server 2000,其中有一个包含 image
列的表。
如何通过指定文件路径将文件的二进制数据插入到该列中?
CREATE TABLE Files
(
FileId int,
FileData image
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信这会在附近的某个地方。
需要注意的是,上面的代码在 SQL Server 2005 和 SQL Server 2008 中运行,数据类型为
varbinary(max)
。 未使用图像作为数据类型进行测试。I believe this would be somewhere close.
Something to note, the above runs in SQL Server 2005 and SQL Server 2008 with the data type as
varbinary(max)
. It was not tested with image as data type.如果您的意思是使用文字,则只需创建一个二进制字符串:
并且您将拥有一条 FileData 字段包含六字节值的记录。
您在注释中指出您只想指定文件名,而 SQL Server 2000(或我知道的任何其他版本)无法执行此操作。
您需要一个 CLR 存储过程来在 SQL Server 2005/2008 中执行此操作,或者需要一个扩展存储过程(但除非必须,否则我会不惜一切代价避免这样做),它获取文件名,然后插入数据(或返回字节)字符串,但可能会很长)。
关于只能从 SP/查询获取数据的问题,我想说答案是肯定的,因为如果你赋予 SQL Server 从文件系统读取文件的能力,那么当你不这样做时你会做什么?通过Windows身份验证连接,使用什么用户来确定权限? 如果您以管理员身份运行该服务(上帝禁止),那么您可能会获得不应该允许的权限提升。
If you mean using a literal, you simply have to create a binary string:
And you will have a record with a six byte value for the FileData field.
You indicate in the comments that you want to just specify the file name, which you can't do with SQL Server 2000 (or any other version that I am aware of).
You would need a CLR stored procedure to do this in SQL Server 2005/2008 or an extended stored procedure (but I'd avoid that at all costs unless you have to) which takes the filename and then inserts the data (or returns the byte string, but that can possibly be quite long).
In regards to the question of only being able to get data from a SP/query, I would say the answer is yes, because if you give SQL Server the ability to read files from the file system, what do you do when you aren't connected through Windows Authentication, what user is used to determine the rights? If you are running the service as an admin (God forbid) then you can have an elevation of rights which shouldn't be allowed.