资讯 IT资讯 中考新闻 2006高考 教育政策 高等教育 自考动态 成考动态 考研动态 留学政策 招生近况 求职快讯
 
IT综合 资讯中心 网络学院 下载 中考 高考 自学考试 成人高考 考研 留学 外语 求职
普及贴吧 教案集锦 范文大全 免费资源 小说 笑话
 
  
返回首页 热门贴子 电影网通1 范文电信1 范文网通1 范文网通2 BT搜索 BT下载网通1 乱贴吧
[当前位置]
首页>网络学院>数据库>Mssql>MSSqlServer基础>正文--在SQL Server中保存和输出图片
[BT滚动] ·影视 ·综艺 ·动漫 ·游戏 ·音乐 ·软件
[本站地图] 访问慢?请走这里--> 电信通道 
搜索
铃声下载


 
 
 
应试心理 当前文章标题:首页>网络学院>数据库>Mssql>MSSqlServer基础>正文--在SQL Server中保存和输出图片 ... 
 

在SQL Server中保存和输出图片

SQL Server中保存和输出图片

 

有时候我们需要保存一些binary data进数据库。SQL Server提供一个叫做image的特殊数据类型供我们保存binary dataBinary 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.

 

保存imagesSQL Server数据库

  为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件。确信你设定了FormencType属性为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文件。

当前文章:在SQL Server中保存和输出图片        打印此文 | 关闭窗口

·关键字相关新闻 ·滚动新闻

应试心理 历年自学考试题/试卷 more... 
应试心理 历年考研试题/试卷 more... 

投稿信息 投稿及评论内容只代表网友观点,与本站立场无关!
①凡本站注明“稿件来源:普及网/中国普及教育网/本站原创”的所有文字、图片和音视频稿件,版权均属本网所有,任何媒体、网站或个人未经本网协议授权不得转载、链接、转贴或以其他方式复制发表。已经本站协议授权的媒体、网站,在下载使用时必须注明"稿件来源:普及网/中国普及教育网",违者本站将依法追究责任。
② 本站注明稿件来源为其他媒体的文/图等稿件均为转载稿,本站转载出于非商业性的教育和科研之目的,并不意味着赞同其观点或证实其内容的真实性。其中摘录的内容以共享、研究为目的,不存在任何商业考虑。如转载稿涉及版权等问题,请作者在两周内速来电或来函联系。目前网站上有些文章未注明作者或出处,甚至标注错误,此类情况出现并非不尊重作者及出处网站,而是因为有些资料来源的不规范。如果有了解作者或出处的原作者或网友,请告知,本网站将立即更正注明,并向作者或出处单位道歉。
当前文章:在SQL Server中保存和输出图片
Powered by:pujiwang.com,普及网.com,普及网.cn,普及网.中国 ©2002-2005 webmaster#pujiwang.com
冀ICP备05000751号 合作代理:展迅互联主机 [点击联系]QQ:46083540 QQ群:10797742
请使用IE6.0或以上浏览器,1024*768分辨率浏览