asp.net 圖片的讀寫入庫實現(xiàn)代碼
更新時間:2009年11月12日 21:43:45 作者:
asp.net對圖片的讀寫,實現(xiàn)將圖片保存到數(shù)據(jù)庫中,然后再讀取顯示的實現(xiàn)代碼。
寫圖片c:\1.jpg到表cinfo中
private static void AddCinfo()
{
string strSql = "insert into cinfo (srvtitle,csttitle,introduction,logo) values
(@srvtitle,@csttitle,@introduction,@logo)";
SqlParameter[] parms =
{
new SqlParameter("@srvtitle",SqlDbType.VarChar,30),
new SqlParameter("@csttitle",SqlDbType.VarChar,30),
new SqlParameter("@introduction",SqlDbType.NVarChar,500),
new SqlParameter("@logo",SqlDbType.Image)
};
parms[0].Value = "旅業(yè)互動";
parms[1].Value = "lyhd";
parms[2].Value = "簡介";
string filePath = @"c:\1.jpg";
FileStream fs = File.OpenRead(filePath);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();
parms[3].Value = content;
DBHelper.ExecuteNonQuery(CommandType.Text, strSql, parms);
}
讀取圖片的頁面 test.aspx
protected void Page_Load(object sender, EventArgs e)
{
string strSql = "select * from cinfo where id=1";
SqlDataReader reader=DBHelper.ExecuteReader(CommandType.Text, strSql, null);
if(reader.Read())
{
byte[] c=(byte[])reader["logo"];
Response.BinaryWrite(c);
}
}
用來顯示圖片的頁面 test2.aspx
<img src="test.aspx" />
復(fù)制代碼 代碼如下:
private static void AddCinfo()
{
string strSql = "insert into cinfo (srvtitle,csttitle,introduction,logo) values
(@srvtitle,@csttitle,@introduction,@logo)";
SqlParameter[] parms =
{
new SqlParameter("@srvtitle",SqlDbType.VarChar,30),
new SqlParameter("@csttitle",SqlDbType.VarChar,30),
new SqlParameter("@introduction",SqlDbType.NVarChar,500),
new SqlParameter("@logo",SqlDbType.Image)
};
parms[0].Value = "旅業(yè)互動";
parms[1].Value = "lyhd";
parms[2].Value = "簡介";
string filePath = @"c:\1.jpg";
FileStream fs = File.OpenRead(filePath);
byte[] content = new byte[fs.Length];
fs.Read(content, 0, content.Length);
fs.Close();
parms[3].Value = content;
DBHelper.ExecuteNonQuery(CommandType.Text, strSql, parms);
}
讀取圖片的頁面 test.aspx
復(fù)制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
string strSql = "select * from cinfo where id=1";
SqlDataReader reader=DBHelper.ExecuteReader(CommandType.Text, strSql, null);
if(reader.Read())
{
byte[] c=(byte[])reader["logo"];
Response.BinaryWrite(c);
}
}
用來顯示圖片的頁面 test2.aspx
復(fù)制代碼 代碼如下:
<img src="test.aspx" />
您可能感興趣的文章:
- ASP.NET對txt文件相關(guān)操作(讀取、寫入、保存)
- asp.net cookie的操作,寫入、讀取與操作
- Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄)
- ASP.NET實現(xiàn)二維碼(QRCode)的創(chuàng)建和讀取實例
- asp.net使用npoi讀取excel模板并導(dǎo)出下載詳解
- Asp.net把圖片存入數(shù)據(jù)庫和讀取圖片的方法
- asp.net 未能寫入輸出文件--“拒絕訪問的解決辦法
- asp.net中上傳圖片文件實現(xiàn)防偽圖片水印并寫入數(shù)據(jù)庫
- asp.net 操作XML 按指定格式寫入XML數(shù)據(jù) WriteXml
- ASP.NET 動態(tài)寫入服務(wù)器端控件
- asp.net讀取模版并寫入文本文件
相關(guān)文章
使用源鏈接對ASP.NET Core源代碼進行調(diào)試
這篇文章介紹了使用源鏈接對ASP.NET Core源代碼進行調(diào)試的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-02-02asp.net 圖標提取以及圖標轉(zhuǎn)換的實例代碼
asp.net 圖標提取以及圖標轉(zhuǎn)換的實例代碼,需要的朋友可以參考一下2013-06-06asp.net基于Web Service實現(xiàn)遠程上傳圖片的方法
這篇文章主要介紹了asp.net基于Web Service實現(xiàn)遠程上傳圖片的方法,涉及asp.net調(diào)用Web Service的文件流操作與文件傳輸實現(xiàn)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-12-12解決在Web.config或App.config中添加自定義配置的方法詳解
本篇文章是對在Web.config或App.config中添加自定義配置的方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05Asp.net獲取服務(wù)器指定文件夾目錄文件并提供下載的方法
這篇文章主要介紹了Asp.net獲取服務(wù)器指定文件夾目錄文件并提供下載的方法,涉及使用http協(xié)議操作文件的技巧,需要的朋友可以參考下2015-01-01發(fā)布asp.net core時如何修改ASPNETCORE_ENVIRONMENT環(huán)境變量
這篇文章主要介紹了發(fā)布asp.net core時如何修改ASPNETCORE_ENVIRONMENT環(huán)境變量,幫助大家更好的理解和學習使用.net技術(shù),感興趣的朋友可以了解下2021-04-04