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

加入VIP,免费下载
 

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

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

下载须知

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

版权提示 | 免责声明

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

C从SQL数据库中读取和存入.docx

1、C从SQL数据库中读取和存入C#从SQL 数据库中读取和存入图片本实例主要介绍如何将图片存入数据库。将图片存入数据库,首先要在数据库中建立一张表,将存储图片的字段类型设为Image类型,用FileStream类、BinaryReader把图片读成字节的形式,赋给一个字节数组,然后用ADO.SqlCommand对象的ExecuteNonQuery()方法来把数据保存到数据库中。主要代码如下: private void button1_Click(object sender, EventArgs e) openFileDialog1.Filter = *jpg|*.JPG|*.GIF|*.GIF|

2、*.BMP|*.BMP; if(openFileDialog1.ShowDialog()=DialogResult.OK) string fullpath =openFileDialog1.FileName;/文件路径 FileStream fs = new FileStream(fullpath, FileMode.Open); byte imagebytes =new bytefs.Length; BinaryReader br = new BinaryReader(fs); imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length); /打开

3、数据库 SqlConnection con = new SqlConnection(server=(local);uid=sa;pwd=;database=db_05); con.Open(); SqlCommand com = new SqlCommand(insert into tb_08 values(ImageList),con); com.Parameters.Add(ImageList, SqlDbType.Image); com.ParametersImageList.Value = imagebytes; com.ExecuteNonQuery(); con.Close();

4、本实例主要介绍如何从数据库中把图片读出来。实现本实例主要是利用SqlDataReader从数据库中把Image字段值读出来,赋给一个byte字节数组,然后使用MemoryStream类与Bitmap把图片读取出来。主要代码如下: private void button1_Click(object sender, EventArgs e) byte imagebytes = null; /打开数据库 SqlConnection con = new SqlConnection(server=(local);uid=sa;pwd=;database=db_05); con.Open(); SqlCo

5、mmand com = new SqlCommand(select top 1* from tb_09, con); SqlDataReader dr = com.ExecuteReader(); while (dr.Read() imagebytes = (byte)dr.GetValue(1); dr.Close(); com.Clone(); con.Close(); MemoryStream ms = new MemoryStream(imagebytes); Bitmap bmpt = new Bitmap(ms); pictureBox1.Image = bmpt; 本实例主要介绍

6、如何只允许输入指定图片格式。用OpenFileDialog控件打开图片文件,只要将OpenFileDialog控件的Filter属性指定为特定的图片格式即可。例如,打开.bmp文件的图片,主要代码如下:this.openFileDialog1.Filter = bmp文件(*.bmp)|*.bmp;在用pictureBox控件输入图片时,要想统一图片大小,只需把控件的SizeMode属性值设为StretchImage即可,StretchImage值表示图像的大小将调整为控件的大小。这样,图片的大小就统一了。ASP.NET下上传图片到数据库,并且读出图片 首先在SQL Server中建立一个图片

7、存储的数库表,ImageData Column为图象二进制数据储存字段,ImageContentType Column为图象文件类型记录字段,ImageDescription Column为储蓄图象文件说明字段,ImageSize Column为储存图象文件长度字段,结构如下:CREATE TABLE dbo.ImageStore ( ImageID int IDENTITY (1, 1) NOT NULL , ImageData image NULL , ImageContentType varchar (50) COLLATE Chinese_PRC_CI_AS NULL , ImageD

8、escription varchar (200) COLLATE Chinese_PRC_CI_AS NULL , ImageSize int NULL ) ON PRIMARY TEXTIMAGE_ON PRIMARY*/ /UpLoadImage.aspx程序内容如下:上传图片 上传图片(选择你要上传的图片) 文件说明(添加上传图片说明,如:作者、出处) /-/UpLoadImage.cs程序内容如下:using System;using System.Web;using System.IO;using System.Data;using System.Data.SqlClient;usi

9、ng System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.HtmlControls; namespace UploadImage public class UploadImage : Page protected HtmlInputFile UP_FILE; /HtmlControl、WebControls控件对象protected TextBox txtDescription;protected Label txtMessage;protected Int32 FileLength = 0; /记录文件长度变量

10、protected void Button_Submit(System.Object sender, System.EventArgs e) HttpPostedFile UpFile = UP_FILE.PostedFile; /HttpPostedFile对象,用于读取图象文件属性FileLength = UpFile.ContentLength; /记录文件长度 try if (FileLength = 0) /文件长度为零时 txtMessage.Text = 请你选择你要上传的文件; else Byte FileByteArray = new ByteFileLength; /图象文

11、件临时储存Byte数组 Stream StreamObject = UpFile.InputStream; /建立数据流对像 /读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度 StreamObject.Read(FileByteArray,0,FileLength); /建立SQL Server链接 SqlConnection Con = new SqlConnection(Data Source=Localhost;Initial Catalog=testdb;User ID=sa;Pwd=;); String SqlCmd = I

12、NSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) valueS (Image, ContentType, ImageDescription, ImageSize); SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); CmdObj.Parameters.Add(Image,SqlDbType.Binary, FileLength).value = FileByteArray; CmdObj.Parameters.Add(ContentTy

13、pe, SqlDbType.VarChar,50).value = UpFile.ContentType; /记录文件类型 /把其它单表数据记录上传 CmdObj.Parameters.Add(ImageDescription, SqlDbType.VarChar,200).value = txtDescription.Text; /记录文件长度,读取时使用 CmdObj.Parameters.Add(ImageSize, SqlDbType.BigInt,8).value = UpFile.ContentLength; Con.Open(); CmdObj.ExecuteNonQuery()

14、; Con.Close(); txtMessage.Text = OK!你已经成功上传你的图片;/提示上传成功 catch (Exception ex) txtMessage.Text = ex.Message.ToString();/-/好了,图片已经上传到数据库,现在还要干什么呢?当然是在数据库中读取及显示在Web页中啦,请看以下程序:/ReadImage.aspx程序内容如下:/- /-/ReadImage.cs程序内容如下:using System;using System.Data;using System.Data.SqlClient;using System.Web.UI;usi

15、ng System.Web.UI.WebControls;using System.Web.UI.HtmlControls;namespace ReadImage public class MainDisplay : System.Web.UI.Page public void Page_Load(System.Object sender, System.EventArgs e) int ImgID = Convert.ToInt32(Request.QueryStringImgID); /ImgID为图片ID /建立数据库链接 SqlConnection Con = new SqlConne

16、ction(Data Source=KING;Initial Catalog=testdb;User ID=sa;Pwd=;); String SqlCmd = SELECT * FROM ImageStore WHERE ImageID = ImageID; SqlCommand CmdObj = new SqlCommand(SqlCmd, Con); CmdObj.Parameters.Add(ImageID, SqlDbType.Int).value = ImgID; Con.Open(); SqlDataReader SqlReader = CmdObj.ExecuteReader(

17、); SqlReader.Read(); Response.ContentType = (string)SqlReaderImageContentType;/设定输出文件类型 /输出图象文件二进制数制 Response.OutputStream.Write(byte)SqlReaderImageData, 0, (int)SqlReaderImageSize); Response.End(); Con.Close(); /很简单吧_/-/最后,我们当然要把它在Web页面显示出来啦/ShowImage.hml这个是从数据库读取出来的图象:00000000000000000000000000000

18、000000000000000000000000000000000000000000000000000000000000000000000000000000000将图片插入数据库并使用读取出来的正确方 作者:佚名 文章来源:不详 点击数:68 更新时间:2006-11-7 书写本文是因为今天见到CSDN的首页上一篇存在明显失误的名为“在Asp.Net中从sqlserver检索(retrieve)图片”的文章。不说其错误是因为用其方法确实能从数据库中读取出图片并显示在浏览器,说其失误是因为代码的意图不能被完全的实现,作者也似乎对http协议以及浏览器在处理http数据的流程一知半解。 1、如何出

19、错 以下是这片文章提到的方法: Public Sub Page_Load(sender As Object, e As EventArgs) Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings(ConnectionString) Dim myCommand As New SqlCommand(Select * from Person, myConnection) Try myConnection.Open() Dim myDataReader as SqlDataReader myDataReader

20、= myCommand.ExecuteReader(CommandBehavior.CloseConnection) Do While (myDataReader.Read() Response.ContentType = myDataReader.Item(PersonImageType) Response.BinaryWrite(myDataReader.Item(PersonImage) Loop myConnection.Close() Response.Write(Person info successfully retrieved!) Catch SQLexc As SqlExce

21、ption Response.Write(Read Failed : & SQLexc.ToString() End Try End Sub 显然,编程者是想将Person表中所有的记录中的PersonImage字段所存储的图片一次性地输出到浏览器中,并且在输出成功地情况下在已输出的图片的下方打印出“Person info successfully retrieved!”信息。然而事实上上述代码仅仅能正确地输出第一条记录中的图片。对于浏览器来说,一个http请求获取一个文件(html或者图片),所以以上代码的输出将被作为一个文件(类型依据Response.ContentType = myDat

22、aReader.Item(PersonImageType)定)被浏览器处理。如果http相应的类型是image/jpeg之类的图片,则浏览器使用相应的图片解析功能对这一个图片文件进行解析。因此,上述代码的显示结果只能是第一条记录PersonImage字段的图片。后面的记录输出的图片数据将成为第一张图片的多余数据(此点具有普遍性,但并非绝对,依图片的格式而定),从而后面的“Person info successfully retrieved!”的信息也自然无法本显示出来,因为这些信息已经是图片文件里面的编码了。 2、正确的做法 A、将图片输入到数据库中,以下是一个将图片输入到数据库的代码片断:(

23、完整的DEMO程序见附录一) FileStream fs=File.OpenRead(filePath.Text); byte content=new bytefs.Length; fs.Read(content, 0,content.Length); fs.Close(); SqlConnection conn=new SqlConnection(Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DatabaseImage;Data Source=(local); conn.Open(); SqlComm

24、and comm=conn.CreateCommand(); comm.CommandText=insert into Images(Image, contentType) values(image, contentType); comm.CommandType=CommandType.Text; comm.Parameters.Add(image, SqlDbType.Image).Value=content; comm.Parameters.Add(contentType, SqlDbType.NVarChar).Value= GetContentType(new FileInfo(filePath.Text).Extension.Remove(0,1); if(comm.ExecuteNonQuery()=1) MessageBox.Show(Suc

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

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