[计算机]数据库存取图片.doc

上传人:音乐台 文档编号:1991013 上传时间:2019-01-28 格式:DOC 页数:3 大小:31KB
返回 下载 相关 举报
[计算机]数据库存取图片.doc_第1页
第1页 / 共3页
[计算机]数据库存取图片.doc_第2页
第2页 / 共3页
[计算机]数据库存取图片.doc_第3页
第3页 / 共3页
亲,该文档总共3页,全部预览完了,如果喜欢就下载吧!
资源描述

《[计算机]数据库存取图片.doc》由会员分享,可在线阅读,更多相关《[计算机]数据库存取图片.doc(3页珍藏版)》请在三一文库上搜索。

1、图片文件在SQL Server数据库的存取来源:武汉北大青鸟鲁广校区 发表时间:2009-07-15 在很多时候,我们需要将图片文件存入到SQL Server数据库中,并且在使用的时候将数据库中的图片取出。本文将描述用C#语言来实现这一过程。数据库表结构如果要将图片数据存入SQL Server数据库的表中,我们必须使用SQL Server的image数据类型,在被试验中,我们将使用如下的语句创建数据库表StudentInfo:CREATE TABLE dbo.StudentInfo(ID int IDENTITY(1,1) NOT NULL,Name varchar(50) COLLATE C

2、hinese_PRC_CI_AS NULL,Age int NULL,Sex nchar(10) COLLATE Chinese_PRC_CI_AS NULL,Class varchar(15) COLLATE Chinese_PRC_CI_AS NULL,Hobby varchar(50) COLLATE Chinese_PRC_CI_AS NULL,Picture image NULL)其中字段Picture字段为image数据类型,用来保存学生的照片。图片存入数据库要将图片数据存入到数据库表的image数据类型的字段中,首先需要将图片文件中的数据读入到内存字节中,在将内存字节存入数据库中

3、,具体示例代码如下:private void btnUpload_Click(object sender, EventArgs e)/上传图片到数据库OpenFileDialog openDlg = new OpenFileDialog();openDlg.Filter = 图片文件(*.jpg)|*.jpg;string filePath = ;if (openDlg.ShowDialog() = DialogResult.OK)filePath = openDlg.FileName;this.txtFilePath.Text = filePath;this.picShow.ImageLoc

4、ation = filePath;/打开文件流,用来读取图片文件中的数据FileStream stream = new FileStream(filePath,FileMode.Open,FileAccess.Read);/将文件流中的数据存入内存字节组中byte buffer = new bytestream.Length;stream.Read(buffer,0,(int)stream.Length);stream.Close();try/调用存储图片数据的存取过程string strName = Path.GetFileName(filePath);string connString =

5、 Data Source=.;Initial Catalog=StuDB;Persist Security Info=True;SqlConnection conn = new SqlConnection(connString);conn.Open();SqlCommand cmd = new SqlCommand(proc_UploadPicture, conn);cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add(ID, SqlDbType.Int).Value = 1;cmd.Parameters.Add(Pi

6、cture, SqlDbType.Image).Value = buffer;cmd.Parameters.Add(Ext, SqlDbType.VarChar).Value = strName;cmd.ExecuteNonQuery();conn.Close();catch (Exception ex)MessageBox.Show(ex.Message);存储过程proc_UploadPicture代码如下:Create procedure dbo.proc_UploadPictureID int,Picture imageASupdate StudentInfo set Picture

7、= Picturewhere ID = ID从数据库读取图片要从数据库中获取图片数据,并将图片显示在界面上,需要将数据库中的图片数据读入到内存中,在将内存的数据用位图来格式化,并将位图显示在界面的PictureBox控件中。具体的实例代码如下:private void btnDownload_Click(object sender, EventArgs e)/将数据库中的图片显示出来trybyte imageBytes;string connString = Data Source=.;Initial Catalog=StuDB;Persist Security Info=True;SqlCo

8、nnection conn = new SqlConnection(connString);conn.Open();SqlCommand cmd = new SqlCommand(proc_DownloadPicture, conn);cmd.CommandType = CommandType.StoredProcedure;cmd.Parameters.Add(ID, SqlDbType.Int).Value = 1;SqlDataReader dataReader = cmd.ExecuteReader();if (dataReader.Read()/获取图片数据imageBytes =

9、(byte)dataReaderPicture;/将内存流格式化为位图MemoryStream stream = new MemoryStream(imageBytes);Bitmap bmap = new Bitmap(stream);stream.Close();/将位图显示在界面的PictureBox控件中this.picShow.Image = bmap;dataReader.Close();conn.Close();catch (Exception ex)MessageBox.Show(ex.Message);存储过程proc_DownloadPicture代码如下:Create procedure dbo.proc_DownloadPictureID intasselect Picturefrom StudentInfowhere ID = ID 【武汉北大青鸟培训中心鲁广校区 吕老师指导】

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1