从SQL Server数据库提取图片并显示在DataGrid


下面的代码实现从SQL Server数据库提取图片并显示在DataGrid的功能。

下面就是完整的代码,拷贝即可运行:

<%@ 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>

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>

C# 版本

DataGridShowImage.ASPx

<%@ Page language="c#" debug="true" Codebehind="DataGridShowImage.ASPx.cs" AutoEventWireup="false"
Inherits="eMeng.Exam.DataGridShowImage.DataGridShowImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>从数据库中取得照片并显示在DataGrid中</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="javascript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="DataGridShowImage" method="post" runat="server">
<h3 align="center">从数据库中取得照片并显示在DataGrid中</h3>
<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&qu

本文作者:



相关阅读:
CSS入门基础教程:CSS盒子模式(Box Model)
asp中一段防SQL注入的通用脚本
ASP.NET 2.0控件处理DB2 9应用程序之二
关于Firefox中的Flash不可以显示透明效果的问题
Web流程图绘制的心得
MySQL 不允许从远程访问的解决方法
微软开始降低Windows XP的服务标准
CSS滤镜参考
asp.net 用户控件中图片及样式问题
linux下的yum命令详解
SSH远程会话管理工具 screen使用教程
javascript css在IE和Firefox中区别分析
让linux用户隶属于多个组
指定Linux安装软件时所需要安装到目录
快速导航

Copyright © 2016 phpStudy | 皖ICP备18014864号-4