I have stored data in a SQL Server table and I need to retrieve it through FileStream to save it back as a png picture as it was before.
I can't understand or find a way to do this, I read many suggestions and examples but none worked. Any ideas?
I just need an easy working solution, all the understanding can come later. Also, is it possible to do it with OleDb instead of T-SQL?
What I did to store the data is:
Dim connection As New OleDbConnection(MyConnString)
Dim command As New OleDbCommand("UPDATE Prodotti SET Immagine = ? WHERE Oggetto = 'Monitor'", connection)
Using picture As Image = Image.FromFile("C:\Users\user\Desktop\Nuova cartella\galar 1 mainland.png")
Using stream As New MemoryStream
picture.Save(stream, Imaging.ImageFormat.Png)
command.Parameters.Add("@Picture", OleDbType.VarBinary).Value = stream.GetBuffer()
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Using
End Using
What I got is in fact the Cell field as a long byte value.
You don't even need an
ImageandMemoryStreamhere, because you are just getting it straight from a file. Just use aFileStreamdirectly.You should also pass
-1as the length (ie amaxvalue), otherwise you could end up with a different query plan for every execution.Use
SqlCommandetc rather than OleDb, as it is designed for SQL Server. If you need to support multiple DBMSs then use theDbConnectionbase type as an abstraction.To do the same thing in reverse, use a reader. Since you only have one value, you can just do a single
Readand then useGetStream