C#从SQL 数据库中读取和存入图片

上传人:壹****1 文档编号:561308044 上传时间:2024-01-19 格式:DOC 页数:20 大小:76.50KB
返回 下载 相关 举报
C#从SQL 数据库中读取和存入图片_第1页
第1页 / 共20页
C#从SQL 数据库中读取和存入图片_第2页
第2页 / 共20页
C#从SQL 数据库中读取和存入图片_第3页
第3页 / 共20页
C#从SQL 数据库中读取和存入图片_第4页
第4页 / 共20页
C#从SQL 数据库中读取和存入图片_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《C#从SQL 数据库中读取和存入图片》由会员分享,可在线阅读,更多相关《C#从SQL 数据库中读取和存入图片(20页珍藏版)》请在金锄头文库上搜索。

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

2、f(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); /打开数据库 SqlConnect

3、ion 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(); SqlCommand com = ne

5、w 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中建立一个图片存储的数库表,ImageDa

7、ta 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 , ImageDescription var

8、char (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;using System.Web.

9、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; /记录文件长度变量 protected void

10、 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; /图象文件临时储存Byte数组 Stream StreamObject = UpFile.InputStream; /建立数据流对像 /读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度 StreamObject.Read(FileByteArray,0,FileLength)

展开阅读全文
相关资源
相关搜索

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

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