1、显示数据库中的图片;
2、利用global.asax的Session_Start来记录登陆者信息;
global.asax.cs 代码:
protected void Session_Start(Object sender, EventArgs e)
{
SqlConnection connection =new SqlConnection("user id=sa;data source=localhost;persist security info=True;initial catalog=NetManager;password=xiayc");
/*SQLCom,Str_IpAddr,Str_Brow,Str_Option*/
string strBrow,strIpAddr,strOption;
strIpAddr=Request.UserHostAddress ;
if (Request.UserAgent.IndexOf("MSIE")> -1 )
{
strBrow="Internet Explore"+Request.Browser.Version ;
}
else
{
strBrow=Request.Browser.Browser + Request.Browser.Version;
}
if (Request.UserAgent.IndexOf("Windows NT 5.2")> -1)
{
strOption = "Windows 2003 ";
}
else
{
if (Request.UserAgent.IndexOf("Windows NT 5.1")> -1)
{
strOption = "Windows XP ";
}
else
{
if (Request.UserAgent.IndexOf("Windows NT 5.0")> -1)
{
strOption ="Windows 2000 ";
}
else
{
strOption = Request.Browser.Platform;
}

}
}
DateTime datetime=new DateTime() ;
datetime =DateTime.UtcNow;
datetime = DateTime.Now;
string s =datetime.ToString ("d");
datetime = DateTime.Parse(s);


}webform1.aspx.cs代码
25-38行为添加代码
1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Data.SqlClient;
12
13
namespace WebApplication1
14
{
15
/// <summary>
16
/// WebForm1 的摘要说明。
17
/// </summary>
18
public class WebForm1 : System.Web.UI.Page
19
{
20
protected System.Web.UI.WebControls.DataGrid DataGrid1;
21
22
private void Page_Load(object sender, System.EventArgs e)
23
{
24
// 在此处放置用户代码以初始化页面
25
if (!IsPostBack){
26
SqlConnection connection = new SqlConnection("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");
27
try
28
{
29
connection.Open();
30
SqlCommand command = new SqlCommand("select EmployeeID,LastName,FirstName,Title FROM Employees",connection);
31
SqlDataReader reader = command.ExecuteReader();
32
DataGrid1.DataSource =reader;
33
DataGrid1.DataBind ();
34
}
35
finally{
36
connection.Close ();
37
}
38
}
39
40
}
41
42
#region Web 窗体设计器生成的代码
43
override protected void OnInit(EventArgs e)
44
{
45
//
46
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
47
//
48
InitializeComponent();
49
base.OnInit(e);
50
}
51
52
/// <summary>
53
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
54
/// 此方法的内容。
55
/// </summary>
56
private void InitializeComponent()
57
{
58
this.Load += new System.EventHandler(this.Page_Load);
59
60
}
61
#endregion
62
63
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
64
{
65
66
}
67
}
68
}
69
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11
using System.Data.SqlClient;12

13
namespace WebApplication114
{15
/// <summary>16
/// WebForm1 的摘要说明。17
/// </summary>18
public class WebForm1 : System.Web.UI.Page19
{20
protected System.Web.UI.WebControls.DataGrid DataGrid1;21
22
private void Page_Load(object sender, System.EventArgs e)23
{24
// 在此处放置用户代码以初始化页面25
if (!IsPostBack){26
SqlConnection connection = new SqlConnection("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");27
try 28
{29
connection.Open();30
SqlCommand command = new SqlCommand("select EmployeeID,LastName,FirstName,Title FROM Employees",connection);31
SqlDataReader reader = command.ExecuteReader();32
DataGrid1.DataSource =reader;33
DataGrid1.DataBind ();34
}35
finally{36
connection.Close ();37
}38
}39

40
}41

42
#region Web 窗体设计器生成的代码43
override protected void OnInit(EventArgs e)44
{45
//46
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。47
//48
InitializeComponent();49
base.OnInit(e);50
}51
52
/// <summary>53
/// 设计器支持所需的方法 - 不要使用代码编辑器修改54
/// 此方法的内容。55
/// </summary>56
private void InitializeComponent()57
{ 58
this.Load += new System.EventHandler(this.Page_Load);59

60
}61
#endregion62

63
private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)64
{65
66
}67
}68
}69

webform1.aspx html代码:
1
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>
2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
3
<HTML>
4
<HEAD>
5
<title>WebForm1</title>
6
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>
7
<meta content=C# name=CODE_LANGUAGE>
8
<meta content=JavaScript name=vs_defaultClientScript>
9
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>
10
</HEAD>
11
<body MS_POSITIONING="GridLayout">
12
<form id=Form1 method=post runat="server"><FONT face=宋体><asp:datagrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server" Width="328px" Height="192px" AutoGenerateColumns="False">
13
<Columns>
14
<asp:TemplateColumn HeaderText="照片">
15
<ItemTemplate>
16
<img src='<%# "ImageGrabber.ashx?id=" + DataBinder.Eval(Container.DataItem,"EmployeeID") %>' border="0">
17
</ItemTemplate>
18
</asp:TemplateColumn>
19
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>
20
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>
21
</Columns>
22
</asp:datagrid></FONT></form>
23
</body>
24
</HTML>
25
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm1" %>2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >3
<HTML>4
<HEAD>5
<title>WebForm1</title>6
<meta content="Microsoft Visual Studio .NET 7.1" name=GENERATOR>7
<meta content=C# name=CODE_LANGUAGE>8
<meta content=JavaScript name=vs_defaultClientScript>9
<meta content=http://schemas.microsoft.com/intellisense/ie5 name=vs_targetSchema>10
</HEAD>11
<body MS_POSITIONING="GridLayout">12
<form id=Form1 method=post runat="server"><FONT face=宋体><asp:datagrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute; TOP: 48px" runat="server" Width="328px" Height="192px" AutoGenerateColumns="False">13
<Columns>14
<asp:TemplateColumn HeaderText="照片">15
<ItemTemplate>16
<img src='<%# "ImageGrabber.ashx?id=" + DataBinder.Eval(Container.DataItem,"EmployeeID") %>' border="0">17
</ItemTemplate>18
</asp:TemplateColumn>19
<asp:BoundColumn DataField="LastName" HeaderText="Last Name"></asp:BoundColumn>20
<asp:BoundColumn DataField="FirstName" HeaderText="First Name"></asp:BoundColumn>21
</Columns>22
</asp:datagrid></FONT></form>23
</body>24
</HTML>25

注意 14行-17行 为DataGrid的模板行,该模板行输出一个典型的<img>标签
<img src='ImageGrabber.ashx?id=1'>
ImageGrabber.ashx是一个HTTP处理句柄。当被调用时,它ProcessRequest方法从查询字符串中截获雇员ID,
并使用它执行自己的数据库查询。这次查询获取相应记录的Photo域,利用它创建一个位图,并把位图流作为JPEG传递给客户端。
ImageGrabber.ashx代码:
仅一行:
<%@ WebHandler language="c#" Class="WebApplication1.ImageGrabber" Codebehind="ImageGrabber.ashx.cs" %>
ImageGrabber.ashx.cs代码:
1
using System;
2
using System.IO;
3
using System.Web;
4
using System.Drawing;
5
using System.Drawing.Imaging ;
6
using System.Data.SqlClient;
7
8
namespace WebApplication1
9
{
10
/// <summary>
11
/// ImageGrabber 的摘要说明。
12
/// </summary>
13
public class ImageGrabber:IHttpHandler
14
{
15
public void ProcessRequest(HttpContext context)
16
{
17
string id = (string) context.Request["id"];
18
if (id !=null)
19
{
20
MemoryStream stream =new MemoryStream() ;
21
SqlConnection connection = new SqlConnection ("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");
22
Bitmap bitmap=null;
23
Image image=null;
24
25
try
26
{
27
connection.Open();
28
SqlCommand command =new SqlCommand ("SELECT Photo from Employees WHERE EmployeeID = '" + id +"'" ,connection);
29
30
byte[] blob =(byte[]) command.ExecuteScalar();
31
stream.Write (blob,78,blob.Length-78);
32
bitmap=new Bitmap(stream);
33
34
int width =48;
35
int height = (int)(width*((double) bitmap.Height /(double) bitmap.Width ));
36
image =bitmap.GetThumbnailImage(width,height,null,IntPtr.Zero );
37
38
39
context.Response.ContentType="image/jpeg";
40
image.Save (context.Response.OutputStream,ImageFormat.Jpeg );
41
42
43
}
44
finally
45
{
46
if (image !=null)
47
image.Dispose();
48
if (bitmap !=null)
49
bitmap.Dispose();
50
stream.Close();
51
connection.Close();
52
53
}
54
}
55
}
56
57
public bool IsReusable
58
{
59
get {return true;}
60
}
61
}
62
}
63
using System;2
using System.IO;3
using System.Web;4
using System.Drawing;5
using System.Drawing.Imaging ;6
using System.Data.SqlClient;7

8
namespace WebApplication19
{10
/// <summary>11
/// ImageGrabber 的摘要说明。12
/// </summary>13
public class ImageGrabber:IHttpHandler14
{15
public void ProcessRequest(HttpContext context)16
{17
string id = (string) context.Request["id"];18
if (id !=null)19
{20
MemoryStream stream =new MemoryStream() ;21
SqlConnection connection = new SqlConnection ("user id=sa;data source=localhost;persist security info=True;initial catalog=Northwind;password=xiayc");22
Bitmap bitmap=null;23
Image image=null;24

25
try26
{27
connection.Open();28
SqlCommand command =new SqlCommand ("SELECT Photo from Employees WHERE EmployeeID = '" + id +"'" ,connection);29
30
byte[] blob =(byte[]) command.ExecuteScalar();31
stream.Write (blob,78,blob.Length-78);32
bitmap=new Bitmap(stream);33

34
int width =48;35
int height = (int)(width*((double) bitmap.Height /(double) bitmap.Width ));36
image =bitmap.GetThumbnailImage(width,height,null,IntPtr.Zero );37

38

39
context.Response.ContentType="image/jpeg";40
image.Save (context.Response.OutputStream,ImageFormat.Jpeg );41

42
43
}44
finally45
{46
if (image !=null)47
image.Dispose();48
if (bitmap !=null)49
bitmap.Dispose();50
stream.Close();51
connection.Close();52

53
}54
}55
}56

57
public bool IsReusable58
{59
get {return true;}60
}61
}62
}63


本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号