Import Image From DialogBox. Store, Retrieve Image In And From Database

May i know how to store image for example .jpeg in database I have already set a column datatype to image in my SQL Server but now i have no idea how to use dialogbox to select a image file and import it and store into database. Besides that, how to retrieve the image and display in imagebox or picturebox Thank you.

Answer this question

Import Image From DialogBox. Store, Retrieve Image In And From Database

  • Burrough

    this is same problem with me. who can help me

  • Allah Is Here

    Hi there,

    there is a problem not with reading from the Database but to store the Picture in the Database.
    If you both correct it, like the way shown below, it works great. Here is the code:
    You HAVE to work with paramters. To give the Memorystream from the Image to
    the right Parameter is the most important step:

    \\\
    Windows.Forms.Cursor.Current = Cursors.WaitCursor

    Dim constring As String = "Provider=SQLOLEDB; Data Source=nkdpc134\SQLEXPRESS; Database=CLDatabase; UID=breiter; PWD=*****;"

    OleDbConnection1 = New OleDb.OleDbConnection(constring)

    OleDbCommand1.CommandText = "INSERT INTO Emp_Profile (emp_no,work_permit_no,portrait) VALUES( , , )"

    OleDbCommand1.Connection = OleDbConnection1

    OleDbConnection1.Open()


    With OleDbCommand1
    .Parameters.Add(New OleDb.OleDbParameter(" ", System.Data.OleDb.OleDbType.VarChar)).Value = "fwefwe1"
    .Parameters.Add(New OleDb.OleDbParameter(" ", System.Data.OleDb.OleDbType.VarChar)).Value = "fwefwe123"
    .Parameters.Add(New OleDb.OleDbParameter(" ", System.Data.OleDb.OleDbType.LongVarBinary)).Value = Image
    End With

    OleDbCommand1.ExecuteNonQuery()

    OleDbConnection1.Close()
    ///

  • WinFXGuy

    That's how I store and retrieve Images to and from a SQL Server:
    If you look at my SQL String:
    In the Table Trailer, the Column Picture has as Datatyp Image

    \\\Store the Picture in a Table:

    Dim f As New IO.FileInfo(Filenname)
    Dim fs As IO.FileStream
    Dim Image(f.Length - 1) As Byte
    fs = f.OpenRead
    fs.Read(Image, 0, f.Length - 1) 'Read
    fs.Close()

    Windows.Forms.Cursor.Current = Cursors.WaitCursor

    SQL.Connection = New SqlClient.SqlConnection(SQL.ConnectionString)


    SQL.Query = "INSERT INTO Trailer (MovieID, Picture, PictureExt) "
    SQL.Query &= "VALUES (@id, @image, @ext)"

    Dim com As New SqlClient.SqlCommand(SQL.Query, SQL.Connection)

    With com
    .Parameters.Add(New SqlClient.SqlParameter("@id", SqlDbType.VarChar)).Value = id
    .Parameters.Add(New SqlClient.SqlParameter("@image", SqlDbType.Image)).Value = Image
    .Parameters.Add(New SqlClient.SqlParameter("@ext", SqlDbType.NChar)).Value = Ext
    End With

    SQL.Connection.Open()
    com.ExecuteNonQuery()
    SQL.Connection.Close()

    \\\ Retrieve the Picture

    Dim ImageLength As Long
    Dim Image() As Byte

    SQL.Connection = New SqlClient.SqlConnection(SQL.ConnectionString)
    SQL.Query = "SELECT DATALENGTH(Picture), Picture, PictureExt FROM Trailer WHERE MovieID LIKE '" & id & "' AND Picture IS NOT NULL"
    Dim com As New SqlClient.SqlCommand(SQL.Query, SQL.Connection)
    SQL.Connection.Open()
    SQL.Reader = com.ExecuteReader
    If SQL.Reader.HasRows = True Then
    SQL.Reader.Read()
    ImageLength = CLng(SQL.Reader.GetValue(0))
    ReDim Image(ImageLength - 1)
    SQL.Reader.GetBytes(1, 0, Image, 0, ImageLength - 1)
    Dim memStream As New System.IO.MemoryStream()
    memStream.Write(Image, 0, Image.Length - 1)
    Dim b As Bitmap = New Bitmap(memStream)
    pbCover.SizeMode = PictureBoxSizeMode.StretchImage
    pbCover.Image = b
    End If
    SQL.Reader.Close()
    SQL.Connection.Close()

  • ACCOUNTINGONLINE.US

    I don't know how to make use of dialogbox and import image from there before i store into database Besides that, i have used the OLEDB and the code shown as above is based on another component. Anyone can show me the sample code on OLEDB. Thank you.
  • V.E

    Besides that, i have problem with the following code, when the pointer reached the underline statement, an error message "Parameter is not valid" came out. May i know how to solve this Thank you.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Try

    Dim FileName_String As String

    OpenFileDialog1.ShowDialog(Me)

    FileName_String = OpenFileDialog1.FileName

    Dim Image_File As New IO.FileInfo(FileName_String)

    Dim File_Stream_Of_Image As IO.FileStream

    Dim Image(Image_File.Length - 1) As Byte

    File_Stream_Of_Image = Image_File.OpenRead

    File_Stream_Of_Image.Read(Image, 0, Image_File.Length - 1) 'Read

    File_Stream_Of_Image.Close()

    Windows.Forms.Cursor.Current = Cursors.WaitCursor

    Dim constring As String = "Provider=SQLOLEDB; Data Source=YAP\SQLEXPRESS; Database=CLDatabase; UID=sa; PWD=sa;"

    OleDbConnection1 = New OleDbConnection(constring)

    OleDbCommand1.CommandText = "INSERT INTO Emp_Profile (emp_no,work_permit_no,portrait) VALUES('fwefwe1','fwefwef213','" & Image(Image_File.Length - 1) & "')"

    OleDbCommand1.Connection = OleDbConnection1

    OleDbConnection1.Open()

    Dim DReader As OleDb.OleDbDataReader = OleDbCommand1.ExecuteReader

    DReader.Close()

    OleDbConnection1.Close()

    Catch err As System.Exception

    MessageBox.Show(err.Message)

    End Try

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Try

    Dim ImageLength As Long

    Dim Image() As Byte

    Dim constring As String = "Provider=SQLOLEDB; Data Source=YAP\SQLEXPRESS; Database=CLDatabase; UID=sa; PWD=sa;"

    OleDbConnection1 = New OleDbConnection(constring)

    OleDbCommand1.CommandText = "SELECT DATALENGTH(portrait), portrait FROM Emp_Profile WHERE emp_no = 'fwefwe1'"

    OleDbCommand1.Connection = OleDbConnection1

    OleDbConnection1.Open()

    Dim DReader As OleDb.OleDbDataReader = OleDbCommand1.ExecuteReader

    If DReader.HasRows = True Then

    DReader.Read()

    ImageLength = CLng(DReader.GetValue(0))

    ReDim Image(ImageLength - 1)

    DReader.GetBytes(1, 0, Image, 0, ImageLength - 1)

    Dim memStream As New System.IO.MemoryStream()

    memStream.Write(Image, 0, Image.Length - 1)

    Dim b As Bitmap = New Bitmap(memStream)

    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage

    PictureBox1.Image = b

    End If

    DReader.Close()

    OleDbConnection1.Close()

    Catch err As System.Exception

    MessageBox.Show(err.Message)

    End Try

    End Sub


  • Pankaj11

    Based on your previous code
     
                Dim f As New IO.FileInfo(Filenname)
                Dim fs As IO.FileStream
                Dim Image(f.Length - 1) As Byte
                fs = f.OpenRead
                fs.Read(Image, 0, f.Length - 1) 'Read
                fs.Close()
     
    For this line
    "Parameters.Add(New SqlClient.SqlParameter("@image", SqlDbType.Image)).Value = Image"
     
    I tried to modify to make it applicable for OLEDB and however the system cannot recognize image

    OleDbCommand1.CommandText = "INSERT INTO Emp_Profile (portrait) VALUES('" & Image & "')"

    So Can i change it to the following line Thank you.
    OleDbCommand1.CommandText = "INSERT INTO Emp_Profile (portrait) VALUES('" & Image(Image_File.Length - 1) & "')"

  • YMaod

    Tanks for Lars Breiter.
    we cant store and streaming picture if conection engine use sqlconnection, not use OLEDB connection.

  • Import Image From DialogBox. Store, Retrieve Image In And From Database