SQL Blob - fread and then insert?

1 view (last 30 days)
Stan
Stan on 19 Jun 2013
Hello,
I am running into some problems with inserting local files to sql server.
When I retrieve a blob I get a huge vector that I can convert to a file with fwrite. So I figured, if I fread a file and then insert I will bypass the permissioning problem.
I put all my data I want to insert in a structure and insert it. Then I get: Error using database/insert (line 189) Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
So what do I need to do with the data from my fread so that the table column (which is varbinary(max)) accepts it?
Thank you.

Accepted Answer

Stan
Stan on 20 Jun 2013
I have figured it out. Not a clean solution in any way though.
I read the file: fid=fopen(FileLocation); FileData=fread(fid, 'uint8'); fclose(fid);
Then I just use fastinsert. I have to put filedata in a cell.
fastinsert(conn, 'DocumentTable', {'FileDescription', 'FileType', 'Id', 'TimeSaved', 'FileData'},[{FileDescription}, {Data.FileType}, num2cell(Id), {Data.TimeSaved}, {Data.FileData}])
However,while this looks clean it is quite messy getting it out.
% Query='SELECT * FROM DocumentTable'; % data=fetch(conn, Query); % test=data.FileData; % Trial=double(test{1}); % Trial(Trial<0)=Trial(Trial<0)+256; % fid=fopen('rout.docx', 'w'); % fwrite(fid, Trial, 'uint8'); % fclose(fid)
I have no idea why I have to add 256 to negative numbers but it seems to work. Tested it for both pdf and docx. There must be a better solution but until I find that one I will use this. It bypasses some permission problem when using SELECT * FROM OPENROWS BULK statements.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!