Tools: SQLServer2012 Visual Studio 2013
Scenario: Add a photo to the PictureBox control and save it in the database
I wrote it like this.
An error occurred:
PS: (There is a type of image in the database, because if you store too many photos, it will increase the burden of the database, so save the path of the picture)
Workaround:
Reason: Because picturebox1.imagelocation is null
To modify the code:
if (picturebox1.image! = null)
{
if (picturebox1.imagelocation = = null)
{
paras. ADD (New SqlParameter ("@ photo", DBNull.Value));
}
else {
paras. ADD (New SqlParameter ("@ photo", picturebox1.imagelocation));
}
}
Else
{
MessageBox.Show ("Photo cannot be empty", "hint");
}
You should also save the path to the selected file
This.pictureBox1.Image = Image.FromFile (openfiledialog1.filename);
Picturebox1.imagelocation = @openFileDialog1. filename;//Note to have @
Results:
Database Add Picture Path exception