Results 1 to 12 of 12

Thread: [2005] Image or Photo in database

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    [2005] Image or Photo in database

    Can i store a image in a DB without just saving it's location??? ie. if I have just a .mdb and I want to access this or send this file via email can someone view a photo?? if so how?

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

    Re: [2005] Image or Photo in database

    See the appropriate link in my signature.
    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

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    Great, what type of MDB field do i need?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    This is my code plus my error... what is wrong the DB has number field with a byte size...

    Code:
     Try
                OpenFileDialog1.Filter = "Image Files (*.jpg)|*.jpg"
                OpenFileDialog1.ShowDialog()
                PictureBox1.ImageLocation = OpenFileDialog1.FileName
    
    
                Dim connection As New Data.OleDb.OleDbConnection(My.Settings.Database1ConnectionString)
                Dim sqlqry As New OleDb.OleDbCommand("UPDATE Contacts SET Picture = @Picture WHERE ID = " & TextBox8.Text, connection)
    
                Using picture As Image = Image.FromFile(PictureBox1.ImageLocation)
                    Using stream As New IO.MemoryStream
                        picture.Save(stream, Imaging.ImageFormat.Jpeg)
                        sqlqry.Parameters.Add("@Picture", OleDb.OleDbType.VarBinary).Value = stream.GetBuffer()
                        'SqlDbType.Image).Value = stream.GetBuffer()
                    End Using
                End Using
    
              
                connection.Open()
                sqlqry.ExecuteNonQuery()
                connection.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
    Attached Images Attached Images  

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

    Re: [2005] Image or Photo in database

    I'll run a few tests myself with Access and get back to you.
    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    Cheers! I look forward to your reply.

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

    Re: [2005] Image or Photo in database

    I just used your code and it worked fine for me.
    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

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    What type of DB did you use and what field type with size???

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

    Re: [2005] Image or Photo in database

    MDB, OLE Object. There aren't any other column types that could be construed as applicable to image content.
    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

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    yeah thanks for the reply, I just got it working with that a moment ago, so to delete a picture how do you update with no object?

    I mean this will not work but what does one do?

    Code:
    Try
               
                Dim connection As New Data.OleDb.OleDbConnection(My.Settings.Database1ConnectionString)
                Dim sqlqry As New OleDb.OleDbCommand("UPDATE Contacts SET Picture = "" WHERE ContactID = " & TextBox8.Text, connection)
    
    
                connection.Open()
                sqlqry.ExecuteNonQuery()
                connection.Close()
    
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try

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

    Re: [2005] Image or Photo in database

    To set any database field to null from VB code you assign DBNull.Value to the field or parameter. That means you'd use exactly the same SQL code as before but you'd assign DBNull.Value to the paarmeter instead of a Byte array.
    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

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2003
    Location
    Auckland
    Posts
    1,139

    Re: [2005] Image or Photo in database

    You're a legend Works fine thanks again.

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