c#中图片以二进制方式存储到access数据库中-凯发app官方网站

凯发app官方网站-凯发k8官网下载客户端中心 | | 凯发app官方网站-凯发k8官网下载客户端中心
  • 博客访问: 279348
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

(93)

  • (2)
  • (2)
  • (4)
  • (12)
  • (3)
  • (2)
  • (3)
  • (2)
  • (1)
  • (0)
  • (0)
  • (12)
  • (22)
  • (9)
  • (0)
  • (18)
  • (1)
文章存档

(4)

(57)

(32)

相关博文
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·
  • ·

分类: c#/.net

2017-01-22 13:37:10

下面是 access 的示例,sql server 你需要调整一下。字段类型可以设置为binary(max) 或 image

点击(此处)折叠或打开

  1. ///
  2. ///c#以二进制方式存储图片到access数据库中
  3. ///将指定的图片信息存入到 access 数据库
  4. ///
  5. ///图片名
  6. ///图片的 byte
  7. public void insertimagetoaccess(string fimagename, byte[] fimagebinary)
  8. {
  9.     // access 表// 字段// imagename 文本// imagebinary ole 对象
  10.     using(oledbconnection conn =newoledbconnection(“provider=microsoft.jet.oledb.4.0;data source=d:\\x.mdb”)){
  11.         oledbcommand finsertcommand =newoledbcommand();
  12.         finsertcommand.connection = conn;
  13.         finsertcommand.commandtext =”insert into images (imagename, imagebinary) values (?, ?);
  14.         finsertcommand.parameters.add(“@imagename”, oledbtype.varchar).value = fimagename;
  15.         finsertcommand.parameters.add(“@imagebinary”, oledbtype.binary, fimagebinary.length).value = fimagebinary;
  16.         conn.open();
  17.         finsertcommand.executenonquery();
  18.     }
  19. }

  20. ///
  21. ///获取指定图片名的 byte
  22. ///
  23. ///图片名
  24. ///
  25. public byte[] getimagefromaccess(string fimagename)
  26. {
  27.     using(oledbconnection conn =newoledbconnection(“provider=microsoft.jet.oledb.4.0;data source=d:\\x.mdb”)){
  28.         oledbcommand fselectcommand =newoledbcommand();
  29.         fselectcommand.connection = conn;
  30.         fselectcommand.commandtext =”select imagebinary from images where imagename = ?;
  31.         fselectcommand.parameters.add(“@imagename”, oledbtype.varchar).value = fimagename; conn.open();
  32.         object o = fselectcommand.executescalar();
  33.         return(o ==null?null: (byte[])o);
  34.         }
  35. }

  36. private void button1_click(objectsender, eventargs e){
  37.     using(openfiledialog dialog =newopenfiledialog()){
  38.         dialog.filter =”gif|*.gif”;
  39.         dialog.multiselect =false;
  40.         if(dialog.showdialog() == dialogresult.ok){
  41.             stringfimagename = path.getfilenamewithoutextension(dialog.filename);
  42.             filestream fs =newfilestream(dialog.filename, filemode.open, fileaccess.read);
  43.             binaryreader br =newbinaryreader(fs);
  44.             byte[] finputimagebinary = br.readbytes((int)fs.length);
  45.             br.close();
  46.             fs.close();
  47.             // 保存到数据库
  48.             this.insertimagetoaccess(fimagename, finputimagebinary);
  49.             // 从数据库获取图片并显示到 picturebox1
  50.             byte[] foutputimagebinary =this.getimagefromaccess(fimagename);
  51.             memorystream ms =newmemorystream(foutputimagebinary);
  52.             picturebox1.image = image.fromstream(ms);
  53.             }
  54.     }
  55. }


阅读(1816) | 评论(0) | 转发(0) |
0

上一篇:c# prcturebox显示图片

下一篇:

给主人留下些什么吧!~~
")); function link(t){ var href= $(t).attr('href'); href ="?url=" encodeuricomponent(location.href); $(t).attr('href',href); //setcookie("returnouturl", location.href, 60, "/"); }
网站地图