Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to save varbinary data to disk

I have some varbinary data stored in a table in MS Sql Server 2005. Does anyone have SQL code that takes a query as input (lets say the query guarantees that a single column of varbinary is returned) and outputs the bytes to disk (one file per row?) I'm sure this has been asked a thousand times before, but Googling comes up with mostly .net solutions. I want an SQL solution.

like image 321
SFun28 Avatar asked Oct 29 '10 21:10

SFun28


People also ask

How do I save a varbinary field to a specific folder?

/* This LinqPad script saves data stored in a VARBINARY field to the specified folder. 1. Connect to SQL server and select the correct database in the connection dropdown (top right) 2. Change the Language to C# Program 3. Change "Attachments" to the name of your table that holds the VARBINARY data 4.

How to save varbinary data of uploaded documents of the employee?

To save VARBINARY data of the uploaded documents of the employee into the SQL server database, first, we need to create a table into the database, so first we will create a table with the name tblEmpIdentity. To create a table in the SQL server database you need to execute the following SQL script as given below.

How do I create a varbinary field in SQL Server?

1. Connect to SQL server and select the correct database in the connection dropdown (top right) 2. Change the Language to C# Program 3. Change "Attachments" to the name of your table that holds the VARBINARY data 4. Change "AttachmentBuffer" to the name of the field that holds the data 5. Change "Id" to the unique identifier field name 6.

How to import field_varbinary from T_table to C?

Select field_varbinary from T_table ... to file 'C: emp\Text1.txt' ? Thanks. Reinhold. You can use bcp to import and export data. Enter the file storage type of field Document [varbinary (max)]:<enter>


1 Answers

The BCP approach does not work for me. The bytes it writes to disk cannot be deserialized back to the .net objects I stored. This means that the bytes on disk aren't equivalent to what's stored. Perhaps BCP is writing some kind of header. I'm not sure.

I found the following code here at the bottom of the article. It works great! Although it was intended for stored BMP images, it works with any varbinary.

DECLARE @SQLIMG VARCHAR(MAX),     @IMG_PATH VARBINARY(MAX),     @TIMESTAMP VARCHAR(MAX),     @ObjectToken INT  DECLARE IMGPATH CURSOR FAST_FORWARD FOR          SELECT csl_CompanyLogo from mlm_CSCompanySettingsLocalizations  OPEN IMGPATH   FETCH NEXT FROM IMGPATH INTO @IMG_PATH   WHILE @@FETCH_STATUS = 0     BEGIN         SET @TIMESTAMP = 'd:\' + replace(replace(replace(replace(convert(varchar,getdate(),121),'-',''),':',''),'.',''),' ','') + '.bmp'          PRINT @TIMESTAMP         PRINT @SQLIMG          EXEC sp_OACreate 'ADODB.Stream', @ObjectToken OUTPUT         EXEC sp_OASetProperty @ObjectToken, 'Type', 1         EXEC sp_OAMethod @ObjectToken, 'Open'         EXEC sp_OAMethod @ObjectToken, 'Write', NULL, @IMG_PATH         EXEC sp_OAMethod @ObjectToken, 'SaveToFile', NULL, @TIMESTAMP, 2         EXEC sp_OAMethod @ObjectToken, 'Close'         EXEC sp_OADestroy @ObjectToken          FETCH NEXT FROM IMGPATH INTO @IMG_PATH      END   CLOSE IMGPATH DEALLOCATE IMGPATH 
like image 108
SFun28 Avatar answered Oct 02 '22 06:10

SFun28