从MySql数据库中检索图片

时间:2014-09-30 18:25:27

标签: mysql vb.net

我想知道这段代码有什么问题,因为当我检索图片时,它会给我一条错误信息:

  

"连接必须有效且开放。"

'Send sql query to retrieve information about the employees
sqlConn = "SELECT Description, Price, Maker, Color, Items, Store_Available, Serial_number, Sales_number, Picture from `mayombe_mdcs`.`shoesinstore` where Serial_number = @serial"

objDataAdapter = New MySqlDataAdapter(sqlConn, objConn)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@serial", strSerial)
Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())


Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)

    'Read the stream and create an Image object from the data.    
    PictureBox1.Image = Image.FromStream(stream)
End Using

'Fill the form with retrieved informations
objDataAdapter.Fill(objDataset)

1 个答案:

答案 0 :(得分:0)

'Send sql query to retreive information about the employees
obConn.open()'<=================================================================================
sqlConn = "SELECT Description, Price, Maker, Color, Items, Store_Available, Serial_number, Sales_number, Picture from `mayombe_mdcs`.`shoesinstore` where Serial_number = @serial"

objDataAdapter = New MySqlDataAdapter(sqlConn, objConn)
objDataAdapter.SelectCommand.Parameters.AddWithValue("@serial", strSerial)
objDataAdapter.SelectCommand.ExecuteScalar()<===================================================
Dim pictureData As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())


Dim picture As Image = Nothing
'Create a stream in memory containing the bytes that comprise the image.
Using stream As New IO.MemoryStream(pictureData)

    'Read the stream and create an Image object from the data.    
    PictureBox1.Image = Image.FromStream(stream)
End Using

'Fill the form with retreived informations
objDataAdapter.Fill(objDataset)