国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

在Asp.Net中從sqlserver檢索(retrieve)圖片

2024-07-10 12:58:04
字體:
供稿:網(wǎng)友
介紹:
這篇文章是我寫的"如何把圖片存入sqlserver中"的后續(xù)。我建議你在讀這篇文章之前先看看那篇。
和存儲(chǔ)圖片相比,讀取圖片就要簡單多了。輸出一副圖片我們要做的就是使用response對(duì)象的binarywrite方法。
同時(shí)設(shè)置圖片的格式。在這篇文章中,我們將討論如何從sqlserver中檢索圖片。
并將學(xué)習(xí)以下幾個(gè)方面的知識(shí).
·如何設(shè)置圖片的格式?
·如何使用binarywrite方法。

我們已經(jīng)在person表中存儲(chǔ)了數(shù)據(jù),那么我們就寫些代碼來從表中讀取數(shù)據(jù)。
下面的代碼檢索了所有的值從person表中。

從sqlserver中讀取圖片的代碼.
public sub page_load(sender as object, e as eventargs)
dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))
dim mycommand as new sqlcommand("select * from person", myconnection)
try
myconnection.open()
dim mydatareader as sqldatareader
mydatareader = mycommand.executereader(commandbehavior.closeconnection)

do while (mydatareader.read())
response.contenttype = mydatareader.item("personimagetype")
response.binarywrite(mydatareader.item("personimage"))
loop

myconnection.close()
response.write("person info successfully retrieved!")
catch sqlexc as sqlexception
response.write("read failed : " & sqlexc.tostring())
end try
end sub

看看他是怎么工作的?
上面的例子很簡單。我們所作的就是執(zhí)行一個(gè)sql語句,再循環(huán)讀取所有的記錄(looping through all the records).
在顯示圖片之前,我們先設(shè)置了圖片的contenttype,然后我們使用binarywrite方法把圖片輸出到瀏覽器。

源代碼:
/// retriving.aspx

<%@ page language="vb" %>
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<html>
<head>
<title>retrieving image from the sql server</title>
<script runat=server>
public sub page_load(sender as object, e as eventargs)
' create instance of connection and command object
dim myconnection as new sqlconnection(configurationsettings.appsettings("connectionstring"))
dim mycommand as new sqlcommand("select * from person", myconnection)
try
myconnection.open()
dim mydatareader as sqldatareader
mydatareader = mycommand.executereader(commandbehavior.closeconnection)


do while (mydatareader.read())
response.contenttype = mydatareader.item("personimagetype")
response.binarywrite(mydatareader.item("personimage"))
loop

myconnection.close()
response.write("person info successfully retrieved!")
catch sqlexc as sqlexception
response.write("read failed : " & sqlexc.tostring())
end try
end sub

</script>
</head>
<body style="font: 10pt verdana">
</body>
</html>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 句容市| 惠东县| 新田县| 巢湖市| 靖边县| 河源市| 都安| 枝江市| 宜兰市| 衡阳市| 安平县| 蓬莱市| 上栗县| 黔江区| 湾仔区| 什邡市| 淮安市| 昆明市| 杭锦后旗| 岚皋县| 密云县| 桂阳县| 灵寿县| 天津市| 肃北| 昌黎县| 鄂伦春自治旗| 措美县| 务川| 德阳市| 东台市| 周宁县| 东港市| 汽车| 舟曲县| 滁州市| 泸州市| 义马市| 涪陵区| 枞阳县| 桓台县|