Chinaunix首页 | 论坛 | 博客
  • 博客访问: 304771
  • 博文数量: 93
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 830
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:44
个人简介

一杯茶,一台电脑

文章分类

全部博文(93)

文章存档

2018年(4)

2017年(57)

2016年(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. }


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

上一篇:C# PrctureBox显示图片

下一篇:MySql类

给主人留下些什么吧!~~