[RESOLVED] How to save image to MySQL database?
This has really been a problem for me in the past and with deadlines, we decided to just have the app copy the image to the server, rename it and save the link in the database. This is the worst way of saving that kind of sensitive data, I know. We now have the time to fix that.
I want to save images to my MySQL database, so this is a two part question.
1) When creating the table in MySQL, what datatype should the column be that would be holding the image?
2) What is the vb code to insert a row where one of the fields will be containing the image into that table?
How do I read it back into a Picturebox or save it to the HDD?
Re: How to save image to MySQL database?
Follow the CodeBank link in my signature and check out my thread on Saving Images In Databases. The example uses SQL Server but as with all ADO.NET code, all you have to do is switch the provider.
As for what data type to use, whichever is for binary data. I've never used MySQL before but I just did a quick search for mysql binary data and it appears that BLOB (Binary Large OBject) is the appropriate data type.
Re: How to save image to MySQL database?
Quote:
Originally Posted by
jmcilhinney
Follow the CodeBank link in my signature and check out my thread on Saving Images In Databases. The example uses SQL Server but as with all ADO.NET code, all you have to do is switch the provider.
As for what data type to use, whichever is for binary data. I've never used MySQL before but I just did a quick search for mysql binary data and it appears that BLOB (Binary Large OBject) is the appropriate data type.
thanks bro...now i go to u article :thumb: :thumb: :thumb:
Re: [RESOLVED] How to save image to MySQL database?
Hi. I had some time to play with this now and still cant get it to work.
This is my code:
vb Code:
Using connection As New MySql.Data.MySqlClient.MySqlConnection("Server=127.0.0.1;Port=3306;Database=test;Uid=root;Pwd=123454321")
Using command As New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO img (PIC) VALUES(@Picture)", connection)
'command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
Using picture As Image = Image.FromFile("c:\bodega\1.jpg")
'Create an empty stream in memory.
Using stream As New IO.MemoryStream
'Fill the stream with the binary data from the Image.
picture.Save(stream, Imaging.ImageFormat.Jpeg)
'Get an array of Bytes from the stream and assign to the parameter.
command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
End Using
End Using
connection.Open()
command.ExecuteNonQuery()
connection.Close()
End Using
End Using
And This is the error I get:
Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.
Re: [RESOLVED] How to save image to MySQL database?
Do you have Option Strict On? If not, fix that. Once you've done that, you should get a compilation error that will require fixing. Once that's fixed, let's see if you still get that same error.