|
-
Mar 9th, 2011, 04:03 AM
#1
Thread Starter
Addicted Member
[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?
-
Mar 9th, 2011, 04:11 AM
#2
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.
-
Mar 9th, 2011, 08:19 PM
#3
Member
Re: How to save image to MySQL database?
-
Mar 28th, 2011, 04:20 AM
#4
Thread Starter
Addicted Member
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'.
-
Mar 28th, 2011, 04:48 AM
#5
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|