0

I'm trying to insert an sql query that take a binary array into sql server but it's not working

public static void UpdateImage(int imageid, byte [] binaryData)
{
    string query = "Update images set IMAGE_BINARIES="+binaryData+" where IMAGE_ID="+imageid;
    new SQLHelper(SQLHelper.ConnectionStrings.KernelAccountConnectionString).Insert(query);

}

IMAGE_BINARIES is of type varbinary(max)

Sora
  • 2,465
  • 18
  • 73
  • 146
  • 1
    You may have a look at MSDN using [SQLParameter](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter(v=vs.110).aspx)!! – Pilgerstorfer Franz Dec 26 '13 at 07:45
  • +1 Why duplicated questions always pop out before "the only right" questions in search engines? I don't know, but this one helped me for sure. – Szybki Jun 19 '17 at 19:47

1 Answers1

3

Use this sample:

        public static void LogActivity( byte[] data)
        {

                var connection =
                    new SqlConnection(ConfigurationManager.ConnectionStrings["LogConnectionString"].ConnectionString);
                var command = new SqlCommand { Connection = connection, CommandType = CommandType.Text };
                try
                {
                    command.CommandText = @"
insert into Logs (Data)
values ( @data)
";
                    command.Parameters.Add("@data", SqlDbType.VarBinary, data.Length).Value = data;
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    connection.Dispose();
                }
                catch (Exception ex)
                {
                    Log(ex);
                }

    }