Sign in to follow this  

VB.Net mdb data issue

This topic is 4660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello: I am new to the programming world and to VB and have just started foring with data bases. I am using VB.Net 2003 and have created a program for storing information. Everthing is going well except for and issue with being able to store pitures in access 2000 mdb table and recalling it in VB.Net, can someone help me please. I am using OleDb with Access 2000 table and trying to call the picture to an ole object.

Share this post


Link to post
Share on other sites
I do not have code for Access but here it is for SQLServer.

Imports System.Data.SqlClient
Imports System.IO
Public Class clsImageBlobs
Dim sCn As String = "workstation id=TIC000733XP;packet size=4096;user id=sa;data source=TUK000731;persist security info=True;initial catalog=Northwind;password=pwd"
Public Function ShowImage() As Image
Try
Dim cn As New SqlConnection(sCn)
cn.Open()
'Retrieve BLOB from database into DataSet.
Dim cmd As New SqlCommand("SELECT ID, BLOB FROM BLOBTest ORDER BY ID", cn)
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds, "BLOBTest")
Dim c As Integer = ds.Tables("BLOBTest").Rows.Count
If c > 0 Then
'BLOB is read into Byte array, then used to construct MemoryStream,
'then passed to PictureBox.
Dim byteBLOBData(-1) As [Byte]
byteBLOBData = CType(ds.Tables("BLOBTest").Rows((c - 1))("BLOB"), [Byte]())
Dim stmBLOBData As New MemoryStream(byteBLOBData)
ShowImage = Image.FromStream(stmBLOBData)
End If
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
Public Function WriteImage() As Boolean
Try
Dim cn As New SqlConnection(sCn)
Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOB) VALUES (@BLOBData)", cn)
Dim strBLOBFilePath As String = "C:\temp\vclogging.jpg" 'Modify this path as needed.
'Read jpg into file stream, and from there into Byte array.
Dim fsBLOBFile As New FileStream(strBLOBFilePath, FileMode.Open, FileAccess.Read)
Dim bytBLOBData(fsBLOBFile.Length) As [Byte]
fsBLOBFile.Read(bytBLOBData, 0, bytBLOBData.Length)
fsBLOBFile.Close()
'Create parameter for insert command and add to SqlCommand object.
Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, bytBLOBData.Length, ParameterDirection.Input, False, 0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
cmd.Parameters.Add(prm)
'Open connection, execute query, and close connection.
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function
End Class

Share this post


Link to post
Share on other sites
Store the raw image data in a BLOB column. To read the image back, create a stream with the blob column data, and use the FromStream method of the Image class to reconstruct an Image object from the data.

That's how I'd do it, but IANAP.

Share this post


Link to post
Share on other sites

This topic is 4660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this