Results 1 to 5 of 5

Thread: [RESOLVED] How to save image to MySQL database?

  1. #1

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    Resolved [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?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Member
    Join Date
    Aug 2006
    Posts
    57

    Re: How to save image to MySQL database?

    Quote Originally Posted by jmcilhinney View Post
    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

  4. #4

    Thread Starter
    Addicted Member tgf-47's Avatar
    Join Date
    Feb 2010
    Location
    CapeTown, South Africa -34.01244,18.337415
    Posts
    209

    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:
    1. Using connection As New MySql.Data.MySqlClient.MySqlConnection("Server=127.0.0.1;Port=3306;Database=test;Uid=root;Pwd=123454321")
    2.             Using command As New MySql.Data.MySqlClient.MySqlCommand("INSERT INTO img (PIC) VALUES(@Picture)", connection)
    3.                 'command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
    4.                 Using picture As Image = Image.FromFile("c:\bodega\1.jpg")
    5.                     'Create an empty stream in memory.
    6.                     Using stream As New IO.MemoryStream
    7.                         'Fill the stream with the binary data from the Image.
    8.                         picture.Save(stream, Imaging.ImageFormat.Jpeg)
    9.  
    10.                         'Get an array of Bytes from the stream and assign to the parameter.
    11.                         command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
    12.                     End Using
    13.                 End Using
    14.  
    15.                 connection.Open()
    16.                 command.ExecuteNonQuery()
    17.                 connection.Close()
    18.             End Using
    19.         End Using
    And This is the error I get:
    Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible'.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width