在SQL Server中保存和输出图片

2016-01-29 19:33 118 1 收藏

在SQL Server中保存和输出图片,在SQL Server中保存和输出图片

【 tulaoshi.com - ASP 】

       介绍
  
  
  
   有时候我们需要保存一些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, Sys

来源:https://www.tulaoshi.com/n/20160129/1509675.html

延伸阅读
SQL 是用于访问和处理数据库的标准的计算机语言。 什么是 SQL? SQL 指结构化查询语言 SQL 使我们有能力访问数据库 SQL 是一种 ANSI 的标准计算机语言 编者注:ANSI,美国国家标准化组织 SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的纪录 S...
标签: SQLServer
1. 查看数据库的版本     select @@version     常见的几种SQL SERVER打补丁后的版本号:     8.00.194   Microsoft SQL Server 2000   8.00.384   Microsoft SQL Server 2000 SP1   8.00.532   Microsoft SQL Server 2000 SP2   8.00.760   Microsoft SQL Server 200...
你需要做的第一件事是确定xp_cmdshell是可用的。你可以选择下面两种方法中的一种来实现。 1.你可以使用sp_configure并执行下面的脚本。 EXEC master.dbo.sp_configure 'show advanced options', 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell', 1 RECONFIGURE 2.你可以使用Surface Area Configura...
标签: SQLServer
 假如你写过很多程序,你可能偶尔会碰到要确定字符或字符窜串否包含在一段文字中,在这篇文章中,我将讨论使用CHARINDEX和PATINDEX函数来搜索文字列和字符串。我将告诉你这两个函数是如何运转的,解释他们的区别。同时提供一些例子,通过这些例子,你可以可以考虑使用这两个函数来解决很多不同的字符搜索的问题。     ...
标签: Web开发
通过ADO可以访问SQL SERVER,并执行相应的SQL语句建库、建表,下面是SQL SERVER BOOKS ONLINE中的相关定义。     建表:   CREATE TABLE   [       database_name.[owner].       | owner. &n...

经验教程

829

收藏

50
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部