How to upload images to the mysql database through asp.net
How to upload images to the mysql database through asp.net
This is the button-Click Event on the page.
The Code is as follows:
Protected void button#click (object sender, EventArgs e)
{
String tid = Utils. getRandom (32 );
Stream mystream = this. FileUpload1.PostedFile. InputStream;
Int length = this. FileUpload1.PostedFile. ContentLength;
Byte [] pic = new byte [length];
Mystream. Read (pic, 0, length );
Bool flg = insert (tid, pic );
}
This is the insert method.
The Code is as follows:
Public bool insert (string tid, byte [] pic)
{
DBConn db = new DBConn ();
StringBuilder SQL = new StringBuilder ();
SQL. Append ("insert into teacher (TID, TPHOTO, TDELETE) values (? Tid ,? Pic ,? Flg )");
Int flg = 0;
Try
{
MyConnection = db. getConnection ();
MySqlCommand myCommand = new MySqlCommand (SQL. ToString (), myConnection );
MyCommand. Parameters. Add (new MySqlParameter ("? Tid ", MySqlDbType. String, 32 ));
MyCommand. Parameters ["? Tid "]. Value = tid;
MyCommand. Parameters. Add (new MySqlParameter ("? Pic ", MySqlDbType. Blob ));
MyCommand. Parameters ["? Pic "]. Value = pic;
MyCommand. Parameters. Add (new MySqlParameter ("? Flg ", MySqlDbType. Int16 ));
MyCommand. Parameters ["? Flg "]. Value = 0;
MyConnection. Open ();
Flg = myCommand. ExecuteNonQuery ();
}
Catch (Exception ex)
{
Return false;
}
Finally
{
If (myConnection! = Null)
{
MyConnection. Close ();
}
}
If (flg> 0)
{
Return true;
}
Return false;
}