1

Is it possible to append binary data to column whose datatype is image in SQL server. I herad it's possible for varbinary(max),but i want solution for image datatype. If so,please provide me the code sample.

1
  • IMAGE has been deprecated - you should always use VARBINARY(MAX) instead. Commented Jun 4, 2012 at 6:54

2 Answers 2

2

First, as others have said, you should use VARBINARY type instead of IMAGE.

Anyway, if you still want to use IMAGE, you can use UPDATETEXT command like this:

DECLARE @Pointer VARBINARY(1);
SELECT @Pointer = TEXTPTR(Data) FROM TableName WHERE Id = @Id;

UPDATETEXT TableName.Data @Pointer @Offset @Delete WITH LOG @Bytes;

Consider:

@Bytes = the actual bytes you want to append

@Delete = if you want to delete what it may have in its bytes positions

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

Comments

0
//Create Binary Data Stream  
    string filePath = Server.MapPath("APP_DATA/TestDoc.docx");
    string filename = Path.GetFileName(filePath);
    FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    BinaryReader br = new BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    br.Close();
    fs.Close();

--------------------
//insert the file into database

string strQuery = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
SqlCommand cmd = new SqlCommand(strQuery,conn);
cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename;
cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-word";
cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;
cmd.ExecuteNonQuery();

Note: ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead.

3 Comments

i don't want to insert,but i want to append/update to existing binary data like .WRITE method for VARBINARY(MAX)
You can overwrite but can't append
Use the Update sql statement instead of Select sql statement in strQuery variable.

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.