How to export an ad-hoc SQLServer query blob data to a binary file
I simply want to be able to export jpg data from a single record to a file without having to write a program every time.
Query:
select Media from开发者_如何学JAVA tbl_GEO_ElementImage where ElementImageID = 64902
Use the command line BCP utility:
c:\temp\>bcp "select Media from tbl_GEO_ElementImage where ElementImageID = 64902" queryout "001.jpg" -T
When promped, give the following answers:
Enter the file storage type of field Media [image]: <Enter>
Enter prefix-length of field Media [4]: 0
Enter length of field Media [0]: <Enter>
Enter field terminator [none]: <Enter>
Do you want to save this format information in a file? [Y/n] n
I ended up writing a command line utility to allow simple unattended export of multiple image records.
Usage:
RunSQLOLEDB "[connection string]" "[query]"
e.g.
To extact all images from table Image into separate files 1.jpg, 2.jpg, etc.
SQLImageOut "Provider=SQLOLEDB;Data Source=(local);..." "SELECT CONVERT(varchar(50), ImageID)+'.jpg', ImageData from Image"
or
SQLImageOut @ConnectionString.txt @Query.sql
Download Binary
GitHub Source
精彩评论