asp.net 將圖片上傳到mysql數(shù)據(jù)庫的方法
更新時(shí)間:2009年06月17日 01:01:10 作者:
圖片通過asp.net上傳到mysql數(shù)據(jù)庫的方法
這是頁面上的按鈕單擊事件
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);
}
這是執(zhí)行插入的方法
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;
}
復(fù)制代碼 代碼如下:
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);
}
這是執(zhí)行插入的方法
復(fù)制代碼 代碼如下:
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;
}
您可能感興趣的文章:
- asp.net圖片上傳生成縮略圖的注意事項(xiàng)
- ASp.net下fckeditor配置圖片上傳最簡單的方法
- asp.net多圖片上傳實(shí)現(xiàn)程序代碼
- Asp.net 2.0 無刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
- Asp.net圖片上傳實(shí)現(xiàn)預(yù)覽效果的簡單代碼
- KindEditor圖片上傳的Asp.net代碼實(shí)例
- asp.net圖片上傳實(shí)例
- ASP.NET簡單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
- ASP.NET圖片上傳實(shí)例(附源碼)
相關(guān)文章
MySQL數(shù)據(jù)庫手冊(cè)DATABASE操作與編碼(小白入門篇)
這篇文章主要介紹了MySQL數(shù)據(jù)庫手冊(cè)DATABASE操作與編碼的小白入門篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05MySQL授權(quán)用戶訪問數(shù)據(jù)操作方式
用戶授權(quán)操作可以控制數(shù)據(jù)庫用戶對(duì)數(shù)據(jù)庫對(duì)象的訪問權(quán)限,本文就來介紹MySQL授權(quán)用戶訪問數(shù)據(jù)操作方式,感興趣的可以了解一下2023-10-10