0

使用 c# 代码,我将字节数组转换为字符串,然后使用实体框架存储到 sql 表中。直到这里没有问题。

但是,如果我尝试在 MSSql Server Management Studio 中使用“选择”命令查看存储在表中的值,它会在该列中显示空白值。如果我查询 Len(MyColumn) 那么它显示一个有效的长度。

为什么我看不到存储的价值?

更新:当我右键单击表格时,它会显示一个值 -> 编辑前 200 行。但是,只要我单击该单元格,值就会消失。我正在使用 Sql server 2005。

4

1 回答 1

4

I have converted my byte array to a string

If you did something like message= Encoding.UTF8.GetString(myByteArray) you likely got unprintable characters. A leading \0 can cause your string to not print. This is the wrong way to handle binary data in a SQL database.

The correct way to handle this is one of the following options (in order of preference)

  1. Store the data as a varbinary not a nvarchar this lets you store the byte[] directly with EF without doing any conversion at all.
  2. Encode the data correctly
    1. Using Base64 encoding via message= Convert.ToBase64String(myByteArray) and myByteArray = Convert.FromBase64String(message)
    2. Using hexadecimal, SoapHexBinary makes it very easy to go to and from hexadecimal strings.
于 2013-07-26T14:35:44.550 回答