asp.net 上傳檔案儲存到資料庫的方法( C#)

來源:互聯網
上載者:User

asp教程.net 上傳檔案儲存到資料庫教程的方法( C#)
C#做一個小的程式,其中用涉及到了照片的存取與顯示,在網上搜尋了很多有關的代碼,但是幾乎沒有完整,大部分只是其中的存取或者顯示代碼,筆者將其整理了一下,形成了一個集照片上傳到資料庫、照片顯示於一體的小模組。 using

System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //選擇按鈕事件處理
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:";
            openFileDialog1.Filter = "圖片檔案 (*.jpg)|*.jpg";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
        }
        //上傳按鈕事件處理
        private void button2_Click(object sender, EventArgs e)
        {
            Save(PhotoToArray(textBox1.Text.ToString()));
        }
        //將圖片資訊轉換成二進位資訊
        private byte[] PhotoToArray(string path)
        {
            FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
            byte[] bufferPhoto = new byte[stream.Length];
            stream.Read(bufferPhoto, 0, Convert.ToInt32(stream.Length));
            stream.Flush();
            stream.Close();
            return bufferPhoto;
        }
        //把二進位的圖片插到資料庫
        private void Save(byte[] image)
        {
            string sql = "insert into Photo(photo_Info) values(@photo)";
            SqlParameter param = new SqlParameter();
            param = new SqlParameter("@photo", SqlDbType.Image);
            param.Value = image;
            SqlCommand commd = new SqlCommand(sql, DBhelper.con);
            commd.Parameters.Add(param);
            try
            {
                DBhelper.con.Open();
                commd.ExecuteNonQuery();
                MessageBox.Show("您已經把圖片成功的插入資料庫!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelper.con.Close();
            }
        }
        //顯示圖片按鈕事件處理
        private void button3_Click(object sender, EventArgs e)
        {
            string strSQL = "Select   [photo_Info]   From   [Photo]   Where   [photo_Id]=(@photo)";
            SqlParameter param = new SqlParameter();
            param = new SqlParameter("@photo", SqlDbType.Int);
            param.Value = comboBox1.Text.ToString();
            SqlCommand cmd = new SqlCommand(strSQL, DBhelper.con);
            cmd.Parameters.Add(param);
            DBhelper.con.Open();
            System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
            try
            {
                reader.Read();
                MemoryStream ms = new MemoryStream((byte[])reader["photo_Info"]);
                System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                this.pictureBox1.Image = image;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                DBhelper.con.Close();
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 這行代碼將資料載入到表“myPhotoDataSet.Photo”中。您可以根據需要移動或移除它。
            this.photoTableAdapter.Fill(this.myPhotoDataSet.Photo);
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.