读取sql server 数据库图片信息
以下来自,他的内容
摘要
.NET是由微软开发的一种新型的分布式计算平台,ASP.NET是它针对Web开发的编程模式。本文的目的是在开发数据驱动的ASP.NET Web应用程序中获取一些好的经验。这个应用程序将告诉你怎么把一幅图片保存到数据库中以及怎样把图片从数据库中读取出来。它以ADO.NET作为数据访问机制,C#作为编程语言,SQL 2000 Server作为后台数据库。
概述
一般的,很大的图片文件往往被保存在Web服务器的文件夹中,而不是数据库中。在一些实例中,以银行系统为例,人们先把用户的签名做成图片文件,然后保存到数据库中。
- 数据库模式
在这个示范中,微软的SQL 2000 Server被用作后台数据库。我使用了一种比较特殊的数据类型 image 。这 image 数据类型是被用来保存图片到数据库的。
- 所使用的控件:
System.Web.UI.HtmlControls.HtmlInputFileSystem.Web.UI.WebControls.TextBoxSystem.Web.UI.WebControls.Button
using System.Data.SqlClient;
using System.Drawing;
using System.Data;
using System.IO;
using System.Drawing.Imaging;
编码
使用 HtmlInputFile 类,它可以用 <input type="file" runat="server"/> 标签来声明一个实例。下面的例子是一个完整的 ASPX 文件,它让用户上传图片文件以及图片的说明。OnUpload 方法把图片以及说明写到iSense 数据库的Picture 表中

1// 保存图片文件到数据库的源码
2
3public void OnUpload(Object sender, EventArgs e)
4{
5// 从输入文件中创建一个 byte[]
6int len = Upload.PostedFile.ContentLength;
7 byte[] pic = new byte[len];
8 Upload.PostedFile.InputStream.Read (pic, 0, len);
9// 插入图片和说明到数据库中
10 SqlConnection connection = new
11 SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");
12 try
13 {
14 connection.Open ();
15 SqlCommand cmd = new SqlCommand ("insert into Image "
16 + "(Picture, Comment) values (@pic, @text)", connection);
17 cmd.Parameters.Add ("@pic", pic);
18 cmd.Parameters.Add ("@text", Comment.Text);
19 cmd.ExecuteNonQuery ();
20 }
21 finally
22 {
23 connection.Close ();
24 }
25}
26 

上面创建的函数可以通过使用按钮的 onClick 属性来调用。
如何使用ADO.NET技术从数据库中读取图片并把它显示在Web页面上?
这里,我使用Web页面来显示图片,而没有用其他任何控件。下面的代码是用来显示数据库中的图片。
private void Page_Load(object sender, System.EventArgs e)2
{3
MemoryStream stream = new MemoryStream ();4
SqlConnection connection = new 5
SqlConnection (@"server=INDIA\INDIA;database=iSense;uid=sa;pwd=india");6
try7
{8
connection.Open ();9
SqlCommand command = new 10
SqlCommand ("select Picture from Image", connection);11
byte[] image = (byte[]) command.ExecuteScalar (); 12
stream.Write (image, 0, image.Length);13
Bitmap bitmap = new Bitmap (stream);14
Response.ContentType = "image/gif";15
bitmap.Save (Response.OutputStream, ImageFormat.Gif);16
} 17
finally18
{19
connection.Close ();20
stream.Close ();21
}22
}23

GDI+函数为操作和定义图片提供了一个丰富的功能集合。本文的例子只能看到它的一小部分功能。你可以使用 System.Drawing 和 System.Drawing.Imaging 名字空间来调用这些功能。举例来说,你可以开发在Web上保存和管理图片文件的应用程序,或者你可以一个简单、易配置的应用程序使用户能够操作图片。
如何运行程序?
首先,创建一个虚拟目录,把你的工程文件放到这虚拟目录中。然后改变服务器名称,数据库名称以及表的名称,如下所示:
SqlConnection connection = new SqlConnection("server=localhost;database=mypictures;uid=sa;pwd=");然后公布你的工程以获得最好的结果。
下面我我的体会:
使用背景: 在查询出来的 用户信息 ,双击用户,弹出包括相片的用户基本信息
在CombinationQueryShow.aspx 的datagrid_ItemDataBound 事件里面写如下
if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
{
//为数据项添加鼠标单击属性 打开
e.Item.Attributes.Add("ondblclick", "window.open('Person_ContentShow.aspx?PersonInfoID="+e.Item.Cells[0].Text+"' ,'个人信息详细显示','width=800,height=600,resizable=yes,top=100,left=100'"+");");
}
Person_ContentShow.aspx 页面的HTML 内容如下:
<%@ Page language="c#" Codebehind="Person_ContentShow.aspx.cs" AutoEventWireup="false" Inherits="Oceansoft.LGB.WEBUI.admin.search.Person_ContentShow" %>2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >3
<HTML>4
<HEAD>5
<title>个人详细信息显示</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
<LINK href="../images/left.css" type="text/css" rel="stylesheet">11
</HEAD>12
<body MS_POSITIONING="GridLayout">13
<form id="Form1" method="post" runat="server">14
<table id="Maintable" height="100%" width="100%" border="2">15
<tr>16
<td>17
<asp:panel id="Panel1" style="OVERFLOW: auto" runat="server" Height="100%" Width="100%">18
<TABLE id="table1" height="10%" width="100%" border="1">19
<TR>20
<TD width="45%" height="30">21
<asp:label id="PersonID" Width="80" Runat="server" text="人员编号:"></asp:label>22
<asp:label id="lblPersonID" Runat="server" text=""></asp:label></TD>23
<TD width="45%" height="30">24
<asp:label id="enjoydeal" Width="80" Runat="server" text="现享受待遇:"></asp:label>25
<asp:label id="lblenjoydeal" Runat="server" text=""></asp:label></TD>26
<TD align="center" rowSpan="3">照片27
<BR>28
<FONT face="宋体"><IMG height=80 29
src='ImagePage.aspx?PersonInfoID=<%=Request.QueryString["PersonInfoID"]%>' 30
width=80> </FONT>31
</TD>32
</TR>33
<TR>34
<TD width="45%" height="30">35
<asp:label id="PersonName" Width="80" Runat="server" text="姓名:"></asp:label>36
<asp:label id="lblPersonName" Runat="server" text=""></asp:label></TD>37
<TD width="45%" height="30">38
<asp:label id="JoinWorkDate" Width="80" Runat="server" text="参加革命日期:"></asp:label>39
<asp:label id="lblJoinWorkDate" Runat="server" text=""></asp:label></TD>40
</TR>41
<TR>42
<TD width="45%" height="30">43
<asp:label id="manageUnitName" Width="80" Runat="server" text="管理单位:"></asp:label>44
<asp:label id="lblmanageUnitName" Runat="server" text=""></asp:label></TD>45
<TD width="45%" height="30">46
<asp:label id="subjectRelation" Width="80" Runat="server" text="隶属关系:"></asp:label>47
<asp:label id="lblsubjectRelation" Runat="server" text=""></asp:label></TD>48
</TR>49
</TABLE>50
<TABLE id="table2" height="50%" width="100%" border="1">51
<TR>52
<TD>53
<asp:panel id="PnlData" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"54
Height="100%">55
<asp:DataGrid id="LgbDetailInfo" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"56
BorderWidth="1px" BackColor="White" CellPadding="4">57
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>58
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>59
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>60
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>61
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>62
</asp:DataGrid>63
</asp:panel></TD>64
</TR>65
</TABLE>66
<TABLE id="table3" height="20%" width="100%" border="1">67
<TR>68
<TD>69
<asp:panel id="Panel3" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"70
Height="100%">中组部多记录信息集 <BR>71
<asp:DataGrid id="A53InfoShow" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"72
BorderWidth="1px" BackColor="White" CellPadding="4">73
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>74
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>75
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>76
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>77
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>78
</asp:DataGrid></asp:panel></TD>79
</TR>80
</TABLE>81
<TABLE id="table4" height="20%" width="100%" border="1">82
<TR>83
<TD>84
<asp:panel id="Panel4" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"85
Height="100%">子女情况多记录信息集 <BR>86
<asp:DataGrid id="A70InfoShow" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"87
BorderWidth="1px" BackColor="White" CellPadding="4">88
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>89
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>90
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>91
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>92
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>93
</asp:DataGrid></asp:panel></TD>94
</TR>95
</TABLE>96
<TABLE id="table5" height="20%" width="100%" border="1">97
<TR>98
<TD>99
<asp:panel id="Panel5" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"100
Height="100%">非固定补贴情况多记录信息集 <BR>101
<asp:DataGrid id="A71InfoShow" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"102
BorderWidth="1px" BackColor="White" CellPadding="4">103
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>104
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>105
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>106
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>107
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>108
</asp:DataGrid></asp:panel></TD>109
</TR>110
</TABLE>111
<TABLE id="table6" height="20%" width="100%" border="1">112
<TR>113
<TD>114
<asp:panel id="Panel6" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"115
Height="100%">离休后奖惩情况多记录信息集 <BR>116
<asp:DataGrid id="A72InfoShow" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"117
BorderWidth="1px" BackColor="White" CellPadding="4">118
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>119
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>120
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>121
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>122
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>123
</asp:DataGrid></asp:panel></TD>124
</TR>125
</TABLE>126
<TABLE id="table7" height="20%" width="100%" border="1">127
<TR>128
<TD>129
<asp:panel id="Panel7" style="OVERFLOW: auto; POSITION: absolute" runat="server" Width="100%"130
Height="100%">住院信息 <BR>131
<asp:DataGrid id="A73InfoShow" Width="100%" Runat="server" BorderStyle="None" BorderColor="#3366CC"132
BorderWidth="1px" BackColor="White" CellPadding="4">133
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>134
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>135
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>136
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>137
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>138
</asp:DataGrid></asp:panel></TD>139
</TR>140
</TABLE>141
</asp:panel>142
</td>143
</tr>144
</table>145
</form>146
</body>147
</HTML>148

关键部分是:
<IMG height=80
src='ImagePage.aspx?PersonInfoID=<%=Request.QueryString["PersonInfoID"]%>'
width=80>这里需要生成 ImagePage.aspx 页面
在 ImagePage.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;
using System.Drawing.Imaging;

namespace Oceansoft.LGB.WEBUI.admin.search
{
/// <summary>
/// ImagePage 的摘要说明。
/// </summary>
public class ImagePage : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
// if (Page.IsPostBack)
// {
Laod_Image();
// }
//加载图象信息
}
private void Laod_Image()
{
string PersonInfoID=Request.QueryString["PersonInfoID"];
string str_sqlGetImage="select A9901 from LGBPInfo where PersonInfoID='"+PersonInfoID+"'";
//图象处理
readImage(str_sqlGetImage);
}
private void readImage(string str_sql)
{
// try
// {
// CommonInterface CIF=new CommonSql(strConn);
// IDataReader dbRead=CIF.ExeForDtr(str_sql);
// if (dbRead.Read())
// {
// Response.Clear();
// byte[] imageData =(byte[])dbRead["A9901"];
// Int32 imageSize = imageData.Length;
// Response.ContentType=dbRead["A9901"].ToString();
// Response.OutputStream.Write(imageData, 0, imageSize);
// //Response.BinaryWrite(imageData);
// CIF.Close();
// }
// else
// {
// CIF.Close();
// }
// //// Clear Response buffer
// Response.End();
// }
// catch (Exception exe)
// {
// //throw exe;
// }
MemoryStream stream=new MemoryStream();
string strConn=(string)Session["str_Conn"];//数据库连接字串
SqlConnection connection=new SqlConnection(strConn);
try
{
connection.Open();
SqlCommand command=new SqlCommand(str_sql,connection);
byte[] image=(byte[])command.ExecuteScalar();
stream.Write(image,0,image.Length);
Bitmap bitImage=new Bitmap(stream);
Response.ContentType="image/gif";
bitImage.Save(Response.OutputStream,ImageFormat.Gif);
}
catch
{
//获得空白图片文件的路径 c:\inetpub\wwwroot\LGBWEB\admin\search
string strblandImagePath= Server.MapPath("");
//c:\inetpub\wwwroot\LGBWEB\admin\
strblandImagePath=strblandImagePath.Substring(0,strblandImagePath.Length-6)+"images\\blandImage.JPG";
///c:\inetpub\wwwroot\LGBWEB\admin\images\blandImage.JPG
//strblandImagePath+="images\blandImage.JPG";
FileStream FileBlandImagestream=File.OpenRead(strblandImagePath);
Bitmap bitImage=new Bitmap(FileBlandImagestream);
Response.ContentType="image/gif";
bitImage.Save(Response.OutputStream,ImageFormat.Gif);
}
finally
{
connection.Close();
stream.Close();
}
}
#region Web 窗体设计器生成的代码
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
}
}
运行通过


浙公网安备 33010602011771号