新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 中文XML论坛 - 专业的XML技术讨论区计算机技术与应用『 Dot NET,C#,ASP,VB 』 → Displaying Images from SQL Server database in ASP. 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 2610 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: Displaying Images from SQL Server database in ASP. 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18407
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 Displaying Images from SQL Server database in ASP.


    发信人: Nineteen (-_-#!), 信区: DotNET
    标  题: Displaying Images from SQL Server database in ASP.NET DataGrid
    发信站: BBS 水木清华站 (Fri Feb 20 16:38:55 2004), 转信

    http://www.dotnetbips.com/displayarticle.aspx?id=101
    Introduction
    ASP.NET DataGrid web control can be bound quickly with any database table like SQL server. For most of the data types all you need to do is to add a bound column and set its datafield property to the column name from the table. However, displaying image data type i.e. binary data is not that easy. It requires some kind of extra coding from developer's side to make that work. In this article we will see how to do just that.  
    SQL Server tables
    The articles make use of a SQL server database table called images. The structure of which can be as follows:  
    CREATE TABLE [dbo].[IMAGES] (
            [imgid] [int] IDENTITY (1, 1) NOT NULL ,
            [imgdata] [image] NULL
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

    Creating a web form with DataGrid
    In order to proceed with the example create a new ASP.NET web form say webform1.aspx and add a DataGrid web control to it. Add two columns to it :  
    one bound column with datafield set to imgid and  
    another templated column with an image web control.  
    In the Page_Load event of the web form bind the datagrid with the database.  
    If Not Page.IsPostBack Then
            BindGrid()
    End If

    Public Sub BindGrid()
            Dim connstr As String =
            "Integrated Security=SSPI;
            Initial Catalog=Northwind;
            Data Source=SERVER\netsdk"
            Dim cnn As New SqlConnection(connstr)
            Dim da As New SqlDataAdapter("select * from images", cnn)
            Dim ds As New DataSet()
            da.Fill(ds, "images")
            DataGrid1.DataSource = ds
            DataGrid1.DataBind()
    End Sub

    Now write the ItemDataBound event for the datagrid as follows:  
    Private Sub DataGrid1_ItemDataBound
    (ByVal sender As Object,
    ByVal e As DataGridItemEventArgs)
    Handles DataGrid1.ItemDataBound

            Dim img As System.Web.UI.WebControls.Image
            If e.Item.ItemType = ListItemType.AlternatingItem Or
               e.Item.ItemType = ListItemType.Item Then
                    img = CType(e.Item.Cells(1).Controls(1),
                              System.Web.UI.WebControls.Image)
                    img.ImageUrl =  
                    "webform2.aspx?id=" & e.Item.Cells(0).Text
            End If
    End Sub

    This event gets fired for all the rows from the DataGrid just before data binding action. All the code from the event handler should be familiar to you except the line where we are setting ImageUrl property of the image web control. Generally this property is set to a valid image file (gif/jpg etc.) but here, we are setting it to another web form. What does that means? This means that the web form (webform2.aspx) is returning the image data based on some processing logic. That is why we are also passing i
    mage id to the web form via query string. Now, let us examine the code from the webform2.aspx.  
    Creating web form that returns image data
    The webform2.aspx simply contains following code in the Page_Load event handler.  
    Dim connstr As String = "Integrated Security=SSPI;
    Initial Catalog=Northwind;Data Source=SERVER\netsdk"
    Dim cnn As New SqlConnection(connstr)
    Dim cmd As New SqlCommand
    ("select * from images where id="  
    & Request.QueryString("id"), cnn)
    cnn.Open()
    Dim dr As SqlDataReader = cmd.ExecuteReader()
    dr.Read()
    Dim bindata() As Byte = dr.GetValue(1)
    Response.BinaryWrite(bindata)

    Here, we are retrieving the image based on the query string passed. Note how we have used Response.BinaryWrite to emit the binary image data. Now, run the application and you should get all the images displayed in columns of the DataGrid.  
    Summary
    In this article we saw how to retrieve image data type from SQL server database and display the image in DataGrid web control. We saw here that ImageUrl of the image web control can be set to another web form that emits binary image data via Response.BinaryWrite method.  
    About the author
    Name :
    Bipin Joshi
      
    Email :
    webmaster at dotnetbips.com
      
    Profile :
    Bipin Joshi is the founder of DotNetBips.com. He runs a company called BinaryIntellect Consulting (www.binaryintellect.com) that provides software development, consulting and training services. He is also Microsoft MVP for .NET.  
      

    --
    ——长夜漫漫,无心睡眠……难道狼妹妹也睡不着吗?


    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 202.206.1.*]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




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

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

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:26:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/21 8:51:11

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms