• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#winform下sql图片二进制存储/读取/显示/写入XML/读

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
c# winform下sql图片二进制存储/读取/显示/写入XML/读取XML显示 winform下: //存储 private void MemoryImage() { string sql = ""; //string conn = "Provider=SQLNCLI;Da ta Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;"; Stream ms; by
  c# winform下sql图片二进制存储/读取/显示/写入XML/读取XML显示 winform下:
        //存储

        private void MemoryImage()
        {
            string sql = "";
            //string conn = "Provider=SQLNCLI;Data Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;";

            Stream ms;
            byte[] picbyte;
            OpenFileDialog fdSelectPic = new OpenFileDialog();
            if (ofdSelectPic.ShowDialog() == DialogResult.OK)
            {
                if ((ms = ofdSelectPic.OpenFile()) != null)
                {
                    picbyte = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));

                    //连接数据库
                    SqlConnection conn = new SqlConnection();
                    conn.ConnectionString = "Data Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;";

                    sql = "Insert into LibraryCover(Cover) values(@UpdateImage)";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.Parameters.Add("@UpdateImage", SqlDbType.VarBinary);
                    cmd.Parameters["@UpdateImage"].Value = picbyte;

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();

                    ms.Close();
                }
            }

            MessageBox.Show("完成!");
        }
        //读取

        private void ShowImage()
        {
            string sql = "";
            sql = "select Cover from LibraryCover where LibID=1";
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = "Data Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;";
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            reader.Read();
            MemoryStream ms = new MemoryStream((byte[])reader["Cover"]);
            Image image = Image.FromStream(ms, true);

            reader.Close();
            conn.Close();
            pictureBox1.Image = image;
        }

        //批量存储

        private void button12_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            FBD.ShowDialog();
            DBImages(FBD.SelectedPath);
        }
        /// <summary>
        /// 图片二进制存SQL库
        /// </summary>
        /// <param name="path">图片所在文件夹</param>
        private void DBImages(string path)
        {
            Stream ms;
            string sql = "";
            byte[] picbyte;
            string FolderName = "";
            string[] Folders;
            string connStr = "Data Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;";
            SqlConnection conn = new SqlConnection(connStr);
            Folders = Directory.GetFiles(path);
            foreach (string folder in Folders)
            {
                if ((ms = File.OpenRead(folder))!= null)
                {
                    picbyte = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));

                    sql = "insert into LibraryCover(ImageName,Cover) values(@ImageName,@Cover)";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandType = CommandType.Text;
                    FolderName = Path.GetFileNameWithoutExtension(folder);
                    cmd.Parameters.Add("@ImageName", SqlDbType.VarChar, 255).Value = FolderName;
                    cmd.Parameters.Add("@Cover", SqlDbType.VarBinary).Value = picbyte;

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    ms.Close();
                }
            }
            MessageBox.Show("存储完成!");
        }

        //批量读取

        private void button13_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            FBD.ShowDialog();
            getImageShow(FBD.SelectedPath);
        }
        /// <summary>
        /// sql库二进制图片显示在存储器上
        /// </summary>
        /// <param name="path">要存储图片的位置</param>
        private void getImageShow(string path)
        {
            string sql = "";
            string conn = "Data Source=192.168.0.9,1433;Database=WebDown;UID=sa;PWD=111122;";
            sql = "select ImageName,Cover from LibraryCover order by LibID";
            SqlDataReader sdr = GetList(conn,sql);
            while (sdr.Read())
            {
                byte[] bytes = (byte[])sdr["Cover"];
                FileStream fs = new FileStream(path+@"\" + sdr["ImageName"] + ".jpg", FileMode.Create, FileAccess.Write);
                fs.Write(bytes, 0, bytes.Length);
                fs.Flush();
                fs.Close();
            }
            MessageBox.Show("完成!");
        }
        public SqlDataReader GetList(string conn, string Sql)
        {
            SqlConnection myConnection = new SqlConnection(conn);
            SqlCommand myCommand = new SqlCommand(Sql, myConnection);

            myConnection.Open();
            SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return result;
        }

        private void getImage()
        {
            int num = 0;
            string path = @"D:\bookpic";
            Stream ms;
            string sql = "";
            byte[] picbyte;
            FileInfo[] Folders;
            string[] files;
            string FilePath = "";
            string connStr = "Data Source=192.168.0.200,1433;Database=MISTemp;UID=sa;PWD=111122;";
            SqlConnection conn = new SqlConnection(connStr);
            DirectoryInfo DI = new DirectoryInfo(path);
            Folders = DI.GetFiles("*.*",SearchOption.AllDirectories);
            foreach (FileInfo file in Folders)
            {
                num++;
                txt_num.Text = num.ToString();
                FilePath = file.FullName.Replace(@"D:\", "").Replace(@"\","/");
                if ((ms = File.OpenRead(file.FullName)) != null)
                {
                    picbyte = new byte[ms.Length];
                    ms.Position = 0;
                    ms.Read(picbyte, 0, Convert.ToInt32(ms.Length));

                    sql = "update shop_books set Cover=@Cover where bookpic='" + modsql(FilePath) + "'";
                    SqlCommand cmd = new SqlCommand(sql, conn);

                    cmd.CommandType = CommandType.Text;
                    cmd.CommandTimeout = 10000;
                    cmd.Parameters.Add("@Cover", SqlDbType.VarBinary).Value = picbyte;

                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    ms.Close();
                }
            }
           
            MessageBox.Show("存储完成!");
        }
        public string modsql(string sql)
        {
            return sql.Replace("'", "''");
        }

//Image写入XML
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

        string MyFile = @"D:\数据处理\image.xml";
        string imageFile = @"G:\zengwei.jpg";
        Stream MyStream;
        SoapFormatter MyFormatter =new SoapFormatter();
        

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MyStream = new FileStream(MyFile, FileMode.Create, FileAccess.Write, FileShare.None);
                MyFormatter.Serialize(MyStream, pictureBox1.Image);
                MyStream.Close();
                MessageBox.Show("完成!");
            }
            catch (Exception ex)
            {
               
            }
        }
//读取XML显示IMAGE
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                MyStream = new FileStream(MyFile, FileMode.Open, FileAccess.Read, FileShare.None);
                pictureBox1.Image = (Bitmap)MyFormatter.Deserialize(MyStream);
                MyStream.Close();
            }
            catch (Exception ex)
            { }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(imageFile);
        }


0 (请您对文章做出评价) 上一篇:int 与 byte[] 的相互转换 关于 int 与 byte[] 的相互转换,Mattias Sjogren 介绍了3种方法

 

///////////////////////////////////////////////////////////

.net教程:winform直接显示二进制数据中的图片

//读取DataSet中以二进制(Image)形式保存的图片
            byte[] byteImage = (byte[])dataSet11.tBGPicture.Rows[2]["PicContent"];

            //转成MemoryStream类型
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteImage, true);
            //stream.Write(byteImage, 0, byteImage.Length);

            //显示图片
            pictureBox1.Image = Image.FromStream(stream);  
           
            //关闭stream
            stream.Close();

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#MongoDB查询所有集合名发布时间:2022-07-14
下一篇:
C#温故而知新学习系列之面向对象编程—自动属性(十一)发布时间:2022-07-14
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap