2015-04-02 64 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