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.

Import Image From DialogBox. Store, Retrieve Image In And From Database
Burrough
Allah Is Here
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
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
V.E
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)
'ReadFile_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.ExecuteReaderDReader.Close()
OleDbConnection1.Close()
Catch err As System.ExceptionMessageBox.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 ThenDReader.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 IfDReader.Close()
OleDbConnection1.Close()
Catch err As System.ExceptionMessageBox.Show(err.Message)
End TryEnd Sub
Pankaj11
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()
OleDbCommand1.CommandText = "INSERT INTO Emp_Profile (portrait) VALUES('" & Image & "')"
So Can i change it to the following line Thank you.YMaod
we cant store and streaming picture if conection engine use sqlconnection, not use OLEDB connection.