AWestEng
asked on
Image BLOB, MySQL
Hi
I'm trying to fix my code so I can Insert a image from a picture box to a BLOB filed in my MySQL table
I'm not sure how to create the the Query.
Dim QueryString As String = "UPDATE tbl_employed SET(Image) = (@Image) WHERE EmpNr = 1234"
I'm trying to fix my code so I can Insert a image from a picture box to a BLOB filed in my MySQL table
I'm not sure how to create the the Query.
Dim QueryString As String = "UPDATE tbl_employed SET(Image) = (@Image) WHERE EmpNr = 1234"
Public Function InsertImageToBLOB(ByVal QueryString As String, ByVal FieldName As String, ByVal PictureBox As PictureBox) As Integer
Dim ms As MemoryStream = New MemoryStream '// Create a new memory reader
Dim bytBLOBData() As Byte '// Create a byte array to store picture
Dim intBytes As Integer = 0 '// Size of picture
Dim iAffectedRows As Integer = 0 '// Number of affected rows
Try
PictureBox.Image.Save(ms, ImageFormat.Jpeg) '// Get Image from picturebox
intBytes = CInt(ms.Length - 1) '// Calulate byte size of image
ReDim bytBLOBData(intBytes) '// Set size if image byte array
ms.Position = 0 '// Start position for reader
ms.Read(bytBLOBData, 0, CInt(ms.Length)) '// Read the image into the bytBLOBData variable
ms.Close() '// Close reading stream
Catch ex As Exception
Throw
End Try
'// The "Using" block will automatically dispose of the connection when we're finished
Using MyConnectionMySQLOpen As New MySqlClient.MySqlConnection(m_strConnectionString)
Try
Dim prm As New MySql.Data.MySqlClient.MySqlParameter( _
"@" & FieldName, _
MySql.Data.MySqlClient.MySqlDbType.Blob, _
bytBLOBData.Length, _
ParameterDirection.Input, _
False, 0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
'// Open the DB connection
MyConnectionMySQLOpen.Open()
'// Create a new command object
Dim cmd As New MySqlClient.MySqlCommand()
'// Set command properties
With cmd
.Connection = MyConnectionMySQLOpen
.CommandType = CommandType.Text
.CommandText = QueryString
.Parameters.Add(prm)
End With
'// Execute the SQL query with the command object, and get the affected rows in the DB back
iAffectedRows = cmd.ExecuteNonQuery()
'// Close the connection
MyConnectionMySQLOpen.Close()
Catch MyException As MySqlException
Throw
Catch ex As Exception
Throw
Finally
'// Close connection if an exception was thrown before the connection could close
If MyConnectionMySQLOpen.State = ConnectionState.Open Then
MyConnectionMySQLOpen.Close()
End If
End Try
End Using
Return iAffectedRows
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hmm are you sure about the ? the .Net connector generates @ if I use a DataAdapter in a dataset
ASKER
oki. you might be right there :)
No I got this exception
"Data too long for column 'Image' at row 1"
No I got this exception
"Data too long for column 'Image' at row 1"
ASKER
thx m8.. I change to LONGBLOB the Imgae size was 250 kB
ASKER
mankowitz:are you still there?
I will give you the points, but I need help with a reading the blob from MySql an put it back to the picturebox.
I will create a new question, can you help me with that?
I will give you the points, but I need help with a reading the blob from MySql an put it back to the picturebox.
I will create a new question, can you help me with that?
ASKER
here is where the new question is created
https://www.experts-exchange.com/questions/23500417/Image-BLOB-Read-MySQL.html
https://www.experts-exchange.com/questions/23500417/Image-BLOB-Read-MySQL.html
ASKER
Dim prm As New MySql.Data.MySqlClient.MyS
"@" & FieldName, _
MySql.Data.MySqlClient.MyS
bytBLOBData.Length, _
ParameterDirection.Input, _
False, 0, 0, Nothing, DataRowVersion.Current, bytBLOBData)