简体   繁体   English

将文件存储到字节数组

[英]Storing files to byte array

I have a database object that has a column to store files as varbinary .我有一个数据库 object ,它有一列将文件存储为varbinary I have tried to store single file using C# and byte arrays and it worked.我尝试使用 C# 和字节 arrays 存储单个文件并且它有效。 How can I add multiple files to this column.如何将多个文件添加到此列。 Please help.请帮忙。

I would suppose you'd need to concat the byte arrays from each file into a giant byte array and then insert that byte array into the field, but then how would know where 1 file begins and the next ends?我想您需要将每个文件中的字节 arrays 连接到一个巨大的字节数组中,然后将该字节数组插入到该字段中,但是如何知道一个文件从哪里开始,下一个文件在哪里结束?

You could try to put in a magic set of bytes between each file byte array, but then what happens when one of those files randomly has that magic set of bytes?您可以尝试在每个文件字节数组之间放入一组神奇的字节,但是当其中一个文件随机具有该神奇的字节集时会发生什么?

If the files are the same exact type, say images, you could look for the magic bytes certain image file types always start with to separate them out, but again, there's still the random chance you might find those magic bytes in the middle of one of the files.如果文件是相同的确切类型,例如图像,您可以查找某些图像文件类型总是以开头的魔术字节将它们分开,但同样,您仍然有随机机会在其中找到这些魔术字节的文件。

There is also memory concerns both saving and retrieving if the combined files are too large.如果组合文件太大,还有 memory 涉及保存和检索。

This idea also violates database design / normalization.这个想法也违反了数据库设计/规范化。

I would do what Jeremy Lakeman recommends: create a child table.我会按照 Jeremy Lakeman 的建议去做:创建一个子表。

IE, IE,

Files Table Columns: ParentID (foreign key to parent table) FileID (Autonumber / primary key) File (varbinary)文件表列:ParentID(父表的外键) FileID(自动编号/主键)文件(varbinary)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM