如何在数据库的文本字段中存储byte []并检索它

111*_*110 2 c# sql-server asp.net bytearray telerik

我有一个大问题.
我上传了图片并获得了byte[].
在数据库中我有NVARCHAR(MAX)字段,不能更改.
我做什么并保存在数据库中我以后无法将其作为图像.
我试过.ToString()不起作用.
我试过这个:

private byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
static string GetString(byte[] bytes)
        {
            char[] chars = new char[bytes.Length / sizeof(char)];
            System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
            return new string(chars);
        }
Run Code Online (Sandbox Code Playgroud)

当我试图把它放入时,RadBinaryImage我什么也得不到.
这是错误:

 The provided binary data may not be valid image or may contains unknown header
Run Code Online (Sandbox Code Playgroud)

Ken*_*ick 9

您可能想要对其进行编码,而不是直接进行转换.

我过去成功使用过Radix-64编码.它有一个C#方法,但我还没用过它:

Convert.ToBase64String(byteArrayForImage);
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

然后你可以使用反向转换来获取你的字节数组:

Convert.FromBase64String(stringFromDB)
Run Code Online (Sandbox Code Playgroud)