仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 592|回复: 7
打印 上一主题 下一主题

[学习教程] MYSQL网站制作之怎样在SQL Server中保留和输入图片

[复制链接]
山那边是海 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:32:04 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
通过支付一定费用,客户可以得到优先的24/7支持,访问内容丰富的在线知识库和联系一个专门的技术负责经理。server建表  

为了实验这个例子你必要一个含无数据的table(你能够在如今的库中创立它,也能够创立一个新的数据库),上面是它的布局: 

<Pclass=code>
  1.   ColumnName  Datatype  Purpose  ID  Integer  identitycolumnPrimarykey  IMGTITLE  Varchar(50)  Storessomeuserfriendlytitletoidentitytheimage  IMGTYPE  Varchar(50)  Storesimagecontenttype.ThiswillbesameasrecognizedcontenttypesofASP.NET  IMGDATA  Image  Storesactualimageorbinarydata.
复制代码
保留images进SQLServer数据库 

为了保留图片到table你起首得从客户端上传它们到你的web服务器。你能够创立一个webform,用TextBox失掉图片的题目,用HTMLFileServerControl失掉图片文件。确信你设定了Form的encType属性为multipart/form-data。 

<Pclass=code>
  1.   Streamimgdatastream=File1.PostedFile.InputStream;  intimgdatalen=File1.PostedFile.ContentLength;  stringimgtype=File1.PostedFile.ContentType;  stringimgtitle=TextBox1.Text;  byte[]imgdata=newbyte[imgdatalen];  intn=imgdatastream.Read(imgdata,0,imgdatalen);  stringconnstr=  ((NameValueCollection)Context.GetConfig  ("appSettings"))["connstr"];  SqlConnectionconnection=newSqlConnection(connstr);  SqlCommandcommand=newSqlCommand  ("INSERTINTOImageStore(imgtitle,imgtype,imgdata)  VALUES(@imgtitle,@imgtype,@imgdata)",connection);  SqlParameterparamTitle=newSqlParameter  ("@imgtitle",SqlDbType.VarChar,50);  paramTitle.Value=imgtitle;  command.Parameters.Add(paramTitle);  SqlParameterparamData=newSqlParameter  ("@imgdata",SqlDbType.Image);  paramData.Value=imgdata;  command.Parameters.Add(paramData);  SqlParameterparamType=newSqlParameter  ("@imgtype",SqlDbType.VarChar,50);  paramType.Value=imgtype;  command.Parameters.Add(paramType);  connection.Open();  intnumRowsAffected=command.ExecuteNonQuery();  connection.Close();  
复制代码
从数据库中输入图片  

如今让我们从数据库中掏出我们方才保留的图片,在这儿,我们将间接将图片输入至扫瞄器。你也能够将它保留为一个文件或做任何你想做的。<Pclass=code>
  1.   privatevoidPage_Load(objectsender,System.EventArgse)  {  stringimgid=Request.QueryString["imgid"];  stringconnstr=((NameValueCollection)  Context.GetConfig("appSettings"))["connstr"];  stringsql="SELECTimgdata,imgtypeFROMImageStoreWHEREid="  +imgid;  SqlConnectionconnection=newSqlConnection(connstr);  SqlCommandcommand=newSqlCommand(sql,connection);  connection.Open();  SqlDataReaderdr=command.ExecuteReader();  if(dr.Read())  {  Response.ContentType=dr["imgtype"].ToString();  Response.BinaryWrite((byte[])dr["imgdata"]);  }  connection.Close();  }  
复制代码
在下面的代码中我们利用了一个已翻开的数据库,经由过程datareader选择images。接着用Response.BinaryWrite取代Response.Write来显现image文件。那时候Sybase已经诞生了6年的时间。至于其他值得关注的开源数据库,PostgreSQL将在2009年达到20岁的生日。虽然MySQL并不是市场上最年轻的数据库,但是却有更多成熟的数据库可供我们选择。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-16 21:17

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表