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

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.kler.cn/news/315045.html

相关文章:

  • 实时流处理框架(如Flink、Spark Streaming)
  • 系统架构设计师:软件可靠性
  • Flyway 常见问题与解决方案
  • c语言编写程序,找出出现次数最高的数字 数字范围1-1000 时间复杂度不超过O(n)
  • html,css基础知识点笔记(二)
  • VB中的垃圾回收(Garbage Collection)机制
  • 二叉搜索树(附源码C++)
  • 将sqlite3移植到开发板上
  • frp内网穿透部署
  • vue一级、二级路由设计
  • 论文阅读-Demystifying Misconceptions in Social Bots Research
  • Ubuntu20.04 搜索不到任何蓝牙设备
  • 【SpringCloud】优雅实现远程调用 - OpenFeign
  • 鸿蒙【项目打包】- .hap 和 .app;(测试如何安装发的hap包)(应用上架流程)
  • 二二复制模式小程序商城开发
  • Python中的IPython:交互式的Python shell
  • 算法题之宝石与石头
  • 微服务、云计算、分布式开发全套课程课件,来原于企培和多年大厂工作提炼
  • el-form动态标题和输入值,并且最后一个输入框不校验
  • Python 课程16-OpenCV
  • C++门迷宫
  • C++高精度计时方法总结(测试函数运行时间)
  • Axios基本语法和前后端交互
  • 【数据结构】排序算法---计数排序
  • Cpp类和对象(中续)(5)
  • Rasa对话模型——做一个语言助手
  • Qt窗口——QToolBar
  • JVM常见面试题(三):类加载器,双亲委派模型,类装载的执行过程
  • python中ocr图片文字识别样例(二)
  • Spring MVC设置请求头和响应头的Header