简体   繁体   English

如何在Varbinary类型的sql server表列中存储字节数组?

[英]How a byte array is being stored in sql server table column of type Varbinary?

I have a requirement in which I want to store a byte array in a sql server table column of type Varbinary using C# code. 我有一个要求,我想使用C#代码将字节数组存储在Varbinary类型的sql server表列中。 Now when I try to do this, I get a value in sql server table as "0x4A6974656E6472612053616E6B686C61" for a byte array a[] = { 74, 105, 116, 101, 110, 100, 114, 97, 32, 83, 97, 110, 107, 104, 108, 97} . 现在,当我尝试这样做时,我在sql server表中得到一个值为"0x4A6974656E6472612053616E6B686C61"的字节数组a[] = { 74, 105, 116, 101, 110, 100, 114, 97, 32, 83, 97, 110, 107, 104, 108, 97} "0x4A6974656E6472612053616E6B686C61" a[] = { 74, 105, 116, 101, 110, 100, 114, 97, 32, 83, 97, 110, 107, 104, 108, 97} I am not sure how this is done. 我不确定这是怎么做到的。 The value I get is actually a hexa representation of each decimal number. 我得到的值实际上是每个十进制数的六进制表示。

Is it like sql server performs some internal conversion while storing a byte array into a varbinary sql table column ? 是不是sql server在将字节数组存储到varbinary sql表列时执行了一些内部转换?

You see a hex-string representation of the actual binary value stored in the field, because that is one semi-readable version to represent the bytes. 您会看到存储在字段中的实际二进制值的十六进制字符串表示形式,因为这是一个表示字节的半可读版本。 How else should it be shown? 它应该怎么显示?

The string representation and the actual bytes are not the same. 字符串表示和实际字节不相同。 This is true for most types. 大多数类型都是如此。 If you store an integer, say 7622389, that too is actually stored as some amount of bytes (depending on the size of the field). 如果存储一个整数,例如7622389,那么它实际上也存储为一些字节数(取决于字段的大小)。 But showing the actual bytes instead of the string representation of the value would just be confusing. 但是显示实际的字节而不是值的字符串表示只会令人困惑。

For general purpose binary data, the hex-string format is the logical string representation. 对于通用二进制数据,十六进制字符串格式是逻辑字符串表示。 Sql Server management studio could theoretically show a thumbnail of a picture if the bytes happened to make up an image file or some other well known file format. 如果字节恰好构成图像文件或其他一些众所周知的文件格式,Sql Server管理工作室理论上可以显示图片的缩略图。 But since the field just contains general purpose binary data, it cannot really know what would be the best display format so it uses hex-string. 但由于该字段只包含通用二进制数据,因此它无法真正知道什么是最佳显示格式,因此它使用十六进制字符串。

When you actually select the field from the database to your C# program, what you get back will generally be a byte[] , sometimes wrapped up inside a container type of some sort. 当您实际从数据库中选择字段到C#程序时,您获得的内容通常是一个byte[] ,有时会包含在某种类型的容器类型中。

Try this: 试试这个:

DECLARE @bin VARBINARY(MAX)=0x4A6974656E6472612053616E6B686C61;
SELECT CAST(@bin AS VARCHAR(MAX))

The result Jitendra Sankhla looks pretty - uhm - fitting... :-) 结果Jitendra Sankhla看起来很漂亮 - Jitendra Sankhla - 适合...... :-)

Some background: 一些背景:

VARBINARY is nothing more than a BLOB. VARBINARY只不过是一个BLOB。 In a SELECT it is presented as HEX-string, but this is not the way it is stored actually . SELECT 表示为HEX-string,但这不是实际存储的方式

You can store literally everything in a VARBINARY column: pictures, strings, numbers, XML, complex structures... The only - but very important! 您可以将所有内容存储在VARBINARY列中:图片,字符串,数字,XML,复杂结构......唯一 - 但非常重要! - point is, that you must know, how to read and interpret this. - 重点是,您必须知道,如何阅读和解释这一点。

Why did I cast this to VARCHAR ? 为什么我把这个转换为VARCHAR Your numbers looked like ASCII-codes for plain letters. 您的数字看起来像普通字母的ASCII码。 VARCHAR is a datatype representing 1-byte-extended-ASCII strings (the bound collation defines - like a codepage - the intpretation and sorting of non-plain characters). VARCHAR是表示1字节扩展ASCII字符串的数据类型(绑定的排序规则定义 - 类似于代码页 - 非普通字符的解释和排序)。
Assuming, that you pass in byte after byte, the conversion to a normal string leads to letters according to the byte's values. 假设您逐字节传递,转换为普通字符串会根据字节值导致字母。

It is obvious, that you won't convert any binary to a string . 很明显, 您不会将任何二进制文件转换为字符串
Many values between 0 and 255 are not printable. 0到255之间的许多值都不可打印。 Other sources of pain can be differing collation/codepage settings. 其他疼痛来源可能是不同的整理/代码页设置。 Furthermore, if you pass in a string with an encoding like utf-8 you will get some erronous characters, because utf-8 will encode some characters with more than one byte. 此外,如果传入一个像utf-8这样的编码的字符串,你会得到一些错误的字符,因为utf-8会编码一些带有多个字节的字符。

But in your simple example it works. 但在你的简单例子中它是有效的。

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

相关问题 如何检索存储为Varbinary(max)的字节数组 - How to retrieve a byte array stored as Varbinary(max) 如何将Excel文件插入到具有varbinary类型的列到SQL表中? - How to insert an Excel file to a column with varbinary type into an SQL table? 如何将C#中的byte []作为字符串传递到SQL Server存储过程并转换为varbinary(MAX) - How to pass byte[] from C# as string to SQL Server stored procedure and convert into varbinary(MAX) 在SQL Server的Varbinary(MAX)或Image数据类型中存储转换为字节数组的位图 - Store Bitmap converted to byte array in Varbinary(MAX) or Image data type of SQL Server SQL Server查询与varbinary类型列不匹配 - SQL Server query not matching for a varbinary type column 在SQL Server中将bytearray保存到VarBinary列只插入一个字节 - saving bytearray to VarBinary column in SQL Server inserts only one byte 在SQL中将字节数组复制到varbinary - Copy byte array to varbinary in SQL 如何将 Byte[] 转换为数据以在 SQL Server 的 varbinary(max) 列中插入值? - How convert a Byte[] to a data to insert a value in a varbinary(max) column in SQL Server? 将存储在SQL XML参数中的byte []存储到SQL Server 2005中的varbinary(MAX)字段。可以完成吗? - Store a byte[] stored in a SQL XML parameter to a varbinary(MAX) field in SQL Server 2005. Can it be done? SQL Server VARBINARY(max) 到 c# byte[] - SQL Server VARBINARY(max) to c# byte[]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM