Username: Password:

在ASP.NET中存取图片到数据库的示例-.NET教程,Asp.Net研发
来源:作者: 发布时间:2007-12-25 13:36:09

//研发环境:window 2000、sqlserver2000、.net framework sdk正式版
//研发语言:c#、asp.net
//简介:数据库中图片存蓄及读取
//作者:engine
/*
说明:在asp中,我们用request.totalbytes、request.binaryread()来上传图片,这个可恶的binaryread()方法很笨,单个文档上传倒没什么大事,单假如多个图片上专可就花大气力了…!而现在asp.net中将会把解决以前asp中文档上传的种种问题,使您在asp.net中轻轻松松研发出功能强大的上传程式,下面大家看看例子啦。
*/
//注意:由于作者水平有限,错误是难免的,如发现错误请指教
//email:e_engine@21cn.com

/*
首先在sql server中建立一个图片存储的数库表,imagedata column为图象二进制数据储存字段,imagecontenttype column为图象文档类型记录字段,imagedescription column为储蓄图象文档说明字段,imagesize column为储存图象文档长度字段,结构如下:
create table [dbo].[imagestore] (
[imageid] [int] identity (1, 1) not null ,
[imagedata] [image] null ,
[imagecontenttype] [varchar] (50) collate chinese_prc_ci_as null ,
[imagedescription] [varchar] (200) collate chinese_prc_ci_as null ,
[imagesize] [int] null
) on [primary] textimage_on [primary]
*/

//uploadimage.aspx程式内容如下:
<%@ page inherits="uploadimage.uploadimage" src="uploadimage.cs" language="c#"%>
上传图片















上传图片(选择您要上传的图片)


文档说明(添加上传图片说明,如:作者、出处)










//-------------------------------------------------------------------
//uploadimage.cs程式内容如下:
using system;
using system.web;
using system.io;
using system.data;
using system.data.sqlclient;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace uploadimage
{
public class uploadimage : page {
protected htmlinputfile up_file; //htmlcontrol、webcontrols控件对象
protected textbox txtdescription;
protected label txtmessage;
protected int32 filelength = 0; //记录文档长度变量
protected void button_submit(system.object sender, system.eventargs e) {
httppostedfile upfile = up_file.postedfile; //httppostedfile对象,用于读取图象文档属性
filelength = upfile.contentlength; //记录文档长度
try {
if (filelength == 0) { //文档长度为零时
txtmessage.text = "请您选择您要上传的文档";
} else {
byte[] filebytearray = new byte[filelength]; //图象文档临时储存byte数组
stream streamobject = upfile.inputstream; //建立数据流对像
//读取图象文档数据,filebytearray为数据储存体,0为数据指针位置、filelnegth为数据长度
streamobject.read(filebytearray,0,filelength);
//建立sql server链接
sqlconnection con = new sqlconnection("data source=localhost;initial catalog=testdb;user id=sa;pwd=;");
string sqlcmd = "insert into imagestore (imagedata, imagecontenttype, imagedescription, imagesize) values (@image, @contenttype, @imagedescription, @imagesize)";
sqlcommand cmdobj = new sqlcommand(sqlcmd, con);
cmdobj.parameters.add("@image",sqldbtype.binary, filelength).value = filebytearray;
cmdobj.parameters.add("@contenttype", sqldbtype.varchar,50).value = upfile.contenttype; //记录文档类型
//把其他单表数据记录上传
cmdobj.parameters.add("@imagedescription", sqldbtype.varchar,200).value = txtdescription.text;
//记录文档长度,读取时使用
cmdobj.parameters.add("@imagesize", sqldbtype.bigint,8).value = upfile.contentlength;
con.open();
cmdobj.executenonquery();
con.close();
txtmessage.text = "

ok!您已成功上传您的图片";//提示上传成功
}
} catch (exception ex) {
txtmessage.text = ex.message.tostring();
}}}}
//----------------------------------------------------------------------
//好了,图片已上传到数据库,现在还要干什么呢?当然是在数据库中读取及显示在web页中啦,请看以下程式:
//readimage.aspx程式内容如下:
/-----------------------------------------------------------------------
<%@ page inherits="readimage.maindisplay" src="readimage.cs"%>
//----------------------------------------------------------------------
//readimage.cs程式内容如下:
using system;
using system.data;
using system.data.sqlclient;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
namespace readimage {
public class maindisplay : system.web.ui.page {
public void page_load(system.object sender, system.eventargs e) {
int imgid = convert.toint32(request.querystring["imgid"]); //imgid为图片id
//建立数据库链接
sqlconnection con = new sqlconnection("data source=king;initial catalog=testdb;user id=sa;pwd=;");
string sqlcmd = "select * from imagestore where imageid = @imageid";
sqlcommand cmdobj = new sqlcommand(sqlcmd, con);
cmdobj.parameters.add("@imageid", sqldbtype.int).value = imgid;
con.open();
sqldatareader sqlreader = cmdobj.executereader();
sqlreader.read();
response.contenttype = (string)sqlreader["imagecontenttype"];//设定输出文档类型
//输出图象文档二进制数制
response.outputstream.write((byte[])sqlreader["imagedata"], 0, (int)sqlreader["imagesize"]);
response.end();
con.close();
//很简单吧^_^
}
}
}
//--------------------------------------------------------------------
//最后,我们当然要把他在web页面显示出来啦
//showimage.hml


这个是从数据库读取出来的图象:


//------------------------------------------------------------------
//最后,这程式当然还很多改进之处,希望大家多想想多编编一定能够写出更多的图象上传程式
//good luck,engine

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅