Results 1 to 8 of 8

Thread: Insert Image in SQL DB

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    115

    Insert Image in SQL DB

    Hello everyone .

    The thing is as follows:
    - 1 picture box
    - 1 button

    - on_click picturebox = loads image from HDD with OpenFileDialog
    - on button = validates if there is a pic in PictureBox.Image
    saves PictureBox.Image in DB


    I know how to do everything except saving the in the DB.
    I am using SQL server 2008 DB .

    Any help ?

    Thanks in advance.
    Best Regards .

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

    Re: Insert Image in SQL DB

    Follow the CodeBank link in my signature and check out my thread dedicated to just that.
    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
    Lively Member
    Join Date
    Jul 2010
    Posts
    115

    Re: Insert Image in SQL DB

    vb Code:
    1. Imports System.Data.SqlClient
    2. Public Class Form1
    3.     Dim File As String
    4.  
    5.     Private Sub FotografiaBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FotografiaBox.Click
    6.         GetImage.Filter = "Image Files (BMP, JPEG, PNG)|*.bmp;*.jpg;*.jpeg;*.png"
    7.         GetImage.ShowDialog()
    8.  
    9.         Try
    10.             FotografiaBox.Image = Image.FromFile(GetImage.FileName)
    11.             File = GetImage.FileName
    12.         Catch ex As Exception
    13.             MessageBox.Show(ex.Message)
    14.         End Try
    15.  
    16.     End Sub
    17.  
    18. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    19.         Dim myConnection As New SqlConnection("Server = " & "INSYSGAMEFORCE\SPESCOLAS; Database = Escola; Integrated Security  = sspi;")
    20.         Dim command As New SqlCommand("INSERT INTO teste VALUES(@Picture)", myConnection)
    21.  
    22.         'Create an Image object.
    23.         Using picture As Image = Image.FromFile(File)
    24.  
    25.             'Create an empty stream in memory.    
    26.             Using stream As New IO.MemoryStream
    27.                 'Fill the stream with the binary data from the Image.        
    28.                 picture.Save(stream, Imaging.ImageFormat.Jpeg)
    29.                 'Get an array of Bytes from the stream and assign to the parameter.        
    30.                 command.Parameters.Add("@Picture", SqlDbType.VarBinary).Value = stream.GetBuffer()
    31.             End Using
    32.         End Using
    33.  
    34.         Try
    35.             myConnection.Open()
    36.             command.ExecuteNonQuery()
    37.             myConnection.Close()
    38.         Catch ex As Exception
    39.             MessageBox.Show(ex.Message)
    40.         End Try
    41.  
    42.     End Sub

    I am using the code above to insert the image . In your example you had an UPDATE command but i need an INSERT so I modified it . The problem is that when I click the button it sais "string or binary data would be truncated. The statement has been terminated."
    Can you help me with this pls ?

    The table I am using is
    Table name: teste
    Fields:
    ID - which is int auto inc (+1)
    Imagem - which in varbinary(100)

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

    Re: Insert Image in SQL DB

    Is your image 100 bytes or less in size? If not then it's not going to fit into a varbinary column limited to 100 bytes.
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    115

    Re: Insert Image in SQL DB

    I tried your UPDATE code too and it still gives me the same error message .
    Any idea why it would do that ?

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

    Re: Insert Image in SQL DB

    Quote Originally Posted by HelderMPinhal View Post
    Any idea why it would do that ?
    Um, yeah. The idea in my previous post.
    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

  7. #7
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Insert Image in SQL DB

    Quote Originally Posted by jmcilhinney View Post
    Is your image 100 bytes or less in size? If not then it's not going to fit into a varbinary column limited to 100 bytes.
    jmc already told you what was wrong.

    You have a 100 byte field.

    Change the field definition to varbinary(max) - max is a keyword in this case indicating to create a truly large varbinary() field.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2010
    Posts
    115

    Re: Insert Image in SQL DB

    When I did my last post my browser wasn't refreshed so I didn't see that post , sorry .

    But thanks everyone for your help

Tags for this Thread

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