开发者

how to get varchar as byte[] using DataReader in Ado.Net?

how to get varchar as byte[] data using DataReader in Ado.Net? We have tried the following code.

if (!objDataReader.IsDBNull(i))
                  {
                                long len = objDataReader.GetBytes(i, 0, null, 0, 0);

                                byte[] buffer = new byte[len];
                                objDataReader.GetBytes(i, 0, buffer, 0, (int)len);

                  }

But the above code gives the error ("Unable to cast object of type 'System.String' to type 'System.Byte[]'.")

Update:

When value for a column is null and i remove dbnull condition then executing following lines 开发者_JS百科of code gives an error as "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]"

long len = objDataReader.GetBytes(i, 0, null, 0, 0);
byte[] buffer = new byte[len];
objDataReader.GetBytes(i, 0, buffer, 0, (int)len);


A varchar field should be storing text, not raw bytes. If you want to store opaque binary data, use image or binary.

If you want to get the bytes which represent some text in a particular encoding, you can use Encoding.GetBytes - but you'll need to specify the encoding, e.g. Encoding.UTF8.GetBytes(text). However, that may not be how it's represented in the database. The point is that it's text data, so the exact binary representation shouldn't matter, so long as you can accurately reproduce the Unicode string.

What are you storing in this field?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜