MattP
Well-known member
I've run into a problem getting a file out of an image column in SQL. My method works when I run it against a pdf document. When I try it with an xls file I'm getting back gibberish due to the Utf-8 charset (I think).
Can somebody give me a nudge on how to incorporate System.Text.Encoding to get around the problem?
Can somebody give me a nudge on how to incorporate System.Text.Encoding to get around the problem?
VB.NET:
Private Sub SqlBlob2File(ByVal DestFilePath As String, ByVal CMID As Integer)
Dim DocumentCol As Integer = 0
Dim cn As New SqlConnection(My.Settings.ViewpointConnectionString.ToString())
Dim cmd As New SqlCommand("SELECT DATAPROP FROM CMDATA WHERE CMID = @CMID", cn)
cmd.Parameters.AddWithValue("@CMID", CMID)
cn.Open()
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim b(dr.GetBytes(DocumentCol, 0, Nothing, 0, Integer.MaxValue) - 1) As Byte
dr.GetBytes(DocumentCol, 0, b, 0, b.Length)
dr.Close()
cn.Close()
Dim fs As New System.IO.FileStream(DestFilePath, IO.FileMode.Create, IO.FileAccess.Write)
fs.Write(b, 0, b.Length)
fs.Close()
End Sub