asp.net實(shí)現(xiàn)存儲和讀取數(shù)據(jù)庫圖片
本文實(shí)例為大家分享了asp.net存儲和讀取數(shù)據(jù)庫圖片的具體代碼,供大家參考,具體內(nèi)容如下
1. 創(chuàng)建asp.net web窗體項(xiàng)目
代碼如下:
<body>
<form id="form1" runat="server">
<h2>上傳圖片</h2>
<div>
<asp:FileUpload ID="ful_image" runat="server" />
<asp:Button ID="btn_submit" runat="server" Text="提交" />
<asp:Label ID="lbl_message" runat="server" Text="Label"></asp:Label>
</div>
<h2>展示圖片</h2>
<div>
<asp:Button ID="btn_show" runat="server" Text="展示圖片" />
<asp:Panel ID="pn_images" runat="server"></asp:Panel>
</div>
</form>
</body>
效果圖如下:

2. 創(chuàng)建數(shù)據(jù)庫
數(shù)據(jù)庫腳本如下:
create database Imagetest go use Imagetest CREATE TABLE [dbo].[imageTable]( [ID] [int] IDENTITY(1,1) NOT NULL, [imageData] [image] NULL, CONSTRAINT [PK_imageTable] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
數(shù)據(jù)庫生成結(jié)果圖:

3.使用EF實(shí)現(xiàn)圖片存儲
實(shí)現(xiàn)代碼:
protected void btn_submit_Click(object sender, EventArgs e)
{
ImagetestEntities ie = new ImagetestEntities();
imageTable it = new imageTable();
it.imageData = ful_image.FileBytes;
it= ie.imageTables.Add(it);
ie.SaveChanges();
if(it.ID!=0)
{
lbl_message.Text = "圖片上傳成功";
}
else
{
lbl_message.Text = "圖片上傳失敗";
}
}
運(yùn)行結(jié)果:

4. 使用EF實(shí)現(xiàn)圖片的展示
實(shí)現(xiàn)代碼:
protected void btn_show_Click(object sender, EventArgs e)
{
ImagetestEntities ie = new ImagetestEntities();
List<imageTable> list= ie.imageTables.ToList<imageTable>();
foreach (imageTable item in list)
{
Image img = new Image();
img.ImageUrl ="data:image/png;base64,"+ Convert.ToBase64String(item.imageData);
pn_images.Controls.Add(img);
}
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Asp.Net Core中創(chuàng)建多DbContext并遷移到數(shù)據(jù)庫的步驟
- 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle數(shù)據(jù)庫
- 淺談如何使用vb.net從數(shù)據(jù)庫中提取數(shù)據(jù)
- .NET Core Dapper操作mysql數(shù)據(jù)庫的實(shí)現(xiàn)方法
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫存儲過程列表及參數(shù)信息示例
- ASP.NET Core2讀寫InfluxDB時(shí)序數(shù)據(jù)庫的方法教程
- ASP.NET WebAPI連接數(shù)據(jù)庫的方法
- .net core利用orm如何操作mysql數(shù)據(jù)庫詳解
- .net core下配置訪問數(shù)據(jù)庫操作
- .net數(shù)據(jù)庫操作框架SqlSugar的簡單入門
相關(guān)文章
MVC4 基礎(chǔ) 枚舉生成 DropDownList 實(shí)用技巧
本篇文章小編為大家介紹,MVC4 基礎(chǔ) 枚舉生成 DropDownList 實(shí)用技巧。需要的朋友參考下2013-04-04
asp.net使用jQuery獲取RadioButtonList成員選中內(nèi)容和值示例
這篇文章主要介紹了通過jQuery來獲取RadioButtonList成員內(nèi)容的方法,大家參考使用吧2014-01-01
C#反射實(shí)例學(xué)習(xí)及注意內(nèi)容
C#反射的入門學(xué)習(xí)首先要明白C#反射提供了封裝程序集、模塊和類型的對象等等需要的朋友可以參考下2012-12-12
使用JavaScript代碼實(shí)現(xiàn)各種數(shù)據(jù)控件的反選功能 不要只做拖控件的菜鳥
在我們做許多項(xiàng)目的時(shí)候,會用到反選這個(gè)功能,但是我一般使用C#代碼創(chuàng)建數(shù)組遍歷實(shí)現(xiàn)功能,今天我想換一種語言實(shí)現(xiàn)一下,于是我就用JavaScript研究了一下怎么實(shí)現(xiàn)這個(gè)功能2011-12-12
數(shù)據(jù)綁定之DataFormatString使用介紹
DataFormatString是很多Asp.Net控件都有的屬性,如GridView等等,下面簡單介紹一下這個(gè)屬性,感興趣的朋友不要錯過2013-10-10

