asp.net 將圖片上傳到mysql數(shù)據(jù)庫(kù)的方法
更新時(shí)間:2009年06月17日 01:01:10 作者:
圖片通過(guò)asp.net上傳到mysql數(shù)據(jù)庫(kù)的方法
這是頁(yè)面上的按鈕單擊事件
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配置圖片上傳最簡(jiǎn)單的方法
- asp.net多圖片上傳實(shí)現(xiàn)程序代碼
- Asp.net 2.0 無(wú)刷新圖片上傳 顯示縮略圖 具體實(shí)現(xiàn)
- Asp.net圖片上傳實(shí)現(xiàn)預(yù)覽效果的簡(jiǎn)單代碼
- KindEditor圖片上傳的Asp.net代碼實(shí)例
- asp.net圖片上傳實(shí)例
- ASP.NET簡(jiǎn)單好用功能齊全圖片上傳工具類(水印、縮略圖、裁剪等)
- asp.net文件上傳解決方案(圖片上傳、單文件上傳、多文件上傳、檢查文件類型)
- ASP.NET圖片上傳實(shí)例(附源碼)
相關(guān)文章
MySQL學(xué)習(xí)之日期函數(shù)的用法詳解
本文將學(xué)習(xí)MySQL的日期函數(shù),在前面章節(jié)的練習(xí)中,我們就利用過(guò)NOW()函數(shù)來(lái)獲取過(guò)當(dāng)前系統(tǒng)時(shí)間,用DATEDIFF函數(shù)來(lái)計(jì)算日期相差的天數(shù),接下來(lái)我們就系統(tǒng)的學(xué)習(xí)一下 日期函數(shù)2022-08-08
MySQL報(bào)1045錯(cuò)誤的幾種可能場(chǎng)景
mysql數(shù)據(jù)庫(kù)中常常會(huì)遇見(jiàn)1045錯(cuò)誤,本文主要介紹了MySQL報(bào)1045錯(cuò)誤的幾種可能場(chǎng)景,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
MySQL MVVC多版本并發(fā)控制的實(shí)現(xiàn)詳解
在多版本并發(fā)控制中,為了保證數(shù)據(jù)操作在多線程過(guò)程中,保證事務(wù)隔離的機(jī)制,降低鎖競(jìng)爭(zhēng)的壓力,保證較高的并發(fā)量。在每開(kāi)啟一個(gè)事務(wù)時(shí),會(huì)生成一個(gè)事務(wù)的版本號(hào),被操作的數(shù)據(jù)會(huì)生成一條新的數(shù)據(jù)行2022-08-08
MySQL安裝與創(chuàng)建用戶操作(新手入門(mén)指南)
這篇文章主要為大家介紹了MySQL安裝與創(chuàng)建用戶的使用講解是非常適合小白新手的入門(mén)學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
MySQL中的insert-on-duplicate語(yǔ)句舉例詳解
這篇文章主要給大家介紹了關(guān)于MySQL中insert-on-duplicate語(yǔ)句的相關(guān)資料,文中通過(guò)圖文以及代碼示例將insert-on-duplicate的用法介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2024-06-06
mysql內(nèi)連接,連續(xù)兩次使用同一張表,自連接方式
這篇文章主要介紹了mysql內(nèi)連接,連續(xù)兩次使用同一張表,自連接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
MySQL更新存放JSON的字段、\“ 轉(zhuǎn)義成 “的問(wèn)題描述
本篇介紹在執(zhí)行MySQL線上變更時(shí)遇到的問(wèn)題,表現(xiàn)為"更新JSON字段時(shí),實(shí)際更新的值與SQL語(yǔ)句中的值不一致,JSON格式錯(cuò)誤",本文給大家分享問(wèn)題描述及解決方案,感興趣的朋友一起看看吧2022-12-12

