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)

2
  • 1
    You may have a look at MSDN using SQLParameter!! Commented Dec 26, 2013 at 7: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. Commented Jun 19, 2017 at 19:47

1 Answer 1

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);
                }

    }
Sign up to request clarification or add additional context in comments.

1 Comment

Quite good dample, but you'd better to use stored procedures for create / update logic

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.