ARTICLE

How to insert the image in sql server 2008 using VB.NET

Posted by Munesh Sharma Articles | ADO.NET in VB.NET May 10, 2011
In this article, we will learning how to store a image in sql server 2008.
Download Files:
 
Reader Level:

There are various steps to store a image in sql Server 2008, That are given bellow:

  • Create a table with two columns id (int) and img (image):
    CREATE TABLE [dbo].[imgtable](
            [id] [int] NULL,
            [img] [image] NULL
     
    )
    ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] 

    sql-tabel.gif

  • Design a window form with a Button1:

    win-design.gif

  • Write these coding of Button1:
    Imports System.Data.SqlClient
    Imports
    System.IO 
    Public
    Class Form1
        Dim s1 As String
      
        Private Sub Button1_Click(ByVal senderAs System.Object, ByVal e As System.EventArgs)Handles Button1.Click
            Dim str As String = "Data Source=MCNDESKTOP14;Initial Catalog=studentinfo; uid=sa;pwd=wintellect"
            Dim conAs SqlConnection
            con = New SqlConnection(str) 
            Dim fName As String
     
            fName = "D:\picture\aur2.jpg"
            If File.Exists(fName)Then
                Dim idAs Integer = 1
                Dim contentAs Byte() = ImageToStream(fName)
                con.Open() 
                Dim cmd As New SqlCommand("insert into imgtable (id,img) values ( @id,@img)", con)           cmd.Parameters.AddWithValue("@id", id)
                cmd.Parameters.AddWithValue("@img", content)
                cmd.ExecuteNonQuery() 
                con.Close()
                MsgBox("Image inserted")
            Else
                MsgBox(fName & " not found ")
            End If
     
        End Sub
        Private Function ImageToStream(ByVal fileNameAs String)As Byte()
            Dim stream As New MemoryStream()
    tryagain:
            Try
                Dim imageAs New Bitmap(fileName)
                image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
            Catch ex As Exception
                GoTo tryagain
            End Try
     
            Return stream.ToArray()
        End Function
             
    End
    Class 

  • Output:

      output.gif

     output-2.gif

Login to add your contents and source code to this article
share this article :
post comment
 

Hi I Downloaded Your Sample Application I Have executed it Id Is Inserted While content Is Not I Tried It Through SQL Query window Its OK Id And Image Are Inserted. Also If Possible How Can I Retrieve Image And Display On A Form. Much Appreciated

Posted by wahid kalo May 16, 2011
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Diagram
Become a Sponsor