Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Passing binary data into database
Message
De
11/04/2016 23:53:41
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 5.0
OS:
Windows 10
Database:
MS SQL Server
Divers
Thread ID:
01634652
Message ID:
01634685
Vues:
41
J'aime (1)
>>>Hi everybody,
>>>
>>>We have the following code which is wrong:
>>>
>>>
>>>if (SqlDbType.VarBinary == parameterType || SqlDbType.Image == parameterType || SqlDbType.Binary == parameterType)
>>>            {
>>>                parameter = new SqlParameter("@" + parameterName, parameterType, parameterValue.Length);
>>>
>>>                // TODO: Performance test these two methods
>>>                //byte[] bytes = new byte[parameterValue.Length * sizeof(char)];
>>>                //System.Buffer.BlockCopy(parameterValue.ToCharArray(), 0, bytes, 0, bytes.Length);
>>>                //parameter.Value = bytes;
>>>
>>>                parameter.Value = Encoding.UTF7.GetBytes(parameterValue);
>>>            }
>>>
>>>The value is being passed as binary string. Do you know what should be the code to pass this info into the DB?
>>>
>>>Thanks in advance.
>>
>>What do you mean by 'wrong' - what exception do you get ?
>>Why UTF7 ?
>
>The result doesn't seem to be what is expected. I don't get exception.
>
>This was programmed by someone else, I have no idea what the correct code should be.
>
>We need to be able to pass
>
>\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09
>
>and get
>
>0x30313233343536373839
>
>in the DB.
>
>I also asked a related question here
>
>https://social.msdn.microsoft.com/Forums/sqlserver/en-US/bfc4fd55-a215-4f18-b7b2-09185abecd78/dataadapter-doesnt-seem-to-read-the-data?forum=sqldataaccess
>
>and here
>
>https://social.msdn.microsoft.com/Forums/sqlserver/en-US/a80db55a-ae29-44d3-b295-11aede6a907a/sending-binary-data-to-sql-server?forum=sqldataaccess

If the data is binary - stay away from chars/string - use a byte array
parameter.Value = parameterValue // I expect parameterValue  will be a byte[]
Gregory
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform