winform把图片存储到数据库

摘要:
1、先在Form中放一个PictureBox控件,再放三个按钮。

1、先在Form中放一个PictureBox控件,再放三个按钮。

winform把图片存储到数据库第1张

2、双击打开按钮,在里面写如下代码:

OpenFileDialog open1 = newOpenFileDialog();
            DialogResult isok =open1.ShowDialog();
            if (isok ==DialogResult.OK)
            {
                string filename =open1.FileName;
                FileStream fs = newFileStream(filename,FileMode.Open);
                Image img =Image.FromStream(fs);
                pictureBox1.Image =img;
                fs.Close();
            }

3、双击存储到数据库按钮,在里面写如下代码:

OpenFileDialog open1 = newOpenFileDialog();
            DialogResult isok =open1.ShowDialog();
            if (isok ==DialogResult.OK)
            {
                //根据路径读取图片,把图片存到数据库
                string filename =open1.FileName;
                FileStream fs = newFileStream(filename, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);//二进制读取器
                byte[] buffer = br.ReadBytes((int)fs.Length);//图片数据进去到buffer中
br.Close();
                fs.Close();
                SqlConnection conn = new SqlConnection("server=.;database=图片;user=sa;pwd=123");
                SqlCommand cmd =conn.CreateCommand();
                cmd.CommandText = "insert into tupian values(@image)";
                cmd.Parameters.Clear();
                cmd.Parameters.Add("@image", buffer);
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }

4、双击读取数据库按钮,在里面写如下代码:

SqlConnection conn = new SqlConnection("server=.;database=图片;user=sa;pwd=123");
            SqlCommand cmd =conn.CreateCommand();
            cmd.CommandText = "select *from tupian where id=1";
            conn.Open();
            SqlDataReader dr =cmd.ExecuteReader();
            while(dr.Read())
            {
                byte[] buffer = (byte[])dr["image"];
                MemoryStream ms = newMemoryStream(buffer);
                Image img =Image.FromStream(ms);
                pictureBox1.Image =img;
            }
            conn.Close();

免责声明:文章转载自《winform把图片存储到数据库》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Java数据解析之JSON(非原创)SAP-ABAP程序开发规范下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

获取DataTable中一列的数据

#region 获取合同号DataRow[] arrRow = new DataRow[ds.Rows.Count];int w = 0;foreach (DataRow row in ds.Rows){arrRow[w] = row;w++;}string[] ary = Array.ConvertAll(arrRow, r => r["cdon...

Navicat数据存放位置和备份数据库路径设置

navicat的数据库存放位置在什么地方?带着这样的疑问,我们去解决问题,navicat是默认安装,mysql也是默认安装,数据库存在默认用户所在的目录下面。 安装MySQL时,请选择“Custom”安装方式。在选择安装目录时,可以同时选择数据安放的位置,很多人此时只是设置了MySQL的安装位置。 对 于已经安装完毕的用户而言,请打开MySQL安装目录中...

pgsql 远程机器无法连接数据库报错处理方法

因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpectedly. This probably means the server terminated abnormally before or while pr...

(02)mongodb 数据库与collection的基本操作

1、查询数据库。 show dbs;或者show databases; 2、创建数据库,必须创建collection,如果只有use shop; 不会创建数据库。 use shop; db.createCollection('goods'); 3、删除数据库。 use shop; db.dropDatabase(); 4、查询collection,先用us...

数据挖掘:理论与算法(导论)

清华大学研究生公开课 数据挖掘是数据科学,是多领域交叉学科:数据挖掘 = 机器学习 + 人工智能 + 模式识别 + 统计学 数据挖掘的广泛应用: Business Intelligence Data Analytics Big Data Decision Support Customer Relationship Management "Educatio...

氚云后台代码小栗子,点击按钮进行计算并同步数据到另外一张表单

protected override void OnSubmit(stringactionName, H3.SmartForm.SmartFormPostValue postValue, H3.SmartForm.SubmitSmartFormResponse response) { if(actionName == "B...