网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.在.Net 里用XPath的case-insensi.
.httpRuntime 一点经验.
.在ASP.NET Atlas中调用Web Servi.
.轻松解决asp.net用户ASPNET登录失.
.实现FCKeditor 多用户分文件夹上.
.NET环境下水晶报表使用总结.
.IIS7 会重用那些不该重用 HttpHa.
.用代码画折线图/柱形图/条形图.
.Asp.net中多彩下拉框的实现.
.求素数(质数)的算法.
.彻底放弃IIS让Apache也支持ASP.N.
.正则表达式提取数字.
.正则学习:组的定义及引用方式.
.ASP.NET底层架构探索之IE6的革新.
.ASP.NET性能最佳实践 .
.把ip转换成对应的城市名.
.使用asp.net2.0中的SiteMap中的一.
.开发ASP.NET Atlas服务器端Exten.
.ASP.NET与MySQL数据库简明图示入.
.发布一个好用的类SafeConvert, 用.

在ASP.NET中从SQL Server检索图片

发表日期:2004-9-6


  和存储图片相比,读取图片就要简单多了。输出一副图片我们要做的就是使用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> 

上一篇:ASP.NET中将数据输出到Excel 人气:14287
下一篇:ASP.NET系统用户权限设计与实现 人气:18582
浏览全部ASP.NET的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐