' Windows Installer utility to copy a file into a database text field ' For use with Windows Scripting Host, CScript.exe or WScript.exe ' Copyright (c) Microsoft Corporation. All rights reserved. ' Demonstrates processing of primary key data ' Option Explicit Const msiOpenDatabaseModeReadOnly = 0 Const msiOpenDatabaseModeTransact = 1 Const msiViewModifyUpdate = 2 Const msiReadStreamAnsi = 2 Dim argCount:argCount = Wscript.Arguments.Count If argCount > 0 Then If InStr(1, Wscript.Arguments(0), "?", vbTextCompare) > 0 Then argCount = 0 If (argCount < 4) Then Wscript.Echo "Windows Installer utility to copy a file into a database text field." &_ vbNewLine & "The 1st argument is the path to the installation database" &_ vbNewLine & "The 2nd argument is the database table name" &_ vbNewLine & "The 3rd argument is the set of primary key values, concatenated with colons" &_ vbNewLine & "The 4th argument is non-key column name to receive the text data" &_ vbNewLine & "The 5th argument is the path to the text file to copy" &_ vbNewLine & "If the 5th argument is omitted, the existing data will be listed" &_ vbNewLine & "All primary keys values must be specified in order, separated by colons" &_ vbNewLine &_ vbNewLine & "Copyright (C) Microsoft Corporation. All rights reserved." Wscript.Quit 1 End If ' Connect to Windows Installer object On Error Resume Next Dim installer : Set installer = Nothing Set installer = Wscript.CreateObject("WindowsInstaller.Installer") : CheckError ' Process input arguments and open database Dim databasePath: databasePath = Wscript.Arguments(0) Dim tableName : tableName = Wscript.Arguments(1) Dim rowKeyValues: rowKeyValues = Split(Wscript.Arguments(2),":",-1,vbTextCompare) Dim dataColumn : dataColumn = Wscript.Arguments(3) Dim openMode : If argCount >= 5 Then openMode = msiOpenDatabaseModeTransact Else openMode = msiOpenDatabaseModeReadOnly Dim database : Set database = installer.OpenDatabase(databasePath, openMode) : CheckError Dim keyRecord : Set keyRecord = database.PrimaryKeys(tableName) : CheckError Dim keyCount : keyCount = keyRecord.FieldCount If UBound(rowKeyValues) + 1 <> keyCount Then Fail "Incorrect number of primary key values" ' Generate and execute query Dim predicate, keyIndex For keyIndex = 1 To keyCount If Not IsEmpty(predicate) Then predicate = predicate & " AND " predicate = predicate & "`" & keyRecord.StringData(keyIndex) & "`='" & rowKeyValues(keyIndex-1) & "'" Next Dim query : query = "SELECT `" & dataColumn & "` FROM `" & tableName & "` WHERE " & predicate REM Wscript.Echo query Dim view : Set view = database.OpenView(query) : CheckError view.Execute : CheckError Dim resultRecord : Set resultRecord = view.Fetch : CheckError If resultRecord Is Nothing Then Fail "Requested table row not present" ' Update value if supplied. Cannot store stream object in string column, must convert stream to string If openMode = msiOpenDatabaseModeTransact Then resultRecord.SetStream 1, Wscript.Arguments(4) : CheckError Dim sizeStream : sizeStream = resultRecord.DataSize(1) resultRecord.StringData(1) = resultRecord.ReadStream(1, sizeStream, msiReadStreamAnsi) : CheckError view.Modify msiViewModifyUpdate, resultRecord : CheckError database.Commit : CheckError Else Wscript.Echo resultRecord.StringData(1) End If Sub CheckError Dim message, errRec If Err = 0 Then Exit Sub message = Err.Source & " " & Hex(Err) & ": " & Err.Description If Not installer Is Nothing Then Set errRec = installer.LastErrorRecord If Not errRec Is Nothing Then message = message & vbNewLine & errRec.FormatText End If Fail message End Sub Sub Fail(message) Wscript.Echo message Wscript.Quit 2 End Sub