C # upload the image to the database in binary format. when reading the image and displaying it in picturebox, the system prompts "invalid parameter". After searching for a long time, the system finally finds the problem:
PostCode: Modified code
Openfiledialog ofd = new openfiledialog ();
If (OFD. showdialog () = dialogresult. OK)
{
This. picturebox1.image = image. fromfile (OFD. filename );
This. picturebox1.sizemode = pictureboxsizemode. Zoom;
Filestream FS = new filestream (OFD. filename, filemode. Open, fileaccess. Read );
Byte [] buffbyte = new byte [fs. Length];
FS. Read (buffbyte, 0, (INT) fs. Length );
FS. Close ();
Stringbuilder strsql = new stringbuilder ();
Strsql. append ("insert into eiis_evidence_imgs (");
Strsql. append ("evidence_no, evidence_img, organization_id, delete_flag, create_user, create_datetime, current_img, transfer_id )");
Strsql. append ("values (");
Strsql. append ("@ evidence_no, @ evidence_img, @ organization_id, @ delete_flag, @ create_user, @ create_datetime, @ current_img, @ transfer_id )");
Strsql. append ("; select @ identity ");
Sqlparameter [] parameters = {
New sqlparameter ("@ evidence_no", sqldbtype. varchar, 50 ),
New sqlparameter ("@ evidence_img", sqldbtype. Image ),
New sqlparameter ("@ organization_id", sqldbtype. Int, 4 ),
New sqlparameter ("@ delete_flag", sqldbtype. Char, 1 ),
New sqlparameter ("@ create_user", sqldbtype. varchar, 30 ),
New sqlparameter ("@ create_datetime", sqldbtype. datetime ),
New sqlparameter ("@ current_img", sqldbtype. Char, 1 ),
New sqlparameter ("@ transfer_id", sqldbtype. INT)
};
Parameters [0]. value = Evi. evidence_no;
Parameters [1]. value = buffbyte;
Parameters [2]. value = globalclass. User. organization_id;
Parameters [3]. value = convert. toint32 (deleteenum. undelete). tostring ();
Parameters [4]. value = globalclass. User. user_no;
Parameters [5]. value = datetime. now;
Parameters [6]. value = convert. toint32 (yesorno. Yes). tostring ();
Parameters [7]. value = evitransfer. transfer_id;
Database. setbusinessconnection (globalclass. busiconnstr );
Object OBJ = database. getscalar (strsql. tostring (), parameters );
}
Code after modification:
openfiledialog ofd = new openfiledialog ();
If (OFD. showdialog () = dialogresult. OK)
{< br> This. picturebox1.image = image. fromfile (OFD. filename);
This. picturebox1.sizemode = pictureboxsizemode. zoom;
Filestream FS = new filestream (OFD. filename, filemode. Open, fileaccess. Read );
Byte [] buffbyte = new byte [fs. Length];
FS. Read (buffbyte, 0, (INT) fs. Length );
FS. Close ();
Stringbuilder strsql = new stringbuilder ();
Strsql. append ("insert into eiis_evidence_imgs (");
Strsql. append ("evidence_no, evidence_img, organization_id, delete_flag, create_user, create_datetime, current_img, transfer_id )");
Strsql. append ("values (");
Strsql. append ("@ evidence_no, @ evidence_img, @ organization_id, @ delete_flag, @ create_user, @ create_datetime, @ current_img, @ transfer_id )");
Strsql. append ("; select @ identity ");
Sqlparameter [] parameters = {
New sqlparameter ("@ evidence_no", sqldbtype. varchar, 50 ),
New sqlparameter ("@ evidence_img", sqldbtype. Image, 16 ),
New sqlparameter ("@ organization_id", sqldbtype. Int, 4 ),
New sqlparameter ("@ delete_flag", sqldbtype. Char, 1 ),
New sqlparameter ("@ create_user", sqldbtype. varchar, 30 ),
New sqlparameter ("@ create_datetime", sqldbtype. datetime ),
New sqlparameter ("@ current_img", sqldbtype. Char, 1 ),
New sqlparameter ("@ transfer_id", sqldbtype. INT)
};
Parameters [0]. value = Evi. evidence_no;
Parameters [1]. value = buffbyte;
Parameters [2]. value = globalclass. User. organization_id;
Parameters [3]. value = convert. toint32 (deleteenum. undelete). tostring ();
Parameters [4]. value = globalclass. User. user_no;
Parameters [5]. value = datetime. now;
Parameters [6]. value = convert. toint32 (yesorno. Yes). tostring ();
Parameters [7]. value = evitransfer. transfer_id;
Database. setbusinessconnection (globalclass. busiconnstr );
Object OBJ = database. getscalar (strsql. tostring (), parameters );
}
The parameter sqlparameter [] parameters is defined to write new sqlparameter ("@ evidence_img", sqldbtype. Image) as new sqlparameter ("@ evidence_img", sqldbtype. Image, 16 ),
Dizzy to death. It's really low.