2015-04-02 68 views
0

我的C#代碼正在與一個OLE數據庫更新OLE數據庫中的圖片框圖像值。在標題爲ProjectParty的表格中,我通過WinForm保存了一個以字節爲單位的圖片值。話雖這麼說,我有一個應該允許用戶更新表的值,包括圖片另一種形式。
enter image description here
我嘗試使用下面的代碼來更新用戶更改回表。如何使用C#

private void btnSaveChanges_Click(object sender, EventArgs e) 
{ 
    OleDbConnection oleDbConn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=HesabKetab.accdb;Persist Security Info=False"); 
    OleDbCommand oleDbCmd = new OleDbCommand("UPDATE ProjectParty SET [email protected], [email protected], [email protected], [email protected], [email protected], [email protected] " 
     + "WHERE [email protected]",oleDbConn); 
    //add parameters 
    oleDbCmd.Parameters.AddWithValue("@title", txtInstTitle.Text); 
    oleDbCmd.Parameters.AddWithValue("@manager", txtInstManager.Text); 
    oleDbCmd.Parameters.AddWithValue("@tel", txtOfficePhone.Text); 
    oleDbCmd.Parameters.AddWithValue("@mobile", txtMobileNo.Text); 
    oleDbCmd.Parameters.AddWithValue("@address", txtAddress.Text); 
    oleDbCmd.Parameters.AddWithValue("@id", Convert.ToInt32(comboInstID.SelectedValue.ToString())); 
    byte[] picByte = null; 
    if(pictureBoxInstLogo.Image!=DBNull.Value){ 
     picByte= (byte[])pictureBoxInstLogo.Image; 
    } 
    oleDbCmd.Parameters.AddWithValue("@picture", picByte); 
    try 
    { 
     oleDbConn.Open(); 
     oleDbCmd.ExecuteNonQuery(); 
    } 
    catch (Exception ex) { MessageBox.Show(ex.Message, "خطای دیتابیس", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } 

} 

我得到的語法錯誤,

操作!=不能應用於類型爲System.Drawing.Image和System.DBNull的操作數。

無法隱式轉換爲System.Drawing.Image爲byte []。

我應該如何檢查pictureBox是否有圖像,如果有,請將其內容保存回數據庫?

回答

1
pictureBoxInstLogo.Image != null 

這將檢查PictureBox的有圖像或沒有,

,如果是的話,你需要將圖像轉換爲使用locacation字節有一個形象;

picByte = System.IO.File.ReadAllBytes(pictureBoxInstLogo.ImageLocation) 
+0

的PictureBox已經從數據庫加載。它是否還有圖像定位?在我看來imagelocation是像c:\ img.jpg這樣的url路徑。 – JasonStack 2015-04-02 11:33:28

+0

我沒有用一個使用數據庫中的圖像,雖然我調試通過的代碼,看看當圖像已經從數據庫加載什麼ImageLocation包含一個PictureBox。 – horHAY 2015-04-02 12:41:15