zoukankan      html  css  js  c++  java
  • c#中实现存储图片到SQLServer2005

    转载的,但原文出处不知道在哪儿了


    上面是主界面


    上面是显示界面
    数据库为Picture,数据表为Picture,表结构设计如下所示:
    PictureID int 4,          PictureContent Image,        PictureText nvarchar(50)


    下面是主界面的代码

    namespace PictureToDataBase
    {
        public partial class Main : Form
        {
            string fileSaveURL;
    
            public Main()
            {
                InitializeComponent();
            }
    
            private void cmdOpen_Click(object sender, EventArgs e)
            {
                this.openFileDialog.ShowDialog();
    
                string fileURL = this.openFileDialog.FileName;
                this.picView.ImageLocation = fileURL;
                this.fileSaveURL = fileURL;
            }
    
            private void cmdSave_Click(object sender, EventArgs e)
            { //获取图片的二进制流
                FileStream fs = new FileStream(fileSaveURL, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
    
                byte[] photo = br.ReadBytes((int)fs.Length);
    
                br.Close();
                fs.Close();
    
                //把图片写到数据库中
                string conn = @"Data Source=JNITDEV\SQLEXPRESS;Initial Catalog=Picture;Integrated Security=True";
                using (SqlConnection sqlConn = new SqlConnection(conn))
                {
                    SqlCommand sqlComm = new SqlCommand();
    
                    sqlConn.Open();
                    sqlComm.Connection = sqlConn;
                    sqlComm.CommandText = "INSERT INTO Picture (PictureContent, PictureText) VALUES (@Picture,'Test')";
                    sqlComm.CommandType = CommandType.Text;
    
                    sqlComm.Parameters.Add("@Picture", SqlDbType.Image, photo.Length).Value = photo;
    
                    sqlComm.ExecuteNonQuery();
                }
            }
    
            private void cmdShow_Click(object sender, EventArgs e)
            {
                PicShow picShow = new PicShow();
                picShow.Show();
            }
        }
    }
    
    下面是显示界面代码
    
    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 PictureToDataBase
    {
        public partial class PicShow : Form
        {
            int picID;
            int maxID;
            int minID;
            string conn = @"Data Source=JNITDEV\SQLEXPRESS;Initial Catalog=Picture;Integrated Security=True";
    
            public PicShow()
            {
                InitializeComponent();
            }
    
            private void PicShow_Load(object sender, EventArgs e)
            {
                using (SqlConnection sqlConn = new SqlConnection(conn))
                {
                    SqlCommand sqlComm = new SqlCommand();
    
                    sqlConn.Open();
                    sqlComm.Connection = sqlConn;
                    sqlComm.CommandText = "SELECT TOP 1 PictureContent,PictureID FROM Picture ORDER BY PictureID DESC";
                    sqlComm.CommandType = CommandType.Text;
    
                    using (SqlDataReader dr = sqlComm.ExecuteReader())
                    {
                        dr.Read();
                        
                        MemoryStream ms = new MemoryStream((byte[])dr[0]);
                        Image img = Image.FromStream(ms);
    
                        this.picShowPic.Image = img;
                        this.picID = (int)dr[1];
    
                    }
    
                    SetButton();
                }
            }
    
            private void SetButton()
            {
                using (SqlConnection sqlConn = new SqlConnection(conn))
                {
                    SqlCommand sqlComm = new SqlCommand();
    
                    sqlConn.Open();
                    sqlComm.Connection = sqlConn;
                    sqlComm.CommandText = "SELECT MAX(PictureID) AS maxID,MIN(PictureID) AS minID FROM Picture";
                    sqlComm.CommandType = CommandType.Text;
    
                    using (SqlDataReader dr = sqlComm.ExecuteReader())
                    {
                        dr.Read();
                        maxID = (int)dr[0];
                        minID = (int)dr[1];
                    }
                }
    
                this.cmdPreview.Enabled = picID > minID;
                this.cmdNext.Enabled = picID < maxID;
            }
    
            private void cmdNext_Click(object sender, EventArgs e)
            {
                this.picID++;
                LoadPicture();
                SetButton();
            }
    
            private void LoadPicture()
            {
                using (SqlConnection sqlConn = new SqlConnection(conn))
                {
                    SqlCommand sqlComm = new SqlCommand();
    
                    sqlConn.Open();
                    sqlComm.Connection = sqlConn;
                    sqlComm.CommandText = "SELECT PictureContent,PictureID FROM Picture WHERE PictureID = @picID";
                    sqlComm.CommandType = CommandType.Text;
                    sqlComm.Parameters.Add("@picID", SqlDbType.Int).Value = picID;
    
                    using (SqlDataReader dr = sqlComm.ExecuteReader())
                    {
                        dr.Read();//以下把数据库中读出的Image流在图片框中显示出来.
    
                        MemoryStream ms = new MemoryStream((byte[])dr[0]);
                        Image img = Image.FromStream(ms);
    
                        this.picShowPic.Image = img;
                        this.picID = (int)dr[1];
                    }
                }
            }
    
            private void cmdPreview_Click(object sender, EventArgs e)
            {
                this.picID--;
                LoadPicture();
                SetButton();
            }
        }
    }
    


    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/06/30/4576218.html

    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/06/30/4576218.html
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    非凸问题寻优
    非凸问题寻优
    函数的微分表
    函数的微分表
    图形的认识(curve,surface,hypersurface)
    几种Java写webservice的比较
    C#利用lambda在函数中创建内部函数
    uva 387 A Puzzling Problem (回溯)
    HDU 4336 Card Collector 数学期望(容斥原理)
    【ArcGIS 10.2新特性】Portal for ArcGIS新特性
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576218.html
Copyright © 2011-2022 走看看