Save, Retrieve Image on SQL Database using VB.net
-
Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks
-
Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks
Here's a link to something that might prove useful in corraling SQL/BLOB/BINARY/C#/VB ponies. It's very tutorialish but looks to me like something that I'd review if I couldn't get my TSQL flapping behind my c#/vb frontend. Saving and Extracting BLOB Data – Basic Examples | Notes on SQL[^]
-
Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks
Why are you converting the image to a Base64 string? You don't have to do that to save it it in the database. Databases support blobs (Binary Large Objects) natively, so conversion to Base64 is not necessary. You can't directly use the Base64 string to create a Bitmap object from it. You have to convert the Base64 string back into an array of bytes, then create the Bitmap from that array.
Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
Hello; I have image on PictureBox, Cinverted to Base64String string and saved in Database as following: Dim ms As MemoryStream = New MemoryStream Dim bmp As Bitmap = New Bitmap(PictureBox1.Image) bmp.Save(ms, System.Drawing.Imaging.ImageFormat.jpg) Dim My_Photo_Str As String = Convert.ToBase64String(ms.ToArray) myqry = "update MYTABLE set COLUMN1=" & "'" & My_Photo_Str & "'" & "where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader Now that image need to retrieve it or shows on PictureBox, So I used the following:- myqry = "select * from MYTABLE where COLUMN2=" & 50 mycmd = New OleDbCommand(myqry, conn) mycmd.CommandTimeout = 0 mydr = mycmd.ExecuteReader mydr.Read() Dim PIC As Byte() PIC = mydr("COLUMN1") Dim My_Photo = New MemoryStream(PIC) PictureBox1.Image = Bitmap.FromStream(My_Photo) <<<<<-----HERE GOT ERROR the error on last line while try to set image to PictureBox, and error said that cannot convert Byte() to image, If any help will so appreciated thanks
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query. Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^] How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^] Query Parameterization Cheat Sheet | OWASP[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer