By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,436 Members | 2,958 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,436 IT Pros & Developers. It's quick & easy.

how to insert image into database

P: 19
Can anyone help me to write the code for inserting image into database? In my form there is a command button labelled 'save'. When click that button, it gets browse, from that we can select an image and that will be saved on database..

My frontend is VB6 and backend is MS Access 2007. In the database, there is a field called 'image', into which images are stored. I am quite confused to select datatype of that field.

Please help me to write the code. It's very urgent. Thanks in advance.
Oct 31 '07 #1
Share this Question
Share on Google+
3 Replies

Expert 5K+
P: 8,127
Please find the details of storing of images and retrieval of images in the articles section of VB.
Oct 31 '07 #2

P: 57
Hi member,

There is another way to store image and retrieve image from database.
We can use Stream (ADO object) to store or get image from database.

Could you please try, otherwise I will post coding how to do.


Oct 31 '07 #3

P: 37
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Imports System.Data
  3. Imports System.IO
  5.  Public Class Form1
  6.     Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
  7.     Dim con As New SqlClient.SqlConnection
  9.     'To open an image from computer
  10.     '-------------------------------
  11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  12.         OpenFileDialog1.Title = "Please select a file"
  13.         OpenFileDialog1.InitialDirectory = "c:temp"
  14.         OpenFileDialog1.ShowDialog()
  15.         TextBox2.Text = OpenFileDialog1.FileName.ToString  '--->To Show the file path in textbox2
  16.         PictureBox1.ImageLocation = TextBox2.Text  '--->To show selected image in picturebox
  17.     End sub
  19.    'To insert selected image into database
  20.    'The datatype of the column in table to store image should be <image>  
  21.    '--------------------------------------------------------------------
  22.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  23.         con.ConnectionString = str
  24.         Dim ms As New IO.MemoryStream()
  25.         PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
  26.         Dim arrimage() As Byte = ms.GetBuffer
  27.         Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
  28.         cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
  29.         con.Open()
  30.         cmd.ExecuteNonQuery()
  31.         con.Close()
  32.     End Sub
  34.     'We have successfully inserted the image into database.
  35.     'Now we want to Retrieve the image from database.
  36.     '-------------------------------------------------
  37.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  38.         Dim stream As New IO.MemoryStream()
  39.         con.Open()
  40.         Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3.Text + "'", con) '--->You can give Emp_id instead of Textbox value.
  41.         Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
  42.         stream.Write(image, 0, image.Length)
  43.         con.Close()
  44.         Dim bitmap As New Bitmap(stream)
  45.         PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
  46.     End Sub
  47.     'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
Jan 6 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.