none
How to insert and received image SQLSERVER (data type: varbinary) use connection string in VB.net ?

    Question

  • How to insert and received image SQLSERVER (data type: varbinary) use connection string in VB.net ?
    Sunday, April 07, 2013 4:23 PM

Answers

  • try this:

    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'save image
            Dim conn As New SqlConnection("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")
            conn.Open()
            Dim cmd As New SqlCommand("INSERT INTO tableName (imageField) VALUES(@image)", conn)
            Dim p As New SqlParameter("@image", SqlDbType.VarBinary)
            Dim ms As New IO.MemoryStream
            PictureBox1.Image.Save(ms, Drawing.Imaging.ImageFormat.Png)
            p.Value = ms.ToArray
            cmd.ExecuteNonQuery()
            conn.Close()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'retrieve image
            Dim conn As New SqlConnection("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")
            Dim cmd As New SqlCommand("SELECT imageField FROM tableName WHERE someField = @value", conn)
            Dim p As New SqlParameter("@value", SqlDbType.Int)
            p.Value = 1
            Dim o As Object = cmd.ExecuteScalar
    
            If o IsNot Nothing Then
                PictureBox1.Image = Image.FromStream(New IO.MemoryStream(DirectCast(o, Byte())))
            End If
        End Sub
    
    End Class


    thanks for any help

    • Marked as answer by srajmuneer Sunday, April 07, 2013 7:31 PM
    Sunday, April 07, 2013 6:15 PM
  • thank you for the solution

    please in addition to the code above using a (dialog box) when you select the image

    here's how to use dialogs:

    http://www.scproject.biz/Using%20Dialogs.htm


    thanks for any help

    • Marked as answer by srajmuneer Sunday, April 07, 2013 7:31 PM
    Sunday, April 07, 2013 7:14 PM

All replies

  • try this:

    Imports System.Data.SqlClient
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'save image
            Dim conn As New SqlConnection("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")
            conn.Open()
            Dim cmd As New SqlCommand("INSERT INTO tableName (imageField) VALUES(@image)", conn)
            Dim p As New SqlParameter("@image", SqlDbType.VarBinary)
            Dim ms As New IO.MemoryStream
            PictureBox1.Image.Save(ms, Drawing.Imaging.ImageFormat.Png)
            p.Value = ms.ToArray
            cmd.ExecuteNonQuery()
            conn.Close()
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'retrieve image
            Dim conn As New SqlConnection("Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;")
            Dim cmd As New SqlCommand("SELECT imageField FROM tableName WHERE someField = @value", conn)
            Dim p As New SqlParameter("@value", SqlDbType.Int)
            p.Value = 1
            Dim o As Object = cmd.ExecuteScalar
    
            If o IsNot Nothing Then
                PictureBox1.Image = Image.FromStream(New IO.MemoryStream(DirectCast(o, Byte())))
            End If
        End Sub
    
    End Class


    thanks for any help

    • Marked as answer by srajmuneer Sunday, April 07, 2013 7:31 PM
    Sunday, April 07, 2013 6:15 PM
  • thank you for the solution

    please in addition to the code above using a (dialog box) when you select the image

    Sunday, April 07, 2013 7:11 PM
  • thank you for the solution

    please in addition to the code above using a (dialog box) when you select the image

    here's how to use dialogs:

    http://www.scproject.biz/Using%20Dialogs.htm


    thanks for any help

    • Marked as answer by srajmuneer Sunday, April 07, 2013 7:31 PM
    Sunday, April 07, 2013 7:14 PM
  • Thank you for answering your answer was fantastic
    Sunday, April 07, 2013 7:32 PM