Visual Basic: Convert a Word document stored as a BLOB in SQL Server back into a document

Platform:Visual Basic
Task:Convert a Word document stored as a BLOB in SQL Server back into a document
Discussion:
You've stored a Word document as a Binary Large Object in SQL Server and now you need to retrieve it as a byte array and convert it back into a Word document. This example assumes you have a DataSet, DataTable and BindingSource all set up and the BindingSource's Current property points to the row you want to retrieve.
Example:
    Private Sub OpenBLOBUsingDefaultApplication()
        'get the bindingsource currentrow as a datarowview
        Dim CurrentRow As DataRowView = Me.BLOBTestBindingSource.Current
        'cast the BLOBData field of the current row into a byte array
        Dim MyBlob As Byte() = DirectCast(CurrentRow.Item("BLOBData"), Byte()) 'BLOBData is the SQL Server column that stores the file
        'create a temporary file path
        Dim MyTemporaryFilename As String = "C:\Temp\MyTemporaryFile.docx"
        'write the byte array to a filestream and write it to file
        Using fs As New FileStream(MyTemporaryFilename, FileMode.OpenOrCreate, FileAccess.Write)
            fs.Write(MyBlob, 0, MyBlob.Length)
            fs.Flush()
            fs.Close()
        End Using
        'start the temporary file using the default application.
        Process.Start(MyTemporaryFilename)
    End Sub
Back to Index