Results 1 to 8 of 8

Thread: [2005] How to store Image in SQL Server 2005

  1. #1

    Thread Starter
    Member
    Join Date
    May 2006
    Location
    Bahrain
    Posts
    43

    [2005] How to store Image in SQL Server 2005

    Hey everybody ,

    can anyone tell me How to store Image in SQL Server 2005 using Visual Basic 2005.

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

    Re: [2005] How to store Image in SQL Server 2005

    Create a MemoryStream, Save the Image to the stream, Seek to the beginning of the stream, then get the data from the stream into a Byte array. The Byte array is the object you save to the database. When retrieving the data you use the converse operaions. I've posted code to do this previously, so a search will turn it up for 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

  3. #3
    Lively Member SonicBoomAu's Avatar
    Join Date
    Aug 2004
    Posts
    78

    Re: [2005] How to store Image in SQL Server 2005

    This is straight out of 101 MSFT VB .Net Applications, so you might need to fiddle with it a bit but here goes:

    (this is something i always want to try )

    On Button Press Event

    VB Code:
    1. Dim ms as New MemoryStream()
    2. picSave.Image.Save(ms, picSave.Image.RawFormat)
    3.  
    4. Dim arrImage() as Byte = ms.GetBuffer
    5. ms.Close()
    6.  
    7. Dim strFileName as String = lblFilePath.text.Substring(lblFilePath.Text.LastIndexOf('|") + 1)
    8.  
    9. Dim cnn as New SqlConnection(connectionString)
    10. Dim strSQL as String = _
    11. "INSERT INTO Picture (Filename, Picture)" & _
    12. "VALUES (@Filename, @Picture)"
    13. Dim cmd as NewSQLCommand(strSQL, cnn)
    14.  
    15. With cmd
    16.      .Parameters.Add(New SqlParameter("@Filename", _
    17.           SqlDbType.NVarChar, 50)).Value = strFileName
    18.      .Parameters.Add(New SqlParameter("@Picture", _
    19.           SqlDbType.Image)).Value = arrImage
    20. End With
    21. cnn.Open()
    22. cmd.ExecuteNonQuery()
    23. cnn.Close


    I hope this helps
    Using Visual Studio .NET 2003

    QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
    -- Rick Cook, The Wizardry Compiled

  4. #4
    Lively Member Shardox's Avatar
    Join Date
    Nov 2006
    Location
    Barcelona, Spain
    Posts
    123

    Re: [2005] How to store Image in SQL Server 2005

    When you create the table to store your pictures in SQL2005 declare the field to store images as Varbinary(MAX) instead of Image.

  5. #5
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [2005] How to store Image in SQL Server 2005

    what is the Technical difference of storing images between varbinary(max) and binary ??? i am just curious

  6. #6
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] How to store Image in SQL Server 2005

    Quote Originally Posted by maged
    what is the Technical difference of storing images between varbinary(max) and binary ??? i am just curious
    http://msdn2.microsoft.com/en-us/library/ms188362.aspx

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: [2005] How to store Image in SQL Server 2005

    Quote Originally Posted by Shardox
    When you create the table to store your pictures in SQL2005 declare the field to store images as Varbinary(MAX) instead of Image.
    I was going to ask why (as they are identical) but found the answer here:

    http://msdn2.microsoft.com/en-us/library/ms187993.aspx

    "ntext, text, and image data types will be removed in a future version of Microsoft SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Use nvarchar(max), varchar(max), and varbinary(max) instead. For more information, see Using Large-Value Data Types."

  8. #8
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: [2005] How to store Image in SQL Server 2005

    Thank you for the Link

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