How to Save an Image to a Database With MS Visual Basic 6

Written by jim campbell
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Save an Image to a Database With MS Visual Basic 6
Store images in your database. (digital image by peter Hires Images from

Your database saves images in binary form, so you can display the images on a webpage or Windows form. The Visual Basic language has a connection syntax that allows you to connect to the database, specify that the information is an image and store it in one of your tables. The image can be one from a webpage or located on the local hard drive. Images are large objects, so the tables need sufficient space to store the binary files. With enough hard drive space, you can create a large image repository for your Visual Basic 6 application.

Skill level:

Other People Are Reading


  1. 1

    Create a memory stream object and assign it to the image. The memory stream reads the binary data from the image and stores it in memory. This binary data is later transferred to a binary array. The following code creates the memory stream and saves the image:

    Dim ms As new MemoryStream() theImage.Save(ms, theImage.RawFormat)

    "theImage" is the ID of the image located on your Visual Basic form. This image element points to a location of an image on the hard drive.

  2. 2

    Create the database connection and SQL query that inserts the image into the database. The connection you create is dependent on the server you use. The following code sets up the connection and creates the "insert" query:

    Dim con As New SqlConnection(string_connection) Dim query As String = "insert into Picture_Table (picture) values (@Picture)" Dim command As New SqlCommand(query, con)

  3. 3

    Set up the image parameter for the query command. The following code sets up a parameter for the SQL code and attaches the image:

    Dim byte Array() As Byte = ms.GetBuffer() command.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = byte Array

  4. 4

    Execute the query and save the image to the database. All the parameters are set up, but the image is still not saved to the database. The code below opens the database connection, executes the SQL code and saves the image to the database table:

    conn.Open() command.ExecuteNonQuery() conn.Close()

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.