We help IT Professionals succeed at work.

Adding image to mysql database from vb.net

kewlchap_85
kewlchap_85 asked
on
Medium Priority
837 Views
Last Modified: 2012-05-06
Hi,

I have a field in my mysql database as longblob which is meant to store images  of the product. The problem now is I want to store image to this filed from the picturebox. Can somebody give me working code to do the same. As well as how I can retrieve the image and display in the picturebox. I want this code to be working for Visual Basic .net application version 2005. I got a lot of code for PHP and ASP.net but not a single code for vb. please help. its urgent.
DROP TABLE IF EXISTS `imint_new`.`tbl_products`;
CREATE TABLE  `imint_new`.`tbl_products` (
  `p_code` varchar(15) NOT NULL,
  `p_name` varchar(45) NOT NULL,
  `p_mrp` decimal(9,2) NOT NULL,
  `p_cost` decimal(9,2) NOT NULL,
  `p_courier` decimal(7,2) DEFAULT NULL,
  `p_weight` decimal(6,3) NOT NULL,
  `p_dispatch` tinyint(1) unsigned NOT NULL,
  `p_image` longblob,
  `p_active` tinyint(1) unsigned NOT NULL,
  PRIMARY KEY (`p_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='A table giving description of the products';

Open in new window

Comment
Watch Question

Author

Commented:
this does not work. inserts NULL value. Have tried it.
Hi,

I got the solution:

For Insert-

Mysql has an inbuilt function load_file('c:/bc.jpg')

For read-



conn.ConnectionString = My.Settings.conn_str
        Try
            str = "select p_image from tbl_products where p_code='SDH'"
            conn.Open()
            mycom.Connection = conn
            mycom.CommandText = str
 
            b = mycom.ExecuteScalar()
            If (b.Length > 0) Then
                Dim stream As New MemoryStream(b, True)
                stream.Write(b, 0, b.Length)
                PictureBox1.Image = New Bitmap(stream)
                stream.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.