存储图片到SQLSERVER数据库中

上传人:壹****1 文档编号:559577645 上传时间:2022-11-24 格式:DOC 页数:8 大小:99.50KB
返回 下载 相关 举报
存储图片到SQLSERVER数据库中_第1页
第1页 / 共8页
存储图片到SQLSERVER数据库中_第2页
第2页 / 共8页
存储图片到SQLSERVER数据库中_第3页
第3页 / 共8页
存储图片到SQLSERVER数据库中_第4页
第4页 / 共8页
存储图片到SQLSERVER数据库中_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《存储图片到SQLSERVER数据库中》由会员分享,可在线阅读,更多相关《存储图片到SQLSERVER数据库中(8页珍藏版)》请在金锄头文库上搜索。

1、如何存储图片到SQL SERVER数据库中SQL Sender提供了一个特别的数据类型:unage,它是一个包含binary数据的类型。下 边这个例子就向你展示了如何将文本或照片放入到数据库中的办法。在这篇文章中我们要看 到如何在SQLServer中存储和读取图片。1、建立一个表:在SQL SERVER中建立这样结构的一个表: 列名 类型 目的ID Integer 主键 IDIMGTITLE Varchar(50) 图片的标题IMGTYPE Varcliar(50)图片类型.ASP.NET要以辨认的类型IMGDATA Image 用于存储二进制数据2、存储图片到SQL SERVER数据库中为了

2、能存储到表中,你首先要上传它们到你的WEE服务器上,你可以开发一个web form,它用来将客户端中TextBox web control中的图片入到你的WEE服务器上来。将你的 encTvpe 属性设置为:myltipart/formdata.Stream migdatastream = File 1 .PostedFile.InputStieam;mt lmgdatalen = Filel.PostedFile.ContentLength;string imgtvpe = File 1 .PostedFile.ContentType;string imgtitle = TextBox 1.

3、Text;byte iingdata = new bytefimgdatalen;mt n = migdatastieam.Read(iingdata,Ojmgdatalen);string coniisti-=(NameValueCollection)Context.GetConfig(HappSettingsH)nconiistrH;SqlCoiinection connection = new SqlConiiection(coimstr);SqlConmiand command = new SqlConmiaiid(INSERT INTO IinageStore(iiugtitle4m

4、gtype4mgdata) VALUES ( iingtitle, imgtype,iingdata connection);SqlParameter paiamTitle = new SqlPaiameter(MiingtitleH, SqlDbTvpe.VaiChai;50 );paiamTitle.Value = iingtitle; coiniuand.Parameters.Add( paiamTitle);SqlParameter paianiData = new SqlParameter( Miingdata, SqlDbTvpe.linage ); paianiData. Val

5、ue = lmgdata;command.Paiameters.Add( paianiData );SqlParameter paiamTvpe = new SqlPaiainetei( Himgtype, SqlDbType.VarChar.5O ); paiamType.Value = imgtvpe;command.Parameters.Add( paiamTvpe);coimection.OpenQ;mt nuniRowsAffected = conmiand.ExecuteNonQueiy();coimection.CloseQ;3、从数据库中恢复读取现在让我们来从SQL Serve

6、r中读取我们放入的数据吧!我们将要输出图片到你的浏览器 上,你也可以将它存放到你要的位置。private void Page_Load(object sender, System.EventAigs e)string iingid =Request.QueryStriiigHuugidn;string comisti-=(NameValueCollection)Context.GetConfig(HappSettmgsH)Hcoiiiistrn;string sql=MSELECT imgdata, iingtype FROM IinageStore WHERE id = + iingid;

7、SqlCoiinection connection = new SqlCoimection(coimstr);SqlConmiand coimiiand = new SqlConmiaiid(sqL connection);coimection.OpenQ;SqlDataReader dr = conmiaiid.ExecuteReaderQ;if(dr.ReadQ)Response.ContentTvpe =iiiigtypeM .ToStiingQ;Response.BmaiyWrite( (bvte) drMimgdataH);coimection.CloseQ;要注意的是 Respon

8、se.BinaryWrite 而不是 Response.Wiite下面给大家一个用于c# Wmform的存入、读取程序。其中不同请犬家自己比较!(为了 方便起见,我将数据库字段简化为二个:imgtitle和imgdam using System;using System.Drawing;using SystemCollections;using System.ComponentModel;using System.Wuidows.Fornis;using System.Data;using System.IO;using System.Data.SqlClient;namespace Wmdo

9、wsApplication21/ V summary/Forml的摘要说明。/ public class Fonnl : System.Windows.Fornis.Fonnprivate System.Windows.Fonns.Button button 1;/ Vsummarv/必需的设计器变量。/ private System.ComponentModel.Contaiiier components = null;private string CoimectionString = Integrated Security=SSPI;Iiiitial Catalog=;Data Sourc

10、e=localhost;H;private SqlComiection conn = null;private SqlConmiand cmd = null;private System.Windows.Fonns.Button buttoii2;private System.Wiiidows.Fonns.PictureBox picl;private System.Windows.Fonns.OpeiiFileDialog opeiiFileDialogl;private string sql = null;private System.Wiiidows.Fonns.Label label2

11、;private string nowld=null;public Fonnl Q/ Wmdoxvs窗体设计器支持所必需的IiutializeComponent();conn = new SqlCoiinection(CoimectionStrmg);TODO:在IiutializeComponent调用后添加任何构造函数代码/清理所有正在使用的资源。/ V/summaryprotected override void Dispose( bool disposing )if (conn.State = CoimectionState.Open) comi.CloseQ;if( disposin

12、g)if (components != null)components.DisposeQ;base.Dispose( disposing);#region Windows Form Designer generated code/ Vsummary/设计器支持所需的方法-不要使用代码编辑器修改/此方法的内容。/ V/summaryprivate void InitializeComponent()this.buttonl = new System.Wiiidows.Forms.ButtonQ;this.picl = new System.Windows.Fonns.PictureBox();t

13、his.buttoii2 = new System.Wiiidows.Forms.ButtonQ;this.opeiiFileDialog 1 = new System.Windows.Forms.OpenFileDialog();this.label2 = new System.Wiiidows.Fonns.LabelQ;thi s Su spendLavout();/ button 1/this.buttonl .Location = new System.Diawmg.Pomt(0.40);this.buttonl.Name = Mbuttonr,;this.buttonl.Size =

14、 new System.Diawiiig. Size(264.48);this.buttonl .Tabliidex = 0;this.buttonl.Text = ”加入新的图片”;this.buttonl.Click += new System.EventHandler(tlus.buttonl_Click);/picl/this.picl.Location = new Svstem.Drawing.Point(280, 8);this.picl.Name = HpiclH;this.picl.Size = new System.Drawiiig.Size(344, 264);this.p

15、icl.Tabliidex = 3;this.picl.TabStop = false;/ buttoii2this.buttoii2.Location = new System.Diawmg.Pomt(0. 104);this.buttoii2 .Name = Mbutton2H;this.buttoii2.Size = new System.Diawiiig. Size(264,40);this.buttoii2 .Tabliidex = 4;tlus.buttoii2.Text = ”从数据库中恢复图像”;this.buttoii2.Click += new System.EventHandler(tlus.buttoii2_Click);/ opeiiFileDialogl/this.opeiiFileDialogl .Filter =图像文件(*jpg/-bmp,*.gif)|*.jpg|*.bmp|*.gif

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号