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

    form1
    
    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)";
    //数据库操作语句 cmd.Parameters.Clear();
    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;
    //定义一个数组 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; } } } }

      

  • 相关阅读:
    mybatis模糊查询语句
    Java中解压文件名有中文的rar包出现乱码问题的解决
    tomcat服务器开启gzip功能的方法
    asp.net 操作word
    asp.net webservice 返回json数据乱码解决方法
    阿里云服务器mysql修改编码问题
    阿里云服务器问题:访问
    EasyUI 在aspx页面显示高度不正常解决办法
    C# 或 JQuery导出Excel
    如何分离数据库 (SQL Server Management Studio)
  • 原文地址:https://www.cnblogs.com/hanke123/p/4919395.html
Copyright © 2011-2022 走看看