Question Image save on mysql database

Ferhat Gürsu

New member
Joined
Jun 3, 2011
Messages
4
Location
Istanbul, Turkey, Turkey
Programming Experience
10+
i can save on windows xp, this code, but same code dont save image on mysql database on windows 7
Dim ms As MemoryStream = New MemoryStream
PictureBox1.Image.Save(ms, Imaging.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.LongBlob, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
Dim ekle As New MySql.Data.MySqlClient.MySqlCommand("update ogrenci set resim=@BLOBData where id='" & ogrid & "'", cnn)
ekle.CommandType = CommandType.Text

ekle.Parameters.Add(prm)
cnn.Close()
cnn.Open()
ekle.ExecuteNonQuery()
cnn.Close()
 
First up, why are you calling Close on the connection and then immediately calling Open? I would assume that the connection is not actually going to be open anyway but, if it is, why would you close it when you want it open? If you're concerned about Open throwing an exception if the connection is already open then check the State property first.

Second, I strongly recommend that you never use string concatenation to insert variables into SQL code. You should be using a parameter for your ID value too.

As for the issue, what if it doesn't do what you expect then what DOES it do? There are only three possible outcomes when call ExecuteNonQuery:

1. An exception is thrown.
2. It returns zero.
3. It returns a non-zero value.

Which is it in your case?
 
this code work on windows xp but dont work on windows 7,i changed code , executenonquery give me "1"

Dim ms As MemoryStream = New MemoryStream
PictureBox1.Image.Save(ms, Imaging.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.LongBlob, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
Dim ekle As New MySql.Data.MySqlClient.MySqlCommand("update ogrenci set resim=@BLOBData where id='" & ogrid & "'", cnn)
ekle.CommandType = CommandType.Text

ekle.Parameters.Add(prm)
Dim gln As Byte
cnn.Open()
gln = ekle.ExecuteNonQuery()
MsgBox(gln.ToString)
cnn.Close()
*****
this code show "1"
 
If you're getting a non-zero value then the data is being saved. What if you set a second column in the same table? Do you see that change? Also, what does your connection string look like?
 
Dim cnn As New MySql.Data.MySqlClient.MySqlConnection(server=localhost;database=kantin;Uid=fg;Pwd=1523)
Dim ms As MemoryStream = New MemoryStream
PictureBox1.Image.Save(ms, Imaging.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.LongBlob, _
bytBLOBData.Length, ParameterDirection.Input, False, _
0, 0, Nothing, DataRowVersion.Current, bytBLOBData)
Dim ekle As New MySql.Data.MySqlClient.MySqlCommand("update ogrenci set resim=@BLOBData where id='" & ogrid & "'", cnn)
ekle.CommandType = CommandType.Text

ekle.Parameters.Add(prm)
Dim gln As Byte
cnn.Open()
gln = ekle.ExecuteNonQuery()' >>>>> give me 1
MsgBox(gln.ToString)
cnn.Close()
i getting 1.
i dont set second table, i cant see that change. this code dosnt work only windows 7, this code working on windows xp
 
Try the same code but make one change. First, set just a non-blob field and check the database to see if that change works. Next set the blob field and a non-blob field and check the database to see if that change works.
 
I don't know if there's some MySQL-specific issue here but I can only conclude that you're looking in the wrong place for the data because if ExecuteNonQuery is returning then 1 then data is being saved.
 

Latest posts

Back
Top