Storing/Retrieving BLOB Data directly into a PictureBox in VB.NET
Posted by: Sourjya Sen
Date: November 17, 2004 12:27PM

Anyone with code samples to retrieve/store blob data directly to the db and back into a PictureBox in VB.NET ?? I've tried converting the code sample on msdn to use the mysql .net connector but it stores only blank data into the blob field. Help needed desperately :)
I'm posting parts of my code here: both the Microsoft version and my own adaptation using the mysql.net connector. Just cant figure out why the BLOB data field receives only NULL data !!!
Thanks,
------------
My Code - tries to insert the picture when you click the cmdSaveDB Button
========
Private Sub cmdSaveDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSaveDB.Click

If picShowPicture.Image Is Nothing Then Exit Sub

Dim ms As MemoryStream = New MemoryStream
picShowPicture.Image.Save(ms, ImageFormat.Jpeg)

Dim bytBLOBData(ms.Length - 1) As Byte

ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)

Dim prm As New MySql.Data.MySqlClient.MySqlParameter("@BLOBData", _
MySql.Data.MySqlClient.MySqlDbType.Blob, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)


Dim ConnectionString As String = "SERVER=localhost;" & _
"PORT=3306;DATABASE=pictureDB;User Id=root;" & _
"PASSWORD=pass;"

Dim QueryString As String = "INSERT INTO blobtest ( BLOBData ) VALUES ( @BLOBData )"

MsgBox(QueryString)

Dim myConnection As New MySqlConnection(ConnectionString)
Dim myCommand As New MySqlCommand(QueryString)
myCommand.Connection = myConnection

myCommand.Parameters.Add(prm)
'Open the connection
myConnection.Open()
'Execute the query
myCommand.ExecuteNonQuery()
'Close the connection
myConnection.Close()

End Sub
======== End of my Code

In Comparision - here's the Microsoft Code:
==============================
Dim cn As New SqlConnection(strCn)
Dim cmd As New SqlCommand("INSERT INTO BLOBTest (BLOBData) " & _
"VALUES (@BLOBData)", cn)
Dim ms As MemoryStream = New MemoryStream()
picBLOB.Image.Save(ms, ImageFormat.Jpeg)
Dim bytBLOBData(ms.Length - 1) As Byte
ms.Position = 0
ms.Read(bytBLOBData, 0, ms.Length)
Dim prm As New SqlParameter("@BLOBData", SqlDbType.VarBinary, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
cmd.Parameters.Add(prm)
cn.Open()
cmd.ExecuteNonQuery()
cn.Close()
==============================

Thanks in advance :)

Options: ReplyQuote


Subject
Written By
Posted
Storing/Retrieving BLOB Data directly into a PictureBox in VB.NET
November 17, 2004 12:27PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.