Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save byte array in sql server

Am looking to use an approach in saving passwords that requires using byte array as in this post

So which data type should i use in sql server to save byte array? and how can i pass and retrieve the byte array using SqlCommand?

like image 546
user2155873 Avatar asked Mar 20 '13 07:03

user2155873


People also ask

Can you store byte array in SQL?

Answers. hai, In Sql Server, use image data type or varbinary data type, to store byte array data.

How can store byte value in SQL Server?

varbinary(max) which is used to store large binary values (BLOBs) up to 2GB. The actual value is stored in a separate location if it's larger than 8000 bytes and just a pointer is stored in the row itself. This type is available since SQL Server 2005. image data type was used to store BLOBs prior to SQL Server 2005.

Can we store array in SQL Server?

Conclusion. As you can see, SQL Server does not include arrays. But we can use table variables, temporary tables or the STRING_SPLIT function. However, the STRING_SPLIT function is new and can be used only on SQL Server 2016 or later versions.

What is byte array in SQL?

Byte is an immutable value type that represents unsigned integers with values that range from 0 to 255. You can almost convert any kind of data into Byte Array(Byte []) like File, Image, Xml and etc.. In SQL Server, we have enough datatypes to store string text, int, bool, datatime and even Xml.


2 Answers

If it's always going to be the same length, then binary(length) would be suitable. If it's going to vary in length, use varbinary(maxlength).

binary and varbinary.

And, as @p.s.w.g says, you pass it from code by placing it into a suitable parameter.

like image 147
Damien_The_Unbeliever Avatar answered Oct 02 '22 20:10

Damien_The_Unbeliever


Just use a byte[] the same way you would any other parameter, specifying SqlDbType.Binary as the parameter type. Here a sample in C#

// Generate your password hash some way byte[] passwordHash = new byte[] { 0x0, 0x1, 0x2 ... };  ...  command.Parameters.Add("@passwordHash", SqlDbType.Binary); command.Parameters["@passwordHash"].Value = passwordHash; 

Or if you prefer VB.NET

' Generate your password hash some way Dim passwordHash As Byte() = New Byte() { &H0, &H1, &H2 ... }  ...  command.Parameters.Add("@passwordHash", SqlDbType.Binary) command.Parameters("@passwordHash").Value = passwordHash 
like image 38
p.s.w.g Avatar answered Oct 02 '22 21:10

p.s.w.g