Store Images to SQL Database with TextCopy
Problem
Loftware can use the images embedded into the binary fields in the SQL database (BLOB or image fields). Storing your images in your database fields can be a challenge.
Solution
SQL Server includes
TEXTCOPY [/S [sqlserver] ] [/U [login] ] [/P [password] ] [/D [database] ] [/T table] [/C column] [/W"where clause"] [/F file] [{/I | /O}] [/K chunksize] [/Z] [/?] /S sqlserver The SQL Server to connect to. If 'sqlserver' is not specified, the local SQL Server is used. /U login The login to connect with. If 'login' is not specified, a trusted connection will be used. /P password The password for 'login'. If 'password' is not specified, a NULL password will be used. /D database The database that contains the table with the text or image data. If 'database' is not specified, the default database of 'login' is used. /T table The table that contains the text or image value. /C column The text or image column of 'table'. /W "where clause" A complete where clause (including the WHERE keyword) that specifies a single row of 'table'. /F file The file name. /I Copy text or image value into SQL Server from 'file'. /O Copy text or image value out of SQL Server into 'file'. /K chunksize Size of the data transfer buffer in bytes. Minimum value is 1024 bytes, default value is 4096 bytes. /Z Display debug information while running. /? Display this usage information and exit.
If you execute the “.exe,” without the required parameters, you are prompted to enter the missing parameters. The following is an example of a command that saves your image to SQL Server.
The image will be stored in a table named “Image” in the database "TEST". To insert the “glacier.jpg” picture into the SQL Server table, use the following command at the Command Prompt:
C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe" /S(local) /Umylogin /Pmysapassword /D TEST /T Image /C Picture /F "C:\temp\glacier.jpg" /W"where Title='glacier'" /I
With this command, you can log on to your local SQL Server database using a SQL Server login “mylogin.”
insert into Image(Picture,Title) values(0x0,'Glacier')
If there is no shell record for
TEXTCOPY Version 1.0 DB-Library version 8.00.194 ERROR: Row retrieval failed.
If you create the shell record, but the Picture column is NULL, the following error displays:
TEXTCOPY Version 1.0 DB-Library version 8.00.2039 ERROR: Text or image pointer and timestamp retrieval failed.
If you want to retrieve images from SQL Server, you can run
"C:\Program Files\Microsoft SQL Server\MSSQL\Binn\TEXTCOPY.exe" /S(local) /Umylogin /Pmysapassword /D TEST /T Image /C Picture /F "C:\temp\glacier_out.jpg" /W"where Title='glacier'" /O
The only difference between this command and the one that saves the image to SQL Server is that the “/O” option is used instead of the “/I” option. If you try to create an output file and the image doesn’t exist, the process successfully completes, but the file has zero bytes.
To insert or export multiple images at the same time, build a stored procedure that exploits the