Click here to Skip to main content
15,941,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear all
I have problem when accessing varbinary(50) value in byte[].

I'm reading value from MS-SQL DB as
C#
byte[] temp= (byte[]) cmd.ExecuteScalor();


Stored value in DB column is 0x617A (Hex ASCII of 'az').

But temp variable contains 13 bytes instead of 2.


Code used to insert value in DB
C#
cmd.Command="INSERT INTO table1(columns1) VALUES (CAST('az' as varbinary))";
return cmd.ExecuteNonQuery();
Posted
Updated 28-Jan-15 7:25am
v3
Comments
PIEBALDconsult 28-Jan-15 13:00pm    
Are you sure there are only two bytes in the database? I suspect the value got stored wrong. Can you use Improve question to show the code used to store the data?
DoingWork 28-Jan-15 13:23pm    
Yes I'm sure that there are only two bytes. I have verified it in Management studio too (Query editor + executor part of SQL server).

Code used to insert value in DB
cmd.Command="INSERT INTO table1(columns1) VALUES (CAST('az' as varbinary))";
return cmd.ExecuteNonQuery();
PIEBALDconsult 28-Jan-15 13:39pm    
I note that System.Byte[] contains thirteen characters.
DoingWork 28-Jan-15 13:57pm    
hmmm..... Why it contains "System.Byte[]" instead of actual data (0x617A)???
PIEBALDconsult 28-Jan-15 14:07pm    
It might not, but a ToString of a byte[] array will.
I wonder why you are storing a string in a varbinary rather than a varchar.
I also wonder what the actual query is you are executing.
And I wonder whether or not CAST(columns1 AS VARCHAR) will help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900