Es kommt immer mal wieder vor, dass Dateien in einer Datenbank gespeichert werden müssen. Am einfachsten kann man die Anforderung mit einem kleinen VBScript realisieren. Der Windows Scripting Host ist auf allen aktuellen Windows-Installationen vorhanden und das Skript kann mit Notepad erstellt werden. Für das Beispiel verwende ich ein SQL Server Express Datenbankfile (*.mdf).
Zunächst braucht man ein Image-Feld in der Tabelle:
CREATE TABLE MyTable
(
ID int NOT NULL,
Title varchar(50) NOT NULL,
BinObject image NULL,
)
Anschließend kann man die erste Datei (im Beispiel ist das ein PDF-Dokument) in die Tabelle eintragen:
Dim strCon
strCon = "Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=" & GetScriptPath & "\mydb.mdf; Database=dbname;Trusted_Connection=Yes;"
'-----------------------------------------------------------------
' Open connection
'-----------------------------------------------------------------
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Dim strSQL: strSQL = "SELECT * FROM MyTable where 1 = 2"
oRs.Open strSQL, oCon, 0, 3
oRs.AddNew
oRs.Fields("ID") = 1
oRs.Fields("Title") = "Hello World"
oRs.Fields("BinObject") = ReadFile(GetScriptPath & "\cvs.pdf")
oRs.Update
oCon.Close
Set oRs = Nothing
Set oCon = Nothing
WScript.Echo "Done"
'---------------------------------------------------------
' Read file content into byte array
'---------------------------------------------------------
Function ReadFile(strFileName)
Const adTypeBinary = 1
Dim bin: Set bin = CreateObject("ADODB.Stream")
bin.Type = adTypeBinary
bin.Open
bin.LoadFromFile strFileName
ReadFile = bin.Read
End Function
'---------------------------------------------------------
' Get script path top avoid hard coding of paths
'---------------------------------------------------------
Function GetScriptPath()
Dim strPath: strPath = Wscript.ScriptFullName
Dim oReg: Set oReg = New RegExp
oReg.Pattern = Wscript.ScriptName & "$"
strPath = oReg.Replace( strPath, "" )
GetScriptPath = strPath
End Function
Zum Schluss sollte man noch testen, ob die Datei auch richtig angekommen ist. Dazu lese ich den Datensatz und speichere die Datei im aktuellen Verzeichnis wieder als PDF-Dokument.unter einem anderen Namen:
Dim strCon
strCon = "Driver={SQL Native Client};Server=.\SQLExpress;AttachDbFilename=" & GetScriptPath & "\mydb.mdf; Database=dbname;Trusted_Connection=Yes;"
'-----------------------------------------------------------------
' Open connection
'-----------------------------------------------------------------
Dim oCon: Set oCon = WScript.CreateObject("ADODB.Connection")
Dim oRs: Set oRs = WScript.CreateObject("ADODB.Recordset")
oCon.Open strCon
Set oRs = oCon.Execute("SELECT * FROM MyTable WHERE ID = 1")
While Not oRs.EOF
WScript.Echo "Extracting: " & oRs.Fields(0).Value & " - " & oRs.Fields(1).Value
SaveToFile GetScriptPath & "\pdf_from_db.pdf", oRs
oRs.MoveNext
Wend
oCon.Close
Set oRs = Nothing
Set oCon = Nothing
WScript.Echo "Done"
'----------------------------------------------------------------
' Store database content as file
'----------------------------------------------------------------
Function SaveToFile(strFileName, oRs)
Const adTypeBinary = 1
Dim bin: Set bin = CreateObject("ADODB.Stream")
bin.Type = adTypeBinary
bin.Open
bin.Write oRs.Fields("BinObject")
bin.SaveToFile strFileName, 2
End Function
'---------------------------------------------------------
' Get script path top avoid hard coding of paths
'---------------------------------------------------------
Function GetScriptPath()
Dim strPath: strPath = Wscript.ScriptFullName
Dim oReg: Set oReg = New RegExp
oReg.Pattern = Wscript.ScriptName & "$"
strPath = oReg.Replace( strPath, "" )
GetScriptPath = strPath
End Function
Source: Insert File Into SQL Server
Currently rated 5.0 by 1 people
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5