silveredge78
Joined: 25 Jul 2006 Posts: 387 Location: Midwest, USA
|
Posted: Fri May 23, 2008 10:27 pm Post subject: SQL Query/Extraction Using ADO help needed |
|
|
I am trying to use the code found here by tfcahm. I have gotten it to work in terms of extracting files from SQL. However, I was wondering how I could use it to pull out actual exported records like I would using isqlw or t4esql. If I can get that, then I can process through that list and know what files to pull down.
Here is my code so far:
| Code: | Author: tfcahm
; Found at: http://www.autohotkey.com/forum/viewtopic.php?p=137323#137323
#SingleInstance Force
#Persistent
#NoEnv
SetBatchLines, -1
SendMode, Input
#Include CoHelper.ahk
CoInitialize()
sFileOut = %A_Desktop%\test_out.doc ;output file, will be overwritten if exists
;Some useful constants
Jet3x:=4, Jet4x:=5
adOpenKeyset:=1, adLockOptimistic:=3, adTypeBinary:=1
adSaveCreateOverWrite:=2, adInteger:=3, adKeyPrimary:=1
VT_I4:=0x3, VT_BSTR:=0x8, VT_DISPATCH:=0x9, VT_UI1:=0x11, VT_ARRAY:=0x2000
;::::::::::::::::::::::::::::::::::::::::::::::::::
; Write binary data from the database into a file
;::::::::::::::::::::::::::::::::::::::::::::::::::
;Define the connection string
sConn := "Driver={SQL Server};Server=SqlServerName;Database=SQLDatabaseName;Uid=SQLUsername;Pwd=SQLPassword;"
;Create and open a connection
pcn := ActiveXObject("ADODB.CONNECTION")
Invoke(pcn, "Open", sConn)
;Create and open the recordset with a query
prs := ActiveXObject("ADODB.RECORDSET")
Invoke_(prs, "Open", VT_BSTR,"Select * From TP_Document Where PD_OwnerKey = '24195' And PD_Key = '127142';"
, VT_DISPATCH,pcn
, VT_I4,adOpenKeyset
, VT_I4,adLockOptimistic)
;Create and open a binary stream
pmstream := ActiveXObject("ADODB.STREAM")
Invoke(pmstream, "Type=", adTypeBinary)
Invoke(pmstream, "Open")
;Write the value of the target field to the stream object and then save it to a file with overwrite
pFields := Invoke(prs, "Fields")
pField := Invoke(pFields, "Item", "PD_Object")
pSafearray:=Invoke(pField, "Value")
Invoke_(pmstream, "Write", VT_ARRAY|VT_UI1,pSafearray)
Invoke(pmstream, "SavetoFile", sFileOut, adSaveCreateOverWrite)
;Cleanup, conservative - release every IDispatch
DllCall("oleaut32\SafeArrayDestroy", "Uint", pSafeArray)
Release(pField)
Release(pFields)
Invoke(pcn, "Close")
Invoke(prs, "Close")
Release(pmstream)
Release(prs)
Release(pcn)
CoUninitialize()
Msgbox, Done!
ExitApp |
I know the SQL query I want to pull the list of accounts, and how to pull the list of documents based off that list. But only using isqlw. I'd like to do it all in AHK. Also, I'd like to do it without crazy nested loops.
Any help would be greatly appreciated. _________________ SilverEdge78 |
|