1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 将SQL Server 2000数据库Image类型图片数据显示在页面上

将SQL Server 2000数据库Image类型图片数据显示在页面上

时间:2023-11-22 09:49:50

相关推荐

将SQL Server 2000数据库Image类型图片数据显示在页面上

一、建立数据表

--==================================

--==================================

--TABLENAME:E_PHOTO

--AUTHOR:XIAOPENGCHENG

--CREATEDATE:-11-1

--DESCRIPTION:SAVEPHTOINFORMATION

--==================================

--==================================

ifexists(select*fromsysobjectswhereid=object_id('dbo.E_PHOTO'))

droptabledbo.E_PHOTO

GO

createtableE_PHOTO

(

USER_NAMENVARCHAR(40)NOTNULL,--USERNAME

PHOTO_TYPE_IDintNOTNULL,--PHOTOTYPE

PHOTO_IDNVARCHAR(30)NOTNULL,--PHOTONAME

PHOTOIMAGENOTNULL,--PHOTOINFORMATION

PHOTO_DESCNVARCHAR(3000),--PHOTODESCRIPTIONINFORMATION

UPLOAD_DATESMALLDATETIME,--UPLOADDATE

CONSTRAINTPHOTOPRIMARYKEY(USER_NAME,PHOTO_ID,PHOTO_TYPE_ID)--PRIMARYKEY

)

GO

--==================================

--==================================

--TABLENAME:E_PHOTO_TYPE

--AUTHOR:XIAOPENGCHENG

--CREATEDATE:-11-3

--DESCRIPTION:SAVEPHTOTYPEINFORMATION

--==================================

--==================================

ifexists(select*fromsysobjectswhereid=object_id('dbo.E_PHOTO_TYPE'))

droptabledbo.E_PHOTO_TYPE

GO

createtableE_PHOTO_TYPE

(

USER_NAMENVARCHAR(40)NOTNULL,--USERNAME

PHOTO_TYPE_CODEintidentity(1,1)NOTNULL,--PHOTOTYPECODE

PHOTO_TYPE_NAMENVARCHAR(30)NOTNULL,--PHOTOTYPENAME

IF_USINGINTDEFAULT0,--IFUSING//USING:0;NOTUSING1;

CONSTRAINTPHOTO_TYPEPRIMARYKEY(USER_NAME,PHOTO_TYPE_CODE)--PRIMARYKEY

)

GO

二、建立页面,对图片类型进行分类

由于本人,在上传图片时,对图片进行了分类,因此当用户进行图片浏览的时候,对图片进行了分类,当用户单击“ALL Photo”时,将在另外的一个页面进行显示,以下是进行图片类型分类的HTML页面代码:

<%...@PageLanguage="C#"MasterPageFile="~/MasterPage2.master"AutoEventWireup="true"

CodeFile="ListPhotoType.aspx.cs"Inherits="ListPhotoType"Title="ListPhotoType"%>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"runat="Server">

<strong>ListThePhotoType:<br/>

</strong>

<br/>

<asp:BulletedListID="BulletedList1"runat="server"BulletStyle="Numbered"DisplayMode="LinkButton"

OnClick="BulletedList1_Click">

</asp:BulletedList>

</asp:Content>

从上面的代码,我们可以看出,我们用了模板页,在页面中,我们只用了一个BulletedList控件,显示的模式是用的连接an钮,列表的模式是用在左边有一些标题,在页面上有个对每一项都有一个单击事件发生。以下是此页面对应的后台编程页面:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclassListPhotoType:System.Web.UI.Page

...{

PhotoManagemanage;

protectedvoidPage_Load(objectsender,EventArgse)

...{

if(!(Convert.ToBoolean(Session["isLogin"])))

...{

Response.Redirect("Login.aspx");

}

if(!Page.IsPostBack)

...{

manage=newPhotoManage(Session["name"].ToString());

this.BulletedList1.DataSource=manage.GetUserPhotoType(Session["name"].ToString());

this.BulletedList1.DataTextField="Photo_Type_Name";

this.BulletedList1.DataValueField="Photo_Type_Code";

this.BulletedList1.DataBind();

this.BulletedList1.Items.Insert(0,newListItem("ALLPhoto","A"));

}

}

protectedvoidBulletedList1_Click(objectsender,BulletedListEventArgse)

...{

stringstrURL=string.Format("ListPhoto.aspx?code={0}",Server.UrlEncode(this.BulletedList1.Items[e.Index].Value));

//this.BulletedList1.Attributes.Add("onclick",string.Format("window.open('{0}','newwinow');",strURL));

Response.Redirect(strURL);

}

}

从此后台页面编程,我们可以看出,首先设置数据源,进行了数据绑定,可以让数据从数据库中读取, 显示在页面,为了将全部数据显示出来,我们在BulletedLista控件的第一个位置添加了"ALL Photo",其中的值是“A”,在控件的单击事件中,我们将值取出,之后传递到ListPhoto.aspx页面进行处理。以下是ListPhoto.aspx页面的HTML代码:

<%...@PageLanguage="C#"MasterPageFile="~/MasterPage2.master"AutoEventWireup="true"

CodeFile="ListPhoto.aspx.cs"Inherits="ListPhoto"Title="ListPhoto"%>

<asp:ContentID="Content1"ContentPlaceHolderID="ContentPlaceHolder1"runat="Server">

<asp:DataListID="dlPhoto"runat="server"RepeatColumns="4"RepeatDirection="Horizontal"

CellPadding="0"Width="790px"OnItemCommand="dlPhoto_ItemCommand"OnItemDataBound="dlPhoto_ItemDataBound">

<ItemTemplate>

<asp:ImageButtonWidth="185"ID="ibtnPhoto"ImageUrl='<%#FormatURL(DataBinder.Eval(Container,"DataItem.user_name"),DataBinder.Eval(Container,"DataItem.photo_type_id"),DataBinder.Eval(Container,"DataItem.photo_id"))%>'

runat="server"AlternateText="clicklistDetailsinformation!"CommandName="Detail"

CommandArgument="Detail"/><br/>

<asp:LabelID="lblPhotoType"runat="server"Text='<%#DataBinder.Eval(Container,"DataItem.photo_type_id")%>'

Visible="false"></asp:Label><br/>

<asp:LabelID="lblPhotoName"runat="server"Text='<%#DataBinder.Eval(Container,"DataItem.photo_id")%>'

Visible="false"></asp:Label>

</ItemTemplate>

</asp:DataList>

</asp:Content>

在此页面,我们可以看出,在此页面,我们只用了一个DataList控件,其中里面用了一个ImageButton用来显示图片,两个标签,一个是此种图片的类型,一个是图片的名称,我们可以从HMTL中我们可以看出,ImageButton控件的ImageUrl属性是通过调用一个函数实现的,里面传递了三个参数,都是通过绑定表达式,输入的。下面是两个标签,用来保存信息。下面的代码是,此页面对应的后台编程页面:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclassListPhoto:System.Web.UI.Page

...{

protectedvoidPage_Load(objectsender,EventArgse)

...{

if(!Page.IsPostBack)

...{

if(!(Convert.ToBoolean(Session["isLogin"])))

...{

Response.Redirect("Login.aspx");

}

else

...{

System.Data.SqlClient.SqlConnectionconn=newSystem.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DogsManagerConnectionString"].ToString());

stringcode,strSQL;

code=Request.QueryString["code"].ToString();

if(code=="A")

...{

strSQL=string.Format("selectuser_name,photo_type_id,photo_id,photofromE_Photowhereuser_name='{0}'",Session["name"].ToString());

}

else

...{

strSQL=string.Format("selectuser_name,photo_type_id,photo_id,photofromE_Photowhereuser_name='{0}'ANDphoto_type_id={1}",Session["name"].ToString(),code);

}

System.Data.SqlClient.SqlCommandcmd=newSystem.Data.SqlClient.SqlCommand(strSQL,conn);

try

...{

conn.Open();

this.dlPhoto.DataSource=cmd.ExecuteReader(CommandBehavior.CloseConnection);

this.dlPhoto.DataBind();

}

catch(System.Data.SqlClient.SqlExceptionsql)

...{

Response.Write("wronginformation:<br>"+sql.Message);

}

finally

...{

cmd.Dispose();

conn.Close();

}

}

}

}

protectedstringFormatURL(objectuserName,objecttypeID,objectphotoID)

...{

returnstring.Format("ReadImage.aspx?userName={0}&typeID={1}&photoID={2}",

userName.ToString(),typeID.ToString(),photoID.ToString());

}

protectedvoiddlPhoto_ItemCommand(objectsource,DataListCommandEventArgse)

...{

if(mandArgument.ToString()=="Detail")

...{

stringuserName=Session["name"].ToString();

stringtypeId=((Label)e.Item.FindControl("lblPhotoType")).Text;

stringphotoId=((Label)e.Item.FindControl("lblPhotoName")).Text;

ImageButtonibtnDetail=(ImageButton)e.Item.FindControl("ibtnPhoto");

stringFormat=string.Format("ListPhotoDetail.aspx?userName={0}&typeID={1}&photoID={2}",

userName,typeId,photoId);

ibtnDetail.Click+=newImageClickEventHandler(ibtnDetail_Click);

}

}

voidibtnDetail_Click(objectsender,ImageClickEventArgse)

...{

Response.Redirect("");

}

protectedvoiddlPhoto_ItemDataBound(objectsender,DataListItemEventArgse)

...{

//stringuserName=Session["name"].ToString();

//stringtypeId=((Label)e.Item.FindControl("lblPhotoType")).Text;

//stringphotoId=((Label)e.Item.FindControl("lblPhotoName")).Text;

//ImageButtonibtnDetail=(ImageButton)e.Item.FindControl("ibtnPhoto");

//stringFormat=string.Format("ListPhotoDetail.aspx?userName={0}&typeID={1}&photoID={2}",

//userName,typeId,photoId);

//ibtnDetail.Attributes.Add("onclick","Response.Write("+Format+")");

}

}

此,后台代码也是比较的多的,在这些代码中,我们可以看到一个FormatURL函数,这和我们在HTML代码中看到的一致,用来将每绑定一个,定位到一个页面,将数据图片信息从数据库中读取出来。这个页面是ReadImage.aspx页面,这个过会再说。在这个后台编程页面中,我们在PageLoad中我们可以看到了我们把传递过来的参数code进行了判断,主要是使判断,是将某个类型的显示出来,还是将所有的显示出来,如果是"A",则是此用户的相关的所有图片信息显示出来,如果是其他,将此类的从数据库中读取,显示出来。读者可以看到,本人之所以用ImageButtonk控件,主要还是想实现另外一个功能,单击图片将此图片的详细信息显示出来,可以我还没有调出来,调出来后,再写吧。在此页面,主要工作是,当绑定一个时,调用FormatURL函数,跳转到ReadImage.aspx页面对图片读取,之后,一次类推。以下是:ReadImage.aspx页面的后台编程代码,此页面仅此作个处理,没有HTML代码了:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclassReadImage:System.Web.UI.Page

...{

protectedvoidPage_Load(objectsender,EventArgse)

...{

if(!Page.IsPostBack)

...{

stringusername=Request.QueryString["userName"].ToString();

stringtypeid=Request.QueryString["typeID"].ToString();

stringphotoid=Request.QueryString["photoID"].ToString();

System.Data.SqlClient.SqlConnectionconn=newSystem.Data.SqlClient.SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DogsManagerConnectionString"].ToString());

stringstrSQL=string.Format("selectphotofromE_Photowhereuser_name='{0}'andphoto_type_id="+

"{1}andphoto_id='{2}'",username,typeid,photoid);

System.Data.SqlClient.SqlCommandcmd=newSystem.Data.SqlClient.SqlCommand(strSQL,conn);

try

...{

conn.Open();

System.Data.SqlClient.SqlDataReaderreader=cmd.ExecuteReader(CommandBehavior.CloseConnection);

if(reader.Read())

...{

Response.Clear();

Response.ContentType="images/*";

Response.BinaryWrite((byte[])reader["photo"]);

}

conn.Close();

}

catch(System.Data.SqlClient.SqlExceptionsql)

...{

throwsql;

}

Response.End();

}

}

}

此页面,接受传递过来的三个参数,是为了准确定位一条记录,我们从数据表中,我们也就看到,这个表示三个联合主键,之后,进行读取,并且设置ContentType为“images/*”类型,之后输出。至此,我们的图片也就可以从数据库中读取出来了,并显示在页面上,但图片毕竟是从数据库中读取出来的,因此,有时我们会感觉,我们的图片显示的有点慢,但毕竟是一个一个从数据库中读取出来的,肯定是慢了点。以下几个页面时我测试用的读取的图片信息:

至此,我的从SQL Server 2000数据库中读取Image类型数据并显示在页面上,已经完成了, 其实,遇到问题,多上网查查,写程序,初级阶段就是比葫芦画瓢,这个方法,我也是从网上找到了。相信问题都可以解决的!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。