The image upload event code looks like this:
1 byte[] binary =upload. filebytes;2StringBuilder SQLSTRSB =NewStringBuilder ();3Sqlstrsb.append ("Update info set thumb=? Imgwhere id=200");4 stringMysqlconstr ="host=localhost; uid=root; pwd=123; Database=db";5Mysqlconnection Mysqlcon =Newmysqlconnection (MYSQLCONSTR);6 Mysqlcon.open ();7Mysqlcommand mysqlcom =NewMysqlcommand ();8Mysqlcom.commandtext =sqlstrsb.tostring ();9Mysqlcom.connection =Mysqlcon;TenMysqlparameter par =NewMysqlparameter ("? IMG", mysqldbtype.varbinary); OnePar. Value =binary; A mySqlCom.Parameters.Add (PAR); - intAFN =mysqlcom.executenonquery (); -Mysqlcon.close ();
Note: You must use Mysqldbtype.varbinary or mysqldbtype.binary to qualify data types when converting to a binary database.
The picture display code looks like this:
1 stringMysqlconstr ="host=host;uid=root;pwd=123;database=db;";2 stringSqlstr ="Select Thumbdata from info where id=200;";3Mysqlconnection Mysqlcon =Newmysqlconnection (MYSQLCONSTR);4 Mysqlcon.open ();5Mysqlcommand MYSQLCOMD =NewMysqlcommand ();6Mysqlcomd.commandtext =sqlstr;7Mysqlcomd.connection =Mysqlcon;8Mysqldataadapter MYSQLSDA =NewMysqldataadapter ();9Mysqlsda.selectcommand =MYSQLCOMD;TenDataSet ds =NewDataSet (); One Mysqlsda.fill (DS); A if(ds. Tables.count >0) - { - byte[] Databyte = (byte[]) ds. tables[0]. rows[0][0]; the Response.BinaryWrite (databyte); - Response.Flush (); - Response.End (); - } +Mysqlcon.close ();
Page Display section:
1 < src= "showimage.aspx"/>
ASP. NET FileUpload control upload files are stored in binary form into the database