I want to insert image into access database from visualbasic.
When I store image path as text into database it works for me but, I want to save image directly to database.
How can I do this ?
Printable View
I want to insert image into access database from visualbasic.
When I store image path as text into database it works for me but, I want to save image directly to database.
How can I do this ?
This code is to insert the image in the database.
Code:Dim theFS As New FileStream(Application.StartupPath & "\testImage.JPG", FileMode.Open)
Dim theBytes(theFS.Length) As Byte theFS.Read(theBytes, 0, theBytes.Length)
theFS.Close()
Dim theQuery As New OleDbCommand("INSERT INTO Table1([image]) VALUES(?)", new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;"))
Dim p1 As New OleDbParameter("@p1", OleDbType.Binary)
p1.Value = theBytes
theQuery.Parameters.Add(p1)
theQuery.Connection.Open()
theQuery.ExecuteNonQuery()
theQuery.Connection.Close()
This code is to Read the image from the database.
Code:Dim theQuery As New OleDbCommand("SELECT ([image]) FROM Table1", New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\db1.mdb;User Id=admin;Password=;")) Dim theDataSet As New DataSet()
Dim theDataAdapter As New OleDbDataAdapter(theQuery)
theQuery.Connection.Open()
theDataAdapter.Fill(theDataSet)
theQuery.Connection.Close()
'get row Dim theItem As Object = theDataSet.Tables(0).Rows(0).Item(0)
Dim correctObject() As Byte = CType(theItem, Byte())
If correctObject Is Nothing = False Then 'read into memstream
Using theMemStream As New MemoryStream() theMemStream.Write(correctObject, 0, correctObject.Length)
theMemStream.Position = 0
Me.PictureBox1.Image = Image.FromStream(theMemStream)
End Using
End If