Extract Blob data from Access via vbscript

社会主义新天地 提交于 2019-12-23 06:51:07

问题


So I have a vbscript that pulls data from an access database

Example:

db = "C:\Users\username\databases\employeeID.mdb"
TextExportFile = "C:\Users\username\databases\Exp.txt"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; " & "Data Source =" & db

strSQL = "SELECT IDNumber+','+Name FROM employeeID "

rs.Open strSQL, cn, 3, 3

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.CreateTextFile(TextExportFile, True)

a = rs.GetString
f.WriteLine a
f.Close

The export looks like this:

19199439,person
29034234,john

The problem I am running into is that when i change name to the Blob Column, the blob column is blank. In access it just says: Long Binary Data

I believe it has something to do with a Text String vs a Binary String, but i dont know how to set it to pull back that info.

The string that is in the BLOB Field is jpg files

My end goal is to script the extraction of the blob data into each column with the IDNumber as the name of the file. If I can get the above query to export the ID Number then the corresponding blob data, I can take care of the rest.

Open to other ideas, but i do need to be able to script it and run it from an access database (unfortunately)

(I am even open to the idea of exporting it from access to sql server if someone knows how to script it. From a sql server i can pull the data pretty easily, but this has to be something i can script to run once a month.

Reason why: the access database is for photo id software and we want to export the pictures for use in other software and this is how the software stores it.

From the Answer Supplied by Suing here is my final code that does whole shebang. pulls the blob data from the access database, names each blob field with the corresponding Id Number adds .jpg to the end of each file and put it in the folder i need.

db = "C:\Users\amoore19\databases\employeeID.mdb"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set mstream = CreateObject("ADODB.Stream")

mstream.Type = 1 ''adTypeBinary

cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; " & "Data Source =" & db

strSQL = "SELECT AimsIDNumber,photo FROM employeeID WHERE AimsIDNumber like 'A00%' and photo is not null"

rs.Open strSQL, cn

DO WHILE not rs.eof

mstream.Open
mstream.Write rs("photo")
mstream.SaveToFile "C:\images\all\" & rs("AimsIDNumber") & ".jpg", 2 ''adSaveCreateOveWrite
mstream.close
rs.movenext

Loop

回答1:


I've had luck writing the Blob value to a stream file try this:

db = "C:\Users\username\databases\employeeID.mdb"
TextExportFile = "C:\Users\username\databases\Exp.txt"

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")
Set mstream = CreateObject("ADODB.Stream")

mstream.Type = 1 'adTypeBinary

cn.Open "Provider = Microsoft.Jet.OLEDB.4.0; " & "Data Source =" & db

strSQL = "SELECT IDNumber,Name FROM employeeID "

rs.Open strSQL, cn

Do Until rs.EOF
   mstream.Open
   mstream.Write rs("Name")
   mstream.SaveToFile rs("IDNumber"), 2 'adSaveCreateOveWrite
   mstream.close
   rs.MoveNext 
Loop


'Set fs = CreateObject("Scripting.FileSystemObject")
'Set f = fs.CreateTextFile(TextExportFile, True)
'a = rs.GetString
'f.WriteLine a
'f.Close


来源:https://stackoverflow.com/questions/29616371/extract-blob-data-from-access-via-vbscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!