简体   繁体   中英

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

My C# code is working with an OLE database. Within a table titled ProjectParty I save a picture value in bytes via a WinForm. That being said, I have another form that is supposed to allow users to update the table's values including 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.

and

Cannot implicitly convert System.Drawing.Image to byte[].

How should I check if a pictureBox has an image and , if so, save its content back into the database?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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