當前位置:首頁 » 操作系統 » asp資料庫圖片讀取

asp資料庫圖片讀取

發布時間: 2025-03-31 08:54:40

A. asp+access資料庫,如何存儲和讀取圖片

在ASP+ACCESS資料庫中存儲和讀取圖片,主要步驟如下:首先,將圖片上傳至伺服器,上傳組件通常為upload組件,例如upload_c(附件中提供)。

接著,在資料庫中為圖片創建相應的存儲記錄。存儲時,只需將圖片的完整URL地址寫入資料庫,這一步是關鍵,因為之後讀取圖片將基於此地址進行。

讀取圖片時,只需通過資料庫查詢獲取圖片的URL地址,然後使用此地址在網頁中直接顯示圖片。這種方式簡便且高效,因為無需額外的伺服器處理步驟。

確保在存儲圖片URL時使用相對路徑,以適應不同的伺服器部署環境。同時,考慮到安全因素,避免直接存儲圖片文件的物理路徑,以免暴露伺服器結構。

在實際應用中,需關註上傳圖片的大小限制,避免伺服器資源過度消耗。另外,為了提升用戶體驗,可以考慮在圖片過大時,使用伺服器端進行壓縮處理,確保圖片載入速度。

總結而言,存儲和讀取圖片在ASP+ACCESS資料庫中的實現,主要依賴於資料庫中的URL地址記錄和網頁端的圖片顯示。通過合理配置和優化,可以有效提升應用的穩定性和用戶體驗。

B. 高分!ASP.NET中如何把圖片以二進制方式存入sql Server資料庫,並能讀取出來

1、建所需資料庫和表,語句如下:

--建立資料庫
create database test

--使用該資料庫
use test

--建立存放圖片的表
create table piclist(
id int Identity primary key,
pic Image not null
)

2、製作上傳圖片的模塊,代碼如下:
前台html代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UpPhoto.aspx.cs" Inherits="Test_UpPhoto" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="UpPhoto" name="UpPhoto" runat="server" type="file" />
<asp:Button id="btnAdd" runat="server" Text="上傳" OnClick="btnAdd_Click"></asp:Button>
</div>
</form>
</body>
</html>

後台代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;

public partial class Test_UpPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void btnAdd_Click(object sender, EventArgs e)
{
//獲得圖象並把圖象轉換為byte[]
HttpPostedFile upPhoto = UpPhoto.PostedFile;
int upPhotoLength = upPhoto.ContentLength;
byte[] PhotoArray = new Byte[upPhotoLength];
Stream PhotoStream = upPhoto.InputStream;
PhotoStream.Read(PhotoArray, 0, upPhotoLength);

//連接資料庫
string ConStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection conn = new SqlConnection(ConStr);

string strSql = "Insert into piclist(pic) values(@pic)";
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.Parameters.Add("@pic", SqlDbType.Image);
cmd.Parameters["@pic"].Value = PhotoArray;

conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Write("圖片上傳成功");
}
}

3、製作顯示圖片的模塊(單獨顯示圖片,即沒用到datalist):
後台代碼:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Test_ShowPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//連接資料庫
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();

SqlCommand cmd=new SqlCommand(strSql,conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}
}

補充步驟3,用datalist顯示圖片方法:
建立兩個asp.net 頁面,名稱為piclist.aspx和StreamImg.aspx。
piclist.aspx前台代碼為:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="piclist.aspx.cs" Inherits="Test_Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlContent" runat="server" Width="554px">
<ItemTemplate>
<table cellpadding="0" cellspacing="0">
<tr>
<td style="width: 554px; text-align: left; background-image: url(Image/標頭.jpg); height: 26px;">
<img id='img1' src='StreamImg.aspx?id= <%# DataBinder.Eval(Container.DataItem,"id") %>'>
</a>
</a>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
</form>
</body>
</html>

piclist.aspx後台代碼為:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class Test_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//連接資料庫
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
SqlConnection sqlcon = new SqlConnection(ConnStr);
sqlcon.Open();
string sqlstr = "select id from piclist";
SqlDataAdapter MyAdapter = new SqlDataAdapter(sqlstr, sqlcon);
DataSet ds = new DataSet();
MyAdapter.Fill(ds, "tb_pic");
this.dlContent.DataSource = ds;
this.dlContent.DataBind();
sqlcon.Close();
}
}
}

StreamImg.aspx無前台代碼,後台代碼為:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;

public partial class StreamImg : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//string type = Request.QueryString["pt"];
int id = Convert.ToInt32(Request.QueryString["id"]);
ShowPic(id);
}
private void ShowPic(int id)
{
//連接資料庫
string ConnStr = "server=(local);user id=sa;pwd=sa;database=test";
string strSql = "select * from piclist where id='"+ id +"'";
SqlConnection conn = new SqlConnection(ConnStr);
conn.Open();

SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Response.ContentType = "application/octet-stream";
Response.BinaryWrite((Byte[])reader["pic"]);
Response.Write("successful");
}
reader.Close();
conn.Close();
Response.End();
}
}

熱點內容
然後弄編程 發布:2025-04-02 02:54:06 瀏覽:103
解壓室俱樂部 發布:2025-04-02 02:47:04 瀏覽:272
安卓哪裡下載文豪野犬 發布:2025-04-02 02:45:04 瀏覽:783
優酷安卓怎麼免廣告 發布:2025-04-02 02:30:07 瀏覽:827
安卓系統怎麼把繁體字改為簡體字 發布:2025-04-02 02:14:39 瀏覽:317
androidpos機 發布:2025-04-02 01:40:54 瀏覽:368
電腦上建立ftp伺服器 發布:2025-04-02 01:26:59 瀏覽:721
wingftp破解 發布:2025-04-02 01:01:28 瀏覽:113
鄭州高檔小區配置是什麼樣的 發布:2025-04-02 01:00:08 瀏覽:449
根伺服器按什麼比例分配的 發布:2025-04-02 00:55:52 瀏覽:619