• 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; } } } }

      

  • 相关阅读:
    如何构造分片报文
    Python趣味入门7:循环与条件的爱恨情仇
    如何使用Javascript开发sliding-nav带滑动条效果的导航插件?
    java获取微信用户信息(含源码,直接改下appid就可以使用了)
    数据库设计工具-PowerDesigner中table视图显示name与code
    typora增加到鼠标右键
    基于springboot整合spring-retry
    idea中提交项目到github及invalid authentication data 404 not found not found问题
    git秘钥问题解析及gitlab配置(Please make sure you have the correct access rights and the repository exists)
    idea中打包跳过test
  • 原文地址:https://www.cnblogs.com/hanke123/p/4919395.html
Copyright © 2020-2023  润新知