2014-09-30 50 views
-2

我想知道什麼是錯,此代碼,因爲當我檢索圖像它給了我一個錯誤說:檢索圖片

「連接必須是有效的和開放的。」

'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) 
+4

怎麼了未使用的數據適配器'objDataAdapter'?在調用'ExecuteScalar'之前,調用'sqlConn.Open()'來打開連接。當你解決這個問題時,你可能會收到一個轉換錯誤,因爲'Description'聽起來不像一個圖像。 'ExecuteScalar'返回第一行的第一列。 – 2014-09-30 18:50:12

+0

我會考慮不在數據庫中存儲圖像,而是將文件路徑的字符串值存儲到圖像。您的情況可能會有一些限制阻止這種情況發生,但通常不會將圖像存儲在數據庫中。 看看這個鏈接的更多信息,接受的答案陳述爲什麼非常簡潔。 http://stackoverflow.com/questions/561447/store-pictures-as-files-or-in-the-database-for-a-web-app – davidallyoung 2014-09-30 19:00:57

+3

那裏沒有代碼表明有一個開放或有效的連接只是就像錯誤信息所說的那樣 – Plutonix 2014-09-30 19:06:44

回答

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)