显示效果如下

SID 图片显示
180
181
182
183
184
1


图片显示页面Html:DataGridShowPicture.aspx
<%@ Page language="c#" Codebehind="DataGridShowPicture.aspx.cs" AutoEventWireup="false" Inherits="_0921test.DataGridShowPicture" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>DataGridShowPicture</title>
        
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        
<meta content="C#" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<form id="DataGridShowImage" method="post" runat="server">
            
<table style="Z-INDEX: 101; LEFT: 400px; POSITION: absolute; TOP: 72px" align="center">
                
<tr>
                    
<td align="center">
                        
<asp:datagrid id="dgShowPicture" HeaderStyle-HorizontalAlign="Center" Runat="server" BorderColor="#3366CC"
                            ItemStyle-BackColor
="Beige" HeaderStyle-ForeColor="#ffffff" HeaderStyle-Font-Bold="True" HeaderStyle-BackColor="#ff0000"
                            Width
="160px" AutoGenerateColumns="False" BorderStyle="None" BorderWidth="1px" BackColor="White"
                            CellPadding
="4" PageSize="5" AllowPaging="True" AllowCustomPaging="True">
                            
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
                            
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
                            
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
                            
<HeaderStyle Font-Bold="True" HorizontalAlign="Center" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
                            
<Columns>
                                
<asp:TemplateColumn HeaderText="SID">
                                    
<ItemTemplate>
                                        
<asp:Label Runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "PID") %>' ID="Label1"/>
                                    
</ItemTemplate>
                                
</asp:TemplateColumn>
                                
<asp:TemplateColumn HeaderText="图片显示">
                                    
<ItemTemplate>
                                        
<asp:Image Runat=server ID="Image1" Height =50 Width=50 ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "Mulname")) %>'/>
                                    
</ItemTemplate>
                                
</asp:TemplateColumn>
                            
</Columns>
                            
<PagerStyle VerticalAlign="Middle" Font-Bold="True" HorizontalAlign="Center" ForeColor="#003399"
                                BackColor
="#99CCCC" Mode="NumericPages"></PagerStyle>
                        
</asp:datagrid>
                    
</td>
                
</tr>
            
</table>
        
</form>
    
</body>
</HTML>



图片显示页面CS:DataGridShowPicture.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace _0921test
{
    
/// <summary>
    
/// DataGridShowPicture 的摘要说明。
    
/// </summary>

    public class DataGridShowPicture : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.DataGrid dgShowPicture;
        

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            if(!this.IsPostBack)
            
{
                BindGrid();
            }

        }


        
Web 窗体设计器生成的代码


        
private void BindGrid()
        

            
string connectionString = "workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=nature";
            
            
string sqlstr = "SELECT PID,Mulname FROM Picture where PID like '%18%'";            
            
try
            
{
                
using (SqlConnection connection = new SqlConnection(connectionString))
                
{
                    DataSet ds 
= new DataSet();
                    
try
                    
{
                        connection.Open();                    
                        SqlDataAdapter Dta
= new SqlDataAdapter(sqlstr,connection);
                        Dta.Fill(ds,
"ds");
                        Dta.Dispose();
                        dgShowPicture.DataSource 
= ds;
                        
this.dgShowPicture.VirtualItemCount=5;
                        dgShowPicture.DataBind();
                    }

                    
catch(System.Data.SqlClient.SqlException ex)
                    
{                
                        
throw new Exception(ex.Message);
                    }
            
                }

            }

            
catch(SqlException SQLexc)
            
{
                Response.Write(
"提取数据时出现错误:" + SQLexc.ToString()); 
            }
 
        }


        
protected string FormatURL(object strArgument) 
        

            
return "DataGridReadImage.aspx?PID=" + strArgument.ToString(); 
        }




    }

        
}



图片读取页面HTML: DataGridReadImage.aspx
<%@ Page language="c#" Codebehind="DataGridReadImage.aspx.cs" AutoEventWireup="false" Inherits="_0921test.DataGridReadImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    
<HEAD>
        
<title>DataGridReadImage</title>
        
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        
<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="Form1" method="post" runat="server">
            
<FONT face="宋体"></FONT>
        
</form>
    
</body>
</HTML>


图片读取页面CS: DataGridReadImage.aspx

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

namespace _0921test
{
    
/// <summary>
    
/// DataGridReadImage 的摘要说明。
    
/// </summary>

    public class DataGridReadImage : System.Web.UI.Page
    
{
        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
             string strImageID = Request.QueryString["PID"];
            SqlConnection myConnection 
= new SqlConnection("workstation id=WANGSZ;packet size=4096;integrated security=SSPI;data source=WANGSZ;persist security info=False;initial catalog=nature");
            
//SqlCommand myCommand = new SqlCommand("Select PersonImageType, PersonImage from Person Where PersonID=" + strImageID, myConnection);
            try
            
{
//                myConnection.Open();
//                SqlDataReader myDataReader;
//                myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
//                if(myDataReader.Read())
//                { 
//                    Response.Clear();
//                    Response.ContentType = myDataReader["PersonImageType"].ToString();
//                    Response.BinaryWrite((byte[])myDataReader["PersonImage"]); 
//                } 
//                myConnection.Close(); 

                FileInfo  file 
= new FileInfo(strImageID);
            
                
if(file.Exists)
                
{
                    
using (FileStream fs = file.OpenRead()) 
                    
{
                        
byte[] buffer = new byte[12048];
                        
int read;

                        
while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
                        
{
                            Response.BinaryWrite(buffer);
                        }

                    }

                }

            }
 
            
catch(SqlException SQLexc)
            
{ }
            Response.End(); 
        }


        
Web 窗体设计器生成的代码
    }

}


posted on 2006-12-20 16:37  Dragon-China  阅读(672)  评论(0编辑  收藏  举报