zoukankan      html  css  js  c++  java
  • 存取数据库图片

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.SqlClient;
     
    namespace WindowsFormsApplication9
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
               // WebRequest wr = HttpWebRequest.Create("http://www.itnba.com");
     
                
     
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*";
                DialogResult isok = openFileDialog1.ShowDialog();
     
                if (isok == DialogResult.OK)
                {
                    //开始使用流读取
                    FileStream fs = new FileStream(openFileDialog1.FileName,FileMode.Open,FileAccess.Read);
                    //使用流读取器,把文件流对象中的内容读取出来,转换成字符串或者其他对应的数据
                    BinaryReader br = new BinaryReader(fs);//二进制读取器
                    byte[] imgbytes = br.ReadBytes((int)fs.Length);//将流中数据读取成byte数组存入数组变量中
     
                    //连接数据库,新增数据
                    SqlConnection conn = new SqlConnection("server=.;database=aaa;user=sa;pwd=123");
                    SqlCommand cmd = conn.CreateCommand();
                    cmd.CommandText = "insert into imgtable values(@img)";<br>                //数据库操作语句
                    cmd.Parameters.Clear();<br>
                    cmd.Parameters.Add("@img",imgbytes);
                    conn.Open();
     
                    cmd.ExecuteNonQuery();
     
                    conn.Close();
     
     
                }
            }
     
            private void button2_Click(object sender, EventArgs e)
            {
                //连接数据库,新增数据
                SqlConnection conn = new SqlConnection("server=.;database=aaa;user=sa;pwd=123");
     
                SqlCommand cmd = conn.CreateCommand();
     
                cmd.CommandText = "select top 1 *from imgtable order by ids desc";
     
                conn.Open();
                 //ExecuteReader 数据库读取用的
                SqlDataReader dr = cmd.ExecuteReader();
                byte[] imgbytes = null;<br>          //定义一个数组 
                if (dr.Read())
                {
                   imgbytes  = (byte[])dr["images"];
                }
     
                conn.Close();
                //如何把二进制数据,转换成一个Image类型,来给piceturebox赋值
                //内存流
                MemoryStream ms = new MemoryStream(imgbytes);
                Image img = Image.FromStream(ms);
     
                pictureBox2.Image = img;
     
     
               // Image img = Image.
            }
     
            private void button3_Click(object sender, EventArgs e)
            {
                openFileDialog1.Filter = "jpg图|*.jpg|png图|*.png|gif图|*.gif|所有文件|*.*";
                DialogResult isok = openFileDialog1.ShowDialog();
     
                if (isok == DialogResult.OK)
                {
                    Image img = Image.FromFile(openFileDialog1.FileName);
                    pictureBox1.Image = img;
                }
            }
        }
    }<br>
  • 相关阅读:
    QT 应用程序关闭某个窗口时,关闭打开的所有其他窗口并退出程序 【转】
    XP配置DCOM服务【转】
    Android最佳性能实践(二)——分析内存的使用情况
    Android最佳性能实践(一)——合理管理内存
    快速实现 ListView下拉,图片放大刷新操作
    Android布局实现圆角边框
    android 自定义文字跑马灯 支持拖拽,按住停止滚动,自定义速度
    Android NDK 环境搭建 + 测试例程
    Android -- 桌面悬浮,仿360
    android-async-http AsyncHttpClient介绍
  • 原文地址:https://www.cnblogs.com/zxm1002/p/4939732.html
Copyright © 2011-2022 走看看