zoukankan      html  css  js  c++  java
  • 图片附件写入SQL和从SQL读取输出

    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;
    using System.Data.SqlClient;
    using System.IO;
    
    namespace imgStream
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void btnUpload_Click(object sender, EventArgs e)
            {
                //上传图片到数据库
                OpenFileDialog openDlg = new OpenFileDialog();
                openDlg.Filter = "图片文件(*.jpg)|*.jpg";
    
                string filePath = "";
                if (openDlg.ShowDialog() == DialogResult.OK)
                {
                    filePath = openDlg.FileName;
                    this.txtFilePath.Text = filePath;
                    this.picShow.ImageLocation = filePath;
    
                    //打开文件流,用来读取图片文件中的数据
                    FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
    
                    //将文件流中的数据存入内存字节组中
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, (int)stream.Length);
    
                    stream.Close();
    
                    try
                    {
                        //调用存储图片数据的存取过程
                        string strName = Path.GetFileName(filePath);
                        string connString = @"Data Source=***SQLEXPRESS;Initial Catalog=newssystem;Persist Security Info=True;User ID=sa;Password=***";
                        SqlConnection conn = new SqlConnection(connString);
                        conn.Open();
                        SqlCommand cmd = new SqlCommand("proc_UploadPicture", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 1;
                        cmd.Parameters.Add("@Age", SqlDbType.Int).Value = 2016;
                        cmd.Parameters.Add("@Picture", SqlDbType.Image).Value = buffer;
                        cmd.Parameters.Add("@Hobby", SqlDbType.VarChar).Value = strName;
                        cmd.ExecuteNonQuery();
    
                        conn.Close();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
    
            private void clear_Click(object sender, EventArgs e)
            {
                this.picShow.ImageLocation = "";
                this.txtFilePath.Text = "";
            }
    
            private void btnDownload_Click(object sender, EventArgs e)
            {
                //将数据库中的图片显示出来
                try
                {
                    byte[] imageBytes;
                    String strName;
                    string connString = @"Data Source=***SQLEXPRESS;Initial Catalog=newssystem;Persist Security Info=True;User ID=sa;Password=***";
                    SqlConnection conn = new SqlConnection(connString);
    
                    conn.Open();
    
                    SqlCommand cmd = new SqlCommand("proc_DownloadPicture", conn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = 24;
    
                    SqlDataReader dataReader = cmd.ExecuteReader();
    
                    if (dataReader.Read())
                    {
                        //获取图片数据
                        imageBytes = (byte[])dataReader["Picture"];
                        strName = (String)dataReader["Hobby"];
                        //将内存流格式化为位图
                        MemoryStream stream = new MemoryStream(imageBytes);
                        Bitmap bmap = new Bitmap(stream);
    
                        stream.Close();
    
                        //将位图显示在界面的PictureBox控件中
                        this.picShow.Image = bmap;
                        this.txtFilePath.Text = strName;
                    }
    
                    dataReader.Close();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
    
        }
    }

    应老板要求,写一个SQL输入和输出图片,PDF等附件,不从目录写入和读取,所以只能另外想办法了。

    网上找了下方法,加工下,可以暂时实现这个功能,另外需要转化为Web Service,再程序调用。

  • 相关阅读:
    Java Synchronized的用法
    静态方法中不能new内部类的实体对象
    android ViewGroup事件分发机制
    安卓设备通过USB接口读取UVC摄像头权限问题
    android View事件分发机制结论
    函数指针与指针函数以及typedef
    GeoHash
    快速排序,C语言实现
    字符串的几个算法
    ANSI C与GNU C
  • 原文地址:https://www.cnblogs.com/iDennis/p/5519045.html
Copyright © 2011-2022 走看看