Error opening a Word doc file downloaded from a SQL SERVER Image field

89 views Asked by At

I have a SQL SERVER 2017 database in which there is a table containing records with a field of type Image. There are Word doc files in this field. When I try to download the contents of these fields to generate real doc files, the file is generated without errors, but when I go to open it with Microsoft Word it tells me that the file is unreadable. Where am I wrong?

Sub Main(args As String())
        Dim connectionString As String = GetConnectionString()
        Dim filePath As String = GetDirectoryRadiceLocale() + "\verbali\" + args(0) + ".doc"

        Dim WordContents As Byte()
        Dim selectStmt As String = "SELECT verbale FROM testata_assemblea WHERE id_assemblea = @id"

        Using connection As SqlConnection = New SqlConnection(connectionString)

            Using cmdSelect As SqlCommand = New SqlCommand(selectStmt, connection)
                cmdSelect.Parameters.Add("@ID", SqlDbType.Int).Value = CInt(args(0))
                connection.Open()
                WordContents = CType(cmdSelect.ExecuteScalar(), Byte())
                connection.Close()
            End Using
        End Using

        File.WriteAllBytes(filePath, WordContents)
    End Sub

I have tried several solutions without success. I also downloaded a little program called SQL Image View to check if the doc files could actually be seen or if there was some archiving problem. The little program SQL Image View shows me correctly the Word files This is what I see when I open the downloaded file with Word

0

There are 0 answers