介绍
这篇文章将为大家详细讲解有关利用c#怎么从数据库中读取图片,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
方式一:
数据库用的是SQL 2008,数据表中存放的是图片的二进制数据,现在把图片以一种图片格式(如jpg)导出,然后存放于指定的文件夹中,实现方式如下:
byte [], bytImg =, (byte []) myDAL.DbHelperSQL.Query (“SELECT  F_Photo 得到myTable WHERE ID=1“) .Tables [0] .Rows [0] [0]; if (bytImg !=, null) { ,MemoryStream 小姐=,new MemoryStream (bytImg); ,Image img =, Image.FromStream(女士); ,img.Save (“D: \ \ me.jpg"); }
方式二:
是windowform程序,数据库已经建好,图像以二进制形式存放在数据库的图像表中,我想把符合查询条件的图像(大量)从数据库中读出,显示在窗形式体上的一个控件(列表视图或imagelist还是图片框?这个不知道那个合适),并保存到选择(或新建)的一个文件夹中
SqlDataAdapter da =, new SqlDataAdapter (“select  *,得到newpicture",,康涅狄格州);//数据库连接,修改一下数据库的操作。 DataSet ds =, new 数据集(); da.Fill (ds,,“pic");//将符合条件的选项保存在数据集的图片表里 ,, string picdotname; string picfilename; int piclength; int 我;//添加新列 DataColumn newcolumn =, ds.Tables [“pic"] .Columns.Add (“pic_url",, typeof (string));//给图片表添加新的一列pic_url,保存你的新写出的图片路径 for (小姐:=,0;,小姐:& lt;, Convert.ToInt16 (ds.Tables [“pic"] .Rows.Count);,我+ +) {=,,picdotname  ds.Tables [“pic"] .Rows[我][“pic_dot"] .ToString();//图片的拓展名,你数据库要有这一列,如jpg=,,piclength  Convert.ToInt32 (ds.Tables [“pic"] .Rows[我][“pic_length"]);//数据流的长度=,,picfilename  Server.MapPath(“新建的文件夹名/?,+,“添加图片名“+,“干净,+,picdotname; ,FileStream fs =, new 文件流(picfilename, FileMode.Create,, FileAccess.Write); ,byte [], piccontent =, new 字节(piclength); ,piccontent =, (byte []) ds.Tables [“pic"] .Rows[我](“pic_content"); ,fs.Write (piccontent, 0,, piclength); ,fs.Close();//读出数据流写成图片 ,//最后把表绑定到控件上。 (“pic"), ds.Tables .Rows[我][“pic_url"],=,“临时/temp", +, i.ToString(), +,“干净,+,picdotname;//意思给表图片的第行,pic_url列里添加文件的路径值。 }//数据源,=,ds.Tables (“pic");//数据绑定
关于利用c#怎么从数据库中读取图片就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。