zoukankan      html  css  js  c++  java
  • 从SQL Server数据库提取图片并显示在DataGrid

    <!-- ReadImage.aspx -->
    <%@ Page Language="vb" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <HTML>
    <HEAD>
    <script runat=server>
    Public Sub Page_Load(sender As Object, e As EventArgs)
      Dim strImageID as String 
    = Request.QueryString("id")
      
    Dim myConnection As New SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;")
      
    Dim myCommand As New SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" _
        + strImageID, 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()
        Catch SQLexc As SqlException
      End Try
    End Sub    
    </script
    >
    </HEAD>
    <body>
    <form runat="server" ID="Form1"></form>
    </body>
    </HTML>

    <!-- DataGridShowImage.aspx -->
    <%@ Page Language="vb" %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>
    <html>
    <head>
    <title>用户列表</title>
    <script runat=server>
    Sub Page_Load(sender As Object, e As EventArgs)
      If Not Page.IsPostBack Then
      BindGrid()
      End If
    End Sub

    Private Sub BindGrid()
      Dim strCnn As String 
    = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;"
      Dim myConnection As SqlConnection 
    = New SqlConnection(strCnn)
      Dim myCommand As SqlCommand 
    = New SqlCommand("SELECT * FROM Person", myConnection)
      myCommand.CommandType 
    = CommandType.Text
      
    Try
        myConnection.Open()
        DG_Persons.DataSource 
    = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
        
    DG_Persons.DataBind()
        Catch SQLexc As SqlException
        Response.Write("Error occured while Generating Data. Error is " & SQLexc.ToString())
      End Try
    End Sub

    Function FormatURL(strArgument) as String
      Return ("ReadImage.aspx?id
    =" & strArgument)
    End Function
    </script>
    </head>
    <body style="
    font: 9pt 宋体">
    <h3 align=center>从数据库中取得照片并显示在DataGrid中</h3>
    <form id="Form1" method="post" runat="server">
    <asp:DataGrid ID="DG_Persons" AutoGenerateColumns=False Width="99%"
     HeaderStyle
    -BackColor="#ff0000" HeaderStyle-Font-Bold="True" HeaderStyle-ForeColor="#ffffff"
     ItemStyle
    -BackColor=Beige BorderColor="#000000" Runat=server HeaderStyle-HorizontalAlign=Center>
    <Columns>
    <asp:TemplateColumn HeaderText="姓名">
    <ItemTemplate>
    <asp:Label Runat="server"  Text='<%# DataBinder.Eval(Container.DataItem, "PersonName"%>' ID="Label1"/>
    </ItemTemplate>                               
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="电子邮件">
    <ItemTemplate>
    <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonEmail"%>' ID="Label2"/>
    </ItemTemplate>                               
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="性别">
    <ItemTemplate>
    <asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PersonSex"%>' ID="Label3"/>
    </ItemTemplate>                               
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="出生日期">
    <ItemTemplate>
    <asp:Label Runat="server" 
      Text
    ='<%# DataBinder.Eval(Container.DataItem, "PersonDOB"%>' ID="Label4"/>
    </ItemTemplate>                               
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="照片">
    <ItemTemplate>
    <asp:Image Runat=server ID="Image1"  Width="150" Height="125"
     ImageUrl
    ='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID")) %>'  />
    </ItemTemplate>                               
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    </form>
    </body>
    </html>

  • 相关阅读:
    CloudStack+KVM环境搭建(步骤很详细,说明ClockStack是用来管理虚拟机的)
    CloudStack和OpenStack该如何选择(如果准备选择OpenStack,请做好hack的准备。CloudStack的底层功能已经做的很完善了,更适合商用)
    NancyFx And ReactiveX
    Apache Kafka® is a distributed streaming platform
    C/C++配置
    Win10 专业版 Hyper-V 主机计算服务无法启动
    使用事件和 CQRS 重写 CRUD 系统
    使用Skywalking分布式链路追踪系统
    GraphQL&DSL&API网关
    TomatoLog 是一个基于 .NETCore 平台的产品。
  • 原文地址:https://www.cnblogs.com/acelove/p/86292.html
Copyright © 2011-2022 走看看