简体   繁体   English

如何使用C#更新OLE数据库中的PictureBox图片值

[英]How to update a PictureBox picture value in OLE database using C#

My C# code is working with an OLE database. 我的C#代码正在使用OLE数据库。 Within a table titled ProjectParty I save a picture value in bytes via a WinForm. 在名为ProjectParty的表中,我通过WinForm以字节为单位保存图片值。 That being said, I have another form that is supposed to allow users to update the table's values including picture . 话虽这么说,我还有另一种形式,应该允许用户更新表的值,包括picture
在此处输入图片说明
I am trying to use the following code to update user changes back into the table. 我正在尝试使用以下代码将用户更改更新回表中。

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 title=@title, manager=@manager, tel=@tel, mobile=@mobile, address=@address, picture=@picture "
        + "WHERE ID=@id",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); }

}   

I am getting syntax errors, 我收到语法错误,

Operator != cannot be applied to operands of type System.Drawing.Image and System.DBNull. 运算符!=不能应用于类型System.Drawing.Image和System.DBNull的操作数。

and

Cannot implicitly convert System.Drawing.Image to byte[]. 无法将System.Drawing.Image隐式转换为byte []。

How should I check if a pictureBox has an image and , if so, save its content back into the database? 我应该如何检查pictureBox是否具有图像,如果有,将其内容重新保存到数据库中?

pictureBoxInstLogo.Image != null

this will check if the picturebox has an image or not, 这将检查图片框是否有图像,

if it does have an image, you need to convert the image to bytes using the locacation; 如果确实有图像,则需要使用定位将图像转换为字节;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM