Results 1 to 3 of 3

Thread: Insert image in access database from vb 6.0

  1. #1
    Join Date
    Dec 2008
    Posts
    12

    Insert image in access database from vb 6.0

    I want to insert image into access database from visualbasic.
    When I store image path as text into database it works for me but, I want to save image directly to database.
    How can I do this ?

  2. #2
    Join Date
    May 2008
    Posts
    2,302

    Re: Insert image in access database from vb 6.0

    This code is to insert the image in the database.

    Code:
    Dim theFS As New FileStream(Application.StartupPath & "\testImage.JPG", FileMode.Open)
    Dim theBytes(theFS.Length) As Byte theFS.Read(theBytes, 0, theBytes.Length)
    theFS.Close() 
     
    Dim theQuery As New OleDbCommand("INSERT INTO Table1([image]) VALUES(?)", new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;"))
    Dim p1 As New OleDbParameter("@p1", OleDbType.Binary)
    p1.Value = theBytes
    theQuery.Parameters.Add(p1)
    theQuery.Connection.Open()
    theQuery.ExecuteNonQuery()
    theQuery.Connection.Close()

  3. #3
    Join Date
    May 2008
    Posts
    2,302

    Re: Insert image in access database from vb 6.0

    This code is to Read the image from the database.

    Code:
    Dim theQuery As New OleDbCommand("SELECT ([image]) FROM Table1", New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;")) Dim theDataSet As New DataSet()
    Dim theDataAdapter As New OleDbDataAdapter(theQuery)
     
    theQuery.Connection.Open()
    theDataAdapter.Fill(theDataSet)
    theQuery.Connection.Close()
     
    'get row Dim theItem As Object = theDataSet.Tables(0).Rows(0).Item(0)
    Dim correctObject() As Byte = CType(theItem, Byte())
    If correctObject Is Nothing = False Then    'read into memstream
       Using theMemStream As New MemoryStream()       theMemStream.Write(correctObject, 0, correctObject.Length)
          theMemStream.Position = 0
          Me.PictureBox1.Image = Image.FromStream(theMemStream)
       End Using
    End If

Similar Threads

  1. Displaying an Image In a JSP With a Access database
    By Henryosa in forum Software Development
    Replies: 5
    Last Post: 12-03-2010, 02:52 PM
  2. insert into select statement for database
    By Aidan 12 in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 09:08 PM
  3. Replies: 5
    Last Post: 05-01-2010, 10:58 AM
  4. Insert DataGridview into database
    By Neil'o in forum Software Development
    Replies: 3
    Last Post: 24-09-2009, 11:28 AM
  5. Scan image into an Access database
    By Okies in forum Windows Software
    Replies: 2
    Last Post: 22-05-2009, 01:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •