shamim
shamim

Reputation: 6778

How to save byte[] to varbinary(64) field in database

I have

byte[] a = HashEncrypt("a");

with

public byte[] HashEncrypt(string password)
{
    SHA512Managed sha = new SHA512Managed();
    byte[] hash = sha.ComputeHash(UnicodeEncoding.Unicode.GetBytes(password));
    return hash;
}

I want to save byte[] a to my database. My database field is a varbinary(64). I'm using SQL Server 2008. I want to know the insert query with C# code.

I am using ADO.NET

Upvotes: 0

Views: 2374

Answers (1)

Aaron
Aaron

Reputation: 7541

Not quite sure how you're doing this as pointed out in the comments under your question, but I've added a byte[] to my db table by using a SqlParamenter inside of a SqlCommand.

SqlCommand cmd = new SqlCommand("insert into binaryTable (example) values (@example)",connection);
cmd.Parameters.Add(new SqlParameter("@example",HashEncript("password"));
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();

Upvotes: 1

Related Questions