SQL Server VARBINARY(max) 到 c# byte[] [英] SQL Server VARBINARY(max) to c# byte[]

查看:47
本文介绍了SQL Server VARBINARY(max) 到 c# byte[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查询返回一些记录的表(其中一列是 VARBINARY(MAX)).

然后我将它保存为 .dat.csv 然后我解析那个 .dat 文件并通过基于逗号分割文件来将该 varbinary 值转换为一个字符串.现在我需要将此 varbinary 转换为字节数组.我该怎么做?

解决方案

好问题.从技术上讲,您可以先转换为字符数组,然后再转换为字节.然而,.NET 中的字符串默认是 Unicode 编码的(而不是 ASCII),所以这很棘手.

如果可能,您应该尝试将 varbinary 作为字节数组从文件中提取出来,使用您正在读取的 FileStream 而不是 StreamReader,后者执行与文件编码类型之间的编码转换.

这种字节到字符串到字节的 babelfishing 的问题在于,某些字节码在每种 Unicode 编码中都有特殊含义,向解码器提供有关解码下一个字符应提取的字节数的信息.在各种 Unicode 编码和字符串的 .NET 原生 UTF-8 编码之间进行转换时,字节将增加、丢失和更改.当它是一个字符串时,没什么大不了的;编码信息保留在字符串中.当它是二进制数据时,除非以非常具体的方式完成,否则编码和解码可能会出现乱码.

唯一能完美运行的方法是,如果您使用 ASCII 编码写出文件,然后将其读回,这将导致每个单独的字节都被视为单个字符.然后,您可以简单地将每个字符转换回一个字节,并且在 Syetem.Char 的幕后 UInt16 的更重要的字节,它只是输入到该字符的字节的零填充,将被丢弃.

var reader = new StreamReader(new FileStream(test.csv"), Encoding.ASCII);var varBinaryString = reader.Read();var byteArray = varBinaryString.ToCharArray().Select(c=>(byte)c).ToArray();

从技术上讲,您也可以使用任何 Unicode 编码将其引入,但是您需要了解很多关于您如何写出这些字节以及阅读器如何将它们读回的细节,以便您可以执行正确的根据需要进行编码和扩展(或压缩)以获得原始字节流.

.NET 2.0 版本 - 没有 Linq:

StreamReader reader = new StreamReader(new FileStream(test.csv"), Encoding.ASCII);string varBinaryString = reader.Read();char[] charArray = varBinaryString.ToCharArray();byte[] byteArray = new byte[charArray.Length];for(int i=0; i< charArray.Length; i++){byteArray[i] = (byte)charArray[i];}

I am querying the table (one of the columns is a VARBINARY(MAX)) which returns some records.

Then I save that as .dat.csv then I parse through that .dat file and get that varbinary value into a string by splitting the file based on commas. Now I need to convert this varbinary to byte array. How can I do that?

解决方案

Good question. Technically, you can do this by first converting to a character array, then converting to bytes. However, strings in .NET are Unicode-encoded by default (instead of ASCII), so it gets tricky.

If at all possible, you should try to pull the varbinary out of the file as a byte array, using the FileStream you're reading from instead of the StreamReader which performs encoding conversions to and from the file encoding type.

The problem with this byte-to-string-to-byte babelfishing is that certain bytecodes have special meaning in each Unicode encoding, giving information to the decoder about the number of bytes it should pull to decode the next character. When converting between various Unicode encodings and the .NET-native UTF-8 encoding for strings, bytes will be gained, lost, and changed. When it's a string, no biggie; the encoding information stays with the string. When it's binary data, the encoding and decoding can garble it unless it's done in a very specific way.

The only way this will work flawlessly is if you write the file out using ASCII encoding, then read it back in as such, which will cause each individual byte to be treated as a single character. You can then simply convert each char back to a byte, and the more significant byte of the UInt16 behind the scenes of the Syetem.Char, which is just zero-padding for the byte fed in to that char, will be discarded.

var reader = new StreamReader(new FileStream("test.csv"), Encoding.ASCII);
var varBinaryString = reader.Read(<wherever the varbinary is in the file/line>);

var byteArray = varBinaryString.ToCharArray().Select(c=>(byte)c).ToArray();

Technically, you could pull it in using any Unicode encoding as well, but you need to know a lot of specifics about how you wrote out those bytes and how the reader is reading them back in, so that you can perform the correct encoding and expansion (or deflation) as necessary to get the original bytestream.

EDIT: The .NET 2.0 version - no Linq:

StreamReader reader = new StreamReader(new FileStream("test.csv"), Encoding.ASCII);
string varBinaryString = reader.Read(<wherever the varbinary is in the file/line>);

char[] charArray = varBinaryString.ToCharArray();
byte[] byteArray = new byte[charArray.Length];

for(int i=0; i< charArray.Length; i++)
{
    byteArray[i] = (byte)charArray[i];
}

这篇关于SQL Server VARBINARY(max) 到 c# byte[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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