设为首页
加入收藏
联系站长
您现在的位置: 网贝 >> 建站学院 >> ASP技术 >> asp基础 >> 文章正文 用户登录 新用户注册
ASP入门及提高       ★★★★
ASP入门及提高
相关网站:
作者:佚名 文章来源:网络整理 点击数: 更新时间:2005-6-7

在SQL Server中保存和输出图片

有时候我们需要保存一些binary data进数据库。SQL Server提供一个叫做image的特殊数据类型供我们保存binary data。Binary data可以是图片、文档等。在这篇文章中我们将看到如何在SQL Server中保存和输出图片。

建表

为了试验这个例子你需要一个含有数据的table(你可以在现在的库中创建它,也可以创建一个新的数据库),下面是它的结构:


Column Name
Datatype
Purpose

ID
Integer
identity column Primary key

IMGTITLE
Varchar(50)
Stores some user friendly title to identity the image

IMGTYPE
Varchar(50)
Stores image content type. This will be same as recognized content types of ASP.NET

IMGDATA
Image
Stores actual image or binary data.

保存images进SQL Server数据库

为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件。确信你设定了Form的encType属性为multipart/form-data。

Stream imgdatastream = File1.PostedFile.InputStream;

int imgdatalen = File1.PostedFile.ContentLength;

string imgtype = File1.PostedFile.ContentType;

string imgtitle = TextBox1.Text;

byte[] imgdata = new byte[imgdatalen];

int n = imgdatastream.Read(imgdata,0,imgdatalen);

string connstr=

((NameValueCollection)Context.GetConfig

("appSettings"))["connstr"];

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand

("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)

VALUES ( @imgtitle, @imgtype,@imgdata )", connection );



SqlParameter paramTitle = new SqlParameter

("@imgtitle", SqlDbType.VarChar,50 );

paramTitle.Value = imgtitle;

command.Parameters.Add( paramTitle);



SqlParameter paramData = new SqlParameter

( "@imgdata", SqlDbType.Image );

paramData.Value = imgdata;

command.Parameters.Add( paramData );



SqlParameter paramType = new SqlParameter

( "@imgtype", SqlDbType.VarChar,50 );

paramType.Value = imgtype;

command.Parameters.Add( paramType );


connection.Open();

int numRowsAffected = command.ExecuteNonQuery();

connection.Close();

数据库中输出图片

现在让我们从数据库中取出我们刚刚保存的图片,在这儿,我们将直接将图片输出至浏览器。你也可以将它保存为一个文件或做任何你想做的。

private void Page_Load(object sender, System.EventArgs e)

{

string imgid =Request.QueryString["imgid"];

string connstr=((NameValueCollection)

Context.GetConfig("appSettings"))["connstr"];

string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "

+ imgid;

SqlConnection connection = new SqlConnection(connstr);

SqlCommand command = new SqlCommand(sql, connection);

connection.Open();

SqlDataReader dr = command.ExecuteReader();

if(dr.Read())

{

Response.ContentType = dr["imgtype"].ToString();

Response.BinaryWrite( (byte[]) dr["imgdata"] );

}

connection.Close();

}


在上面的代码中我们使用了一个已经打开的数据库,通过datareader选择images。接着用Response.BinaryWrite代替Response.Write来显示image文件。

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] 下一页  

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章: 没有了
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    相关网站:

    文章 下载 图片
    推荐文章ASP入门及提高
    普通文章ASP新手指南
    普通文章ASP六大对象介绍
    普通文章ASP函数一览表
    普通文章日期函数包
    推荐文章一些初学都常用的AS
    普通文章Javascript 中对像 
    普通文章[图文]Javascript 中
    推荐文章ASP入门及提高
    推荐文章一些初学都常用的AS
    推荐文章[图文]ASP个人上手指
    推荐文章精华asp代码收集
    推荐文章1小时ASP入门,非常
    推荐文章在ASP中使用SQL语句
    推荐文章[组图]ASP简介
    推荐文章ASP技术
    普通文章把手教你写私服列表
    普通文章WEB标准
    普通文章GOOGLE sitemap官方
    普通文章用php定制404错误页
    普通文章WAP(wml)开发问答
    普通文章[组图]PS教程系列:快
    普通文章时使用apache和IIS,
    普通文章[组图]2005年的第一
    推荐文章[组图]彻底掌握IIS6
    推荐文章[组图]CSS 全攻略
    推荐文章各种脚本错误详解!
    推荐文章直接生成XML的Googl
    推荐文章九个常见的错误原因
    推荐文章Win XP家用版也能装
    推荐文章JSP入门初级教程之J
    推荐文章ASP入门及提高
    没有相关文章

    Javascript 中 浅拷

    ASP简介
    (只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    Copyright © 2003-2005 NetBei.com All rights reserved.