【C#、SQL】C#+SQL 2008 存储和读取多幅图片

如题,通过按钮上传和下载10张图片,存储图片。

话不多说,直接上代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
using System.Collections;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Threading;

namespace ConsoleApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Byte[] img2stream(string imgpath)
        {
            FileStream imgstr = new FileStream(imgpath, FileMode.Open);
            Byte[] buffer = new byte[imgstr.Length];
            imgstr.Read(buffer, 0, buffer.Length);
            Bitmap bmp = new Bitmap(imgstr);
            Image image = bmp;
            pictureBoxGetImage.Image = image; 
            imgstr.Close();   
            return buffer;
        }
        private void UpLoad(string name, string picpath)
        {
            string con =
  "server =KING-SUN\\SUNSQLSERVER;database = sun;uid = sa;pwd = 123456";
           
            SqlConnection conn = new SqlConnection(con);
                //打开数据库连接
                conn.Open();

                //事先在SQL创建 存储过程 uploadpic
                
                SqlCommand cmd = new SqlCommand("uploadpic", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
            Byte[][] buffer = new Byte[11][]; 
            for(int i = 1;i<11;i++)
            { 
                
                string p = picpath + i + ".jpg";
                buffer[i]= img2stream(p);
                cmd.Parameters.Add("@Picture"+i, SqlDbType.Image).Value = buffer[i];
            }    
                int n = cmd.ExecuteNonQuery();
                if (n != 0)
                {
                    MessageBox.Show("插入成功", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBoxGetID.Text= name+":图片导入数据库成功!";
                    
                }
                else
                {
                    MessageBox.Show("插入失败", " 信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBoxGetID.Text = "图片导入数据库失败!";
                    
                }
                conn.Close();
                pictureBoxGetImage.Image = null;              
            
        }
        public void button1_Click(object sender, EventArgs e)
        {
            textBoxGetDescription.Text = null;
             try
            {
            
                string picname = "乔布斯";
                string picpath = @"G:\picsource\";
                UpLoad(picname, picpath);
                 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void Download(string name)
        {
            string con =
  "server =KING-SUN\\SUNSQLSERVER;database = sun;uid = sa;pwd = 123456";
            SqlConnection conn = new SqlConnection(con);
            //打开数据库连接
            conn.Open();
            SqlCommand cmd = new SqlCommand("downloadpic", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Name", SqlDbType.NVarChar).Value = name;
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                for (int i = 1; i < 11; i++)
                {
                    string rea = "RegPicture" + i;
                    byte[] imgbyte = (byte[])reader[rea];
                    MemoryStream ms = new MemoryStream(imgbyte);
                    Bitmap img = new Bitmap(ms);
                    string saven = "db" +name + i + ".jpg";
                    img.Save(saven);
                    ms.Close();
                    pictureBoxGetImage.Image = img;
                    if (img.Size.Height > 0)
                        textBoxGetDescription.Text = "图片导出成功!";
                }
            }
            reader.Close();
            conn.Close();
        }
        public void button2_Click(object sender, EventArgs e)
        {
            textBoxGetID.Text = null;
            try
            {
                string namedown = "乔布斯";
                Download(namedown);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
                textBoxGetDescription.Text = "图片导出失败!";
            }
        }
    }
}

结果截图:


*****************************************************************************************************************************************************************************************

**************************************************************************************************************************************************************************************


**************************************************************************************************************************************************************************************


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值