Executesqlcommand - Update byte - Error

Asked

Viewed 131 times

1

Follows code:

var bytes = ConvertTo.Bytes(file);

int num = ctx.Database.ExecuteSqlCommand(
    $"UPDATE dbo.Table" +
    $"SET Video = '{bytes}' " +
    $"WHERE id = {Id} ");

Follow code to convert:

public static byte[] Bytes(HttpPostedFileBase file)
{
    var length = file.InputStream.Length;
    byte[] fileData = null;
    using (var binaryReader = new BinaryReader(file.InputStream))
    {
        fileData = binaryReader.ReadBytes(file.ContentLength);
    }
    return fileData;
}

I get error:

Implicit conversion of data type varchar to varbinary(max) is not permitted. Use the CONVERT function to execute this query.

Some solution?

  • What type of column Video at the bank?

  • @LINQ, varbinary(max)

1 answer

2


You should use darlings parameterized to do this job.

Anyway, you can do it this way:

var valor = "0x" + BitConverter.ToString(arraytoinsert).Replace("-", "");

int num = ctx.Database.ExecuteSqlCommand(
          $"UPDATE dbo.Table" +
          $"SET Video = {valor} " +
          $"WHERE id = {Id} ");

Using a parameterized query, it would look like this:

using(SqlCommand cmd = new SqlCommand("UPDATE dbo.Table SET Video = (@pVideo)", conn))
{
    cmd.Parameters.Add("@pVideo", SqlDbType.VarBinary, 8000).Value = bytes;
    cmd.ExecuteNonQuery();
}
  • LINQ, if I want to force variable as null, how do I do ? I get this error: A consulta parametrizada '(@imagem1 varbinary(max) ,@imagem2 nvarchar(4000),@imagem3 nvarc' espera o parâmetro '@imagem2', que não foi fornecido. , when I define only image1 other than null and the rest as null.

  • I asked a new question here: https://answall.com/questions/255098/erro-ao-fazer-update-quando-defino-vari%C3%A1vel-como-null

Browser other questions tagged

You are not signed in. Login or sign up in order to post.