存储 Base64 编码文件的 SQL 数据类型是什么?

2022-01-21 00:00:00 base64 sql-server

nvarchar(max), varchar(max),还是我真的应该解码字符串并将其放入图像或 blob 或其他东西中?

nvarchar(max), varchar(max), or should I really decode the string and put it in an image or blob or something?

我有一个 .Net SOAP Web 服务,它将记录保存在 SQL 2008 DB 中.该服务将被扩展以接受图像,该图像(无论好坏)也需要暂时进入数据库.

I've got a .Net SOAP web service, which saves records in a SQL 2008 DB. The service is going to be extended to accept an image, which (for better or worse) also needs to go into the DB temporarily.

为简单起见,服务将图像作为 Base 64 编码字符串,稍后必须将其作为 base64 编码字符串返回(同一服务上的不同方法).

To keep things simple, the service takes the image as a Base 64 encoded string, and will have to give it back as a base64 encoded string later (a different method on the same service).

我最初只是打算使用 nvarchar(max),我相信这会起作用.但后来我认为 base64 编码意味着它可以使用 varchar(max) 代替,并且使用更少的存储空间.这是正确的吗?还是我应该硬着头皮将文本解码为二进制,将其存储为 blob,然后在输出时重新编码?

I was originally just going to use nvarchar(max), and I'm sure this would work. But then I thought that base64 encoded means it could use varchar(max) instead, and use less storage space. Is this right? Or should I bite the bullet and decode the text to binary, store it as a blob, and then re-encode it on the way out again?

最后 - 负载存储和性能不太可能导致问题,这是一个宠物项目,负载会很低.

Finally - load storage and performance are unlikely to cause problems, this is a pet project and will be low load.

在回答@howiecamp 的问题时,我在这里写下了我是如何对图像进行 URL 编码的:http://www.flowerchild.org.uk/archive/2010/06/13/base-64-encoding-an-image-to-pass-across-a-web.html

In response to @howiecamp's question, I wrote up how I was URL encoding the image here: http://www.flowerchild.org.uk/archive/2010/06/13/base-64-encoding-an-image-to-pass-across-a-web.html

推荐答案

嗯,Base64 是 ASCII 编码,真的 - 所以绝对不需要 NVARCHAR - 因为它是文本,我建议 VARCHAR(MAX)

Well, Base64 is a ASCII encoding, really - so definitely no need for NVARCHAR - and since it's text, I'd suggest VARCHAR(MAX)

纯文本,最大2GB(应该够用了),而且是字符串类型,所以可以在上面使用所有的字符串函数.NVARCHAR 确实使用了两倍的存储空间 - 每个字符总是 2 个字节 - 在这种情况下完全没有必要.

It's pure text, up to 2 GB (should be enough), and it's a string-type, so you can use all string functions on it. NVARCHAR does indeed use twice as much storage - always 2 bytes per character - and is totally unnecessary in this case.

相关文章