当前位置: 首页 > news >正文

C# 访问Access存取图片

图片存入ole字段,看有的代码是获取图片的字节数组转换为base64字符串,存入数据库;显示图片是把base64字符串转换为字节数组再显示;直接存字节数组可能还好一点;

插入的时候用带参数的sql写法比较好;用拼接sql一般会出错;

测试表有三个字段,加载一个图片然后插入记录;

插入以后如下;第5、6条是对的,3、4没插对;如果要png、jpeg、gif等格式都支持,写为 image1.Save(ms, image1.RawFormat);

 

读取并显示如下;

 

插入记录的代码,

            Image image1;MemoryStream ms = new MemoryStream();byte[] arr1 = null; ;if (pictureBox1.Image != null){image1.Save(ms, image1.RawFormat);arr1 = new byte[ms.Length];ms.Position = 0;ms.Read(arr1, 0, (int)ms.Length);//pic1 = Convert.ToBase64String(arr1);}ms.Close();if (button1.Text == "添加"){if (textBox1.Text == ""){MessageBox.Show("名字不能为空!", "添加信息");return;}else{string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + Environment.CurrentDirectory + "\\mytest.mdb'";string query = "INSERT INTO testpic (a1, a2, ppp) VALUES (?,?,?)";using (OleDbConnection conn = new OleDbConnection(connectionString)){using (OleDbCommand cmd = new OleDbCommand(query, conn)){// 这里设置参数值,注意索引对应你SQL语句中参数的位置cmd.Parameters.AddWithValue("?", textBox1.Text);cmd.Parameters.AddWithValue("?", textBox2.Text);cmd.Parameters.AddWithValue("?", arr1);conn.Open();int rowsAffected = cmd.ExecuteNonQuery();conn.Close();}}textBox1.Text = "";textBox2.Text = "";}}

读取显示图片的代码,

	private void button2_Click(object sender, EventArgs e){string sql1 = "select * from testpic where id = " + 6;byte[] buff = null;buff = achelp.GetBufferFromDB(sql1, "ppp");System.IO.MemoryStream picbuf = new System.IO.MemoryStream(buff);Image image = Image.FromStream(picbuf, true);picbuf.Close();pictureBox1.Image = image;}......//返回图片byte[]public byte[] GetBufferFromDB(string strSql, string fieldname){byte[] buff = null;if (conn_str == null){return null;}try{ole_connection.Open();//打开连接if (ole_connection.State == ConnectionState.Closed){return null;}ole_command.CommandText = strSql;ole_command.Connection = ole_connection;ole_reader = ole_command.ExecuteReader(CommandBehavior.Default);if (ole_reader.Read()){buff = (byte[])ole_reader[fieldname];}ole_reader.Close();ole_reader.Dispose();}catch (System.Exception e){//Console.WriteLine(e.ToString());MessageBox.Show(e.Message);}finally{if (ole_connection.State != ConnectionState.Closed){ole_connection.Close();}}return buff;}

要先执行ole_reader.Read(),然后才能取ole_reader[fieldname];
 

http://www.lryc.cn/news/441621.html

相关文章:

  • 正则表达式中常见字符的用法介绍
  • Vue3.0组合式API:依赖注入provide和inject实现跨层组件的通信
  • VSCode中配置C/C++环境
  • vue实现鼠标滚轮控制页面横向滑动
  • 【Git使用】删除Github仓库中的指定文件/文件夹
  • Iptables命令常用命令
  • 前端开发之原型模式
  • 分布式缓存服务Redis版解析与配置方式
  • WordPress建站钩子函数及使用
  • Qt 模型视图(二):模型类QAbstractItemModel
  • 算法打卡 Day41(动态规划)-理论基础 + 斐波那契数 + 爬楼梯 + 使用最小花费爬楼梯
  • 鸿蒙环境服务端签名直传文件到OSS
  • 计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
  • solidwork剪裁实体
  • Junit与Spring Test简单使用
  • Vxe UI vue vxe-table 实现自适应列宽,根据内容自适应列的宽度
  • document.visibilityState 监听浏览器最小化
  • 前端框架对比和选择
  • Linux 进程2
  • WPF入门教学六 Grid布局进阶
  • while循环及简单案例
  • 电子看板实时监控数据可视化助力工厂精细化管理
  • 邮储银行:面向金融行业的移动应用安全风险监测案例
  • ARMxy车辆数据采集Linux智能控制器
  • 7.Java高级编程 多线程
  • MT8370|MTK8370(Genio 510 )安卓核心板参数介绍
  • 动物识别系统Python+卷积神经网络算法+TensorFlow+人工智能+图像识别+计算机毕业设计项目
  • 【STL】priority_queue 基础,应用与操作
  • tasklist命令的应用实例
  • 基于协同过滤算法+PHP的新闻推荐系统