欢迎光临
我们一直在努力

在ASP.NET中从SQL Server检索图片-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

和存储图片相比,读取图片就要简单多了。输出一副图片我们要做的就是使用response对象的binarywrite方法。

  同时设置图片的格式。在这篇文章中,我们将讨论如何从sqlserver中检索图片。并将学习以下几个方面的知识。

  ·如何设置图片的格式?

  ·如何使用binarywrite方法。

  我们已经在person表中存储了数据,那么我们就写些代码来从表中读取数据。

  下面的代码检索了所有的值从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

  看看他是怎么工作的?

  上面的例子很简单。我们所作的就是执行一个sql语句,再循环读取所有的记录(looping through all the records).

  在显示图片之前,我们先设置了图片的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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在ASP.NET中从SQL Server检索图片-.NET教程,Asp.Net开发
分享到: 更多 (0)