Johnny的专栏

技术Blog-欢迎大家光临查看我收集的技术文档

原创 在Asp.Net中从sqlserver检索(retrieve)图片收藏

出处:www.ASPAlliance.com

介绍:
这篇文章是我写的"如何把图片存入sqlServer中"的后续。我建议你在读这篇文章之前先看看那篇。
和存储图片相比,读取图片就要简单多了。输出一副图片我们要做的就是使用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>


发表于 @ 2004年09月01日 17:30:00|评论(loading...)

新一篇: 关于ASP.Net中的时间处理 | 旧一篇: 如何在ASP.Net 中把图片存入数据库

用户操作
[即时聊天] [发私信] [加为好友]
Johnny
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
Johnny的公告
      我喜欢
驾驭着代码在风驰电掣中创造完美
      我喜欢
操纵着代码在随心所欲中体验生活;
      我喜欢
用心情代码编制我小小的与众不同!
      每一段新的代码在我手中延生对我来说就象观看刹那花开的感动!

我不需要焦点.因为我就是焦点!

--------------------------------

--------------------------------
留言请点击->留言薄
Google搜索 --------------------------------
文章分类
收藏
    个人Blog收藏
    Eric's Blogy
    Java3D
    JAVA夜未眠
    Mobile.aawolf
    zjcxc(邹建)的专栏
    孟子E章
    笨猫.NET
    老猫的理想
    蝈蝈俊.net
    笑话
    笑一笑,十年少
    存档
    软件项目交易
    Csdn Blog version 3.1a
    Copyright © Johnny