asp.net 將圖片上傳到mysql數據庫的辦法。本站提示廣大學習愛好者:(asp.net 將圖片上傳到mysql數據庫的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是asp.net 將圖片上傳到mysql數據庫的辦法正文
這是頁面上的按鈕單擊事宜
protected void Button1_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);
}
這是履行拔出的辦法
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;
}