Technical Article

Extract Images from SQL Database

,

This is a VBS script that will extract images from a Microsoft SQL database and create a individual file for each image.  The script utilizes two important utilities: isqlw and textcopy (both standard executables that come with Microsoft SQL Server).

'-----------------------------------------------------------------
'   
'   Script Name:  Extract Images From SQL Server
'  
'   Description: This is a VBS script that will extract images
'   from a Microsoft SQL database and create a individual file
'   for each image.  The script utilizes two important utilities:
'   isqlw.exe and textcopy.exe (both standard executables that 
'   come with Microsoft SQL Server).
'   
'   Created by: Walter Bentley
'   Date: 11/06/2002
'   File Name: extract_image.vbs
'   
'   This code is copyrighted and has limited warranties.
'
'srvr -- server to load
'db -- database to load
'usr -- login user
'pwd -- login password
'tbl -- table to load/unload
'col -- column to load/unload binary image blob
'whr -- where clause
'fil -- filename including path
'
'
'--------------------------------------------------------------------


Dim fs, f1, srvr, db, usr, pwd, tbl, col, c, t, whrx, whr

Set WshShell = WScript.CreateObject("WScript.Shell")

' Replace the variables in the isqlw statement with your database information
' input.txt should have a valid SQL Statment (exa. SELECT * from Table)
' out.txt is set to receive the list of image names (if available)

t = "isqlw -S server -U user -P password -d database -o d:\out.txt -i d:\input.txt"

Return = WshShell.Run(t, 1, TRUE)


Set fs = CreateObject("Scripting.FileSystemObject")
Set f1 = fs.OpenTextFile("d:\out.txt")

' Skip unneeded lines in the text file
f1.SkipLine
f1.SkipLine

Do While Not f1.AtEndOfStream

' Assign values to the below variables
' the whrx variable is tricky....the syntax must look like this: "where column = "
srvr = ""
db = ""
usr = ""
pwd = ""
tbl = ""
col = ""
whrx = ""

' reads the text file line by line
GetFile = f1.ReadLine

' db_image variable is used to name each image file
db_image = GetFile

fil = Chr(34) & "d:\" & db_image & ".jpg" & Chr(34)
whr = Chr(34) & whrx & Chr(39) & db_image & Chr(39) & Chr(34)

c = "textcopy.exe /S " & srvr & " /D " & db & " /U " & usr & " /P " & pwd & " /T " & tbl & " /C " & col & " /W " & whr & " /F " & fil & " /O"

' Uncomment the line below if you want to see each image textcopy command on the screen
'wscript.echo c

Return = WshShell.Run(c, 1, TRUE)

Loop

f1.close

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating