Skip to content
Advertisement

Trying to extract data form SQL using PS script

I have been trying to get a PS script to work in extracting files (pdf, word, etc.) from an SQL Server database. I came across the PowerShell script below. The script runs and populates the destination folder but all files are 0 bytes and during the script execution. It throws the error:

“Exporting Objects from FILESTREAM container: .docx Exception calling “GetBytes” with “5” argument(s): “Invalid attempt to GetBytes on column ‘extension’. The GetBytes function can only be used on columns of typ e Text, NText, or Image.””

Can anyone point me in what am I doing wrong and how to fix this please? Much appreciated.

Advertisement

Answer

BinaryWriter is unnecessary. It’s for writing primitive types to a Stream.

And there’s no need to muck around with buffers; you can simply use SqlDataReader.GetStream(int).CopyTo(Stream), eg

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement