I'm trying to insert this byte array into a SQL Server database, the column data type is varbinary
and this is my code in C#
SqlParameter param = new SqlParameter("@buffer", SqlDbType.VarBinary, 8000);
param.Value = buffer;
string _query = "INSERT INTO [dbo].[Files] (FileID, Data, Length) VALUES ('" + uID + "','" + buffer + "','" + buffer.Length + "')";
using (SqlCommand comm = new SqlCommand(_query, conn))
{
comm.Parameters.Add(param);
try
{
conn.Open();
comm.ExecuteNonQuery();
}
catch (SqlException ex)
{
return Request.CreateResponse(HttpStatusCode.OK, "Something went wrong : " + ex.Message);
}
finally
{
conn.Close();
}
}
I also tried it with @buffer
inside the _query
string instead of buffer but I keep getting the error :
Converting from varchar to varbinary is not allowed use the CONVERT command to execute this query
and I used Convert
and it is saved successfully, but when I retrieve it, it retrieves the first 14 bytes only,
byte[] bytearr = (byte[])row["Data"];
I've been looking and found nothing. Can you please help me in storing the bytes and retrieving it?