0

When I code an insert, everything works great. For example:

dataCommand.CommandText = "use mydb; INSERT INTO mytable VALUES(@binaryvalue);
dataCommand.Parameters.Add("@binaryvalue", SqlDbType.VarBinary, 256).Value = mycard;
dataCommand.ExecuteNonQuery();

However, if I try to update, it won't work. I used the .WRITE method, but it won't do:

dataCommand.CommandText = "use mydb; update mytable set mycolumn .Write(@binaryvalue, 0, NULL) where myid = " + wid;
dataCommand.Parameters.Add("@binaryvalue", SqlDbType.VarBinary, 256).Value = mycard;
dataCommand.ExecuteNonQuery();

How can I perform an update instead of deleting/inserting?

Thanks for any response!

1
  • Stop using the use mydb; inside your SQL script - by means of the connection string, you're already defining what databas you're running in.... Commented Aug 4, 2011 at 19:36

2 Answers 2

2

Maybe the problem is the NULL:

.WRITE (expression,@Offset,@Length)         (reference)
.Write(@binaryvalue, 0, NULL)               (your code)

Try putting the length of your content instead of NULL.

@Length is the length of the section in the column, starting from @Offset, that is replaced by expression. @Length is bigint and cannot be a negative number. If @Length is NULL, the update operation removes all data from @Offset to the end of the column_name value.

More info here: http://msdn.microsoft.com/en-us/library/ms177523.aspx

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

2 Comments

I changed the length, and still get the same error message: "Cannot call methods on varbinary." According to MSDN, my syntax is perfect, and MSDN says you need to use ".WRITE" for varbinary updating, so I don't understand why it won't work! Any other ideas?
I was assuming you just typed in your example code incorrectly, but your update... where myId=... looks at the identity, but in your example insert doesn't specify the id. Do you have a primary key on the table?
0

What's the exact type of the binary column in the table? Is it VARBINARY(MAX)? If not please try it and see if it works. According to the documentation:

"Use the .WRITE (expression, @Offset, @Length) clause to perform a partial or full update of varchar(max), nvarchar(max), and varbinary(max) data types"

and

"To achieve the same functionality of .WRITE with other character or binary data types, use the STUFF (Transact-SQL)."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.