ImageVerifierCode 换一换
格式:DOCX , 页数:25 ,大小:29.45KB ,
资源ID:4801973      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/4801973.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(向SQL Server数据库添加图片.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

向SQL Server数据库添加图片.docx

1、向SQL Server数据库添加图片程序代码:下面的代码实现向SQLServer数据库添加图片和文字的功能。首先,在SQL查询分析器中执行下面的语句,以创建表和存储过程。DropTablePersonGoCreateTablePerson(PersonIDIntIdentity,PersonEmailVarchar(255),PersonNameVarchar(255),PersonSexChar(1),PersonDOBDateTime,PersonImageImage,PersonImageTypeVarchar(255)DropProcsp_person_ispGoCreateProcs

2、p_person_ispPersonEmailVarchar(255),PersonNameVarchar(255),PersonSexChar(1),PersonDOBDateTime,PersonImageImage,PersonImageTypeVarchar(255)AsBeginInsertintoPerson(PersonEmail,PersonName,PersonSex,PersonDOB,PersonImage,PersonImageType)Values(PersonEmail,PersonName,PersonSex,PersonDOB,PersonImage,Perso

3、nImageType)EndGo下面就是完整的代码,拷贝即可运行:向SQLServer插入图片PublicSubAddPerson(senderAsObject,eAsEventArgs)DimintImageSizeAsInt64DimstrImageTypeAsStringDimImageStreamAsStream获得图片的大小intImageSize=PersonImage.PostedFile.ContentLength获得图片类型strImageType=PersonImage.PostedFile.ContentType读取图片ImageStream=PersonImage.Po

4、stedFile.InputStreamDimImageContent(intImageSize)AsByteDimintStatusAsIntegerintStatus=ImageStream.Read(ImageContent,0,intImageSize)创建Connection和Command对象DimstrCnnAsString=DataSource=.;InitialCatalog=mxh;UserId=sa;Password=;DimmyConnectionAsNewSqlConnection(strCnn)DimmyCommandAsNewSqlCommand(sp_perso

5、n_isp,myConnection)使用存储过程myCommand.CommandType=CommandType.StoredProcedure向存储过程添加参数DimprmEmailAsNewSqlParameter(PersonEmail,SqlDbType.VarChar,255)prmEmail.Value=txtPersonEmail.TextmyCommand.Parameters.Add(prmEmail)DimprmNameAsNewSqlParameter(PersonName,SqlDbType.VarChar,255)prmName.Value=txtPersonNa

6、me.TextmyCommand.Parameters.Add(prmName)DimprmSexAsNewSqlParameter(PersonSex,SqlDbType.Char,1)IfsexMale.CheckedThenprmSex.Value=MElseprmSex.Value=FEndIfmyCommand.Parameters.Add(prmSex)DimprmPersonDOBAsNewSqlParameter(PersonDOB,SqlDbType.DateTime)prmPersonDOB.Value=txtPersonDob.TextmyCommand.Paramete

7、rs.Add(prmPersonDOB)DimprmPersonImageAsNewSqlParameter(PersonImage,SqlDbType.Image)prmPersonImage.Value=ImageContentmyCommand.Parameters.Add(prmPersonImage)DimprmPersonImageTypeAsNewSqlParameter(PersonImageType,SqlDbType.VarChar,255)prmPersonImageType.Value=strImageTypemyCommand.Parameters.Add(prmPe

8、rsonImageType)TrymyConnection.Open()myCommand.ExecuteNonQuery()myConnection.Close()Response.Write(添加成功!)CatchSQLexcAsSqlExceptionResponse.Write(添加失败,原因:&SQLexc.ToString()EndTryEndSub本日志由 迈克老猫 于 2005-01-06 10:43 PM 编辑引用通告地址 (0):收藏此页迈克老猫 于 2004-06-09 05:16 PM 发表评论:SQLServer提供了一个特别的数据类型:image,它是一个包含.SQ

9、LServer提供了一个特别的数据类型:image,它是一个包含binary数据的类型。下边这个例子就向你展示了如何将文本或照片放入到数据库中的办法。在这篇文章中我们要看到如何在SQLServer中存储和读取图片。1、建立一个表:在SQLSERVER中建立这样结构的一个表:列名类型目的IDInteger主键IDIMGTITLEVarchar(50)图片的标题IMGTYPEVarchar(50)图片类型.ASP.NET要以辨认的类型IMGDATAImage用于存储二进制数据2、存储图片到SQLSERVER数据库中为了能存储到表中,你首先要上传它们到你的WEB服务器上,你可以开发一个webform

10、,它用来将客户端中TextBoxwebcontrol中的图片入到你的WEB服务器上来。将你的encType属性设置为:myltipart/formdata.Streamimgdatastream=File1.PostedFile.InputStream;intimgdatalen=File1.PostedFile.ContentLength;stringimgtype=File1.PostedFile.ContentType;stringimgtitle=TextBox1.Text;byteimgdata=newbyteimgdatalen;intn=imgdatastream.Read(im

11、gdata,0,imgdatalen);stringconnstr=(NameValueCollection)Context.GetConfig(appSettings)connstr;SqlConnectionconnection=newSqlConnection(connstr);SqlCommandcommand=newSqlCommand(INSERTINTOImageStore(imgtitle,imgtype,imgdata)VALUES(imgtitle,imgtype,imgdata),connection);SqlParameterparamTitle=newSqlParam

12、eter(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.Valu

13、e=imgtype;command.Parameters.Add(paramType);connection.Open();intnumRowsAffected=command.ExecuteNonQuery();connection.Close();3、从数据库中恢复读取现在让我们来从SQLServer中读取我们放入的数据吧!我们将要输出图片到你的浏览器上,你也可以将它存放到你要的位置。privatevoidPage_Load(objectsender,System.EventArgse)stringimgid=Request.QueryStringimgid;stringconnstr=(

14、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.Conte

15、ntType=drimgtype.ToString();Response.BinaryWrite(byte)drimgdata);connection.Close();要注意的是Response.BinaryWrite而不是Response.Write.下面给大家一个用于C#Winform的存入、读取程序。其中不同请大家自己比较!(为了方便起见,我将数据库字段简化为二个:imgtitle和imgdata。usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.W

16、indows.Forms;usingSystem.Data;usingSystem.IO;usingSystem.Data.SqlClient;namespaceWindowsApplication21/summary/Form1的摘要说明。/summarypublicclassForm1:System.Windows.Forms.FormprivateSystem.Windows.Forms.Buttonbutton1;/summary/必需的设计器变量。/summaryprivateSystem.ComponentModel.Containercomponents=null;private

17、stringConnectionString=IntegratedSecurity=SSPI;InitialCatalog=;DataSource=localhost;privateSqlConnectionconn=null;privateSqlCommandcmd=null;privateSystem.Windows.Forms.Buttonbutton2;privateSystem.Windows.Forms.PictureBoxpic1;privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;privatestringsql=

18、null;privateSystem.Windows.Forms.Labellabel2;privatestringnowId=null;publicForm1()/Windows窗体设计器支持所必需的/InitializeComponent();conn=newSqlConnection(ConnectionString);/TODO:在InitializeComponent调用后添加任何构造函数代码/summary/清理所有正在使用的资源。/summaryprotectedoverridevoidDispose(booldisposing)if(conn.State=ConnectionS

19、tate.Open)conn.Close();if(disposing)if(components!=null)components.Dispose();base.Dispose(disposing);#regionWindowsFormDesignergeneratedcode/summary/设计器支持所需的方法-不要使用代码编辑器修改/此方法的内容。/summaryprivatevoidInitializeComponent()this.button1=newSystem.Windows.Forms.Button();this.pic1=newSystem.Windows.Forms.PictureBox();this.button2=newSystem.Windows.Forms.Button();this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();this.label2=newSystem.Windows.Forms.Label();this.SuspendLayout();/

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1