从SQL Server数据库提取图片并显示在DataGrid(2)
发表于:2007-06-30来源:作者:点击数:
标签:
DataGridShowImage.aspx.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;
DataGridShowImage.aspx.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 eMeng.Exam.DataGridShowImage
{
/// <summary>
/// DataGridShowImage 的摘要说明。
/// </summary>
public class DataGridShowImage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DG_Persons;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!this.IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
string strCnn = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;";
SqlConnection myConnection = new SqlConnection(strCnn);
SqlCommand myCommand = new SqlCommand("SELECT * FROM Person", myConnection);
myCommand.CommandType = CommandType.Text;
try
{
myConnection.Open();
DG_Persons.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
DG_Persons.DataBind();
}
catch(SqlException
SQLexc)
{
Response.Write("提取数据时出现错误:" + SQLexc.ToString());
}
}
protected string FormatURL(object strArgument)
{
return "ReadImage.aspx?id=" + strArgument.ToString();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
ReadImage.aspx
<%@ Page language="c#" Codebehind="ReadImage.aspx.cs" AutoEventWireup="false"
Inherits="eMeng.Exam.DataGridShowImage.ReadImage" %>
ReadImage.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Data.SqlClient;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace eMeng.Exam.DataGridShowImage
{
/// <summary>
/// ReadImage 的摘要说明。
/// </summary>
public class ReadImage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
string strImageID = Request.QueryString["id"];
SqlConnection myConnection = new SqlConnection("Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;");
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();
}
catch (SqlException SQLexc)
{
}
Response.End();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
原文转自:http://www.ltesting.net