下载首页 | 最近更新 | 软件分类 | 下载排行 | 主机托管 | 免费电影 | 网游外挂 | 网页设计 | 幻想春秋 | 剑侠情缘II | 幻想春秋 |
 
网络软件 | 系统工具 | 应用软件 | 破解补丁 | 杀毒软件 | 下载排行 | 软件分类 | 最近更新
游戏娱乐 | 游戏外挂 | 街机游戏 | 安全相关 | 联络聊天 | 图形图像 | 多媒体类 | 行业软件
精品源码 | 精品字体 | 编程开发 | 教育教学 | 音乐视频 | 资讯中心 | 两性频道 | 社会资讯
软件搜索
您现在的位置
真诚网络首页 -> 电子书库电脑教程 -> 在SQL Server中保存和输出图片
在SQL Server中保存和输出图片
软件语言: 未知
软件类型: 未知/授权未知/电脑教程
运行环境: Win9x/NT/2000/XP/
页面刷新: 2008-11-2 12:15:02
软件大小: 未知
软件等级:
整理时间: 2005-2-19 22:13:16
联 系 人: 未知
开 发 商: 未知
解压密码:
下载次数: 0
特约广告:
介绍
   
   
   
   有时候我们需要保存一些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文件。
   
相关网站:
3721 3721 中“在SQL Server中保存和输出图片 ”相关内容
google Google 中“在SQL Server中保存和输出图片 ”相关内容
sogou sogou搜狐 中“在SQL Server中保存和输出图片 ”相关内容
yisou 一搜 中“在SQL Server中保存和输出图片 ”相关内容
baidu 百度 中“在SQL Server中保存和输出图片 ”相关内容
热门文章
相关软件
总下载排行
本周下载派行
·红色警戒2之尤里的复仇 中文版
·联众麻将作弊器助手
·恐龙快打
·净网先锋 客户端破解程序
·星际争霸1.08
·遥志代理服务器(CCProxy) 6.2 正式注册版
·魔兽争霸-冰峰王座完美硬盘版
·红色警戒3-世界大战
·闪光鬼怪(FlashSprite) 3.03
·微机组装与维护---显示声音设备
·10天学会asp
·ASP 与XML 高级编程(PDG)
·ASP 中健壮的页结构的异常处理
·在SQL Server中保存和输出图片
·web.config一个中文解释
·ASP.NET高级应用(1)
·ASP.NET高级应用(2)
·ASP.NET高级应用(3)
·怎样创建.NET Web Service
·怎样创建.NET Web Service(2)
关于我们 | 帮助(?) | 广告服务 | 友情链接 | 版权声明 | 网站地图 | 商业合作 | 在线留言 | 联系我们 |
『真诚下载网』非凡享受源至于真诚服务 本站QQ官方1号群:6694792
CopyRight 2002-2005 zcdown.com? All Rights Reserved
苏ICP备05031592