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
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    第四章 生命周期函数-- 34 生命周期函数-组件运行和销毁阶段的钩子函数
    第三章 指令-- 31 指令-定义私有指令,32 指令-指令函数的简写形式
    第三章 指令-- 30 指令-使用钩子函数的第二个binding参数拿到传递的值
    第三章 指令-- 29 指令-自定义全局指令让文本框获取焦点
    bindActionCreators作用
    Object.prototype.toString.call()
    C语言基础 (4) 原码反码补码与数据类型
    React中的setState(obj)
    清除行内元素中间的空白
    分清encodeURIComponent encodeURI 和 escape
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576218.html
Copyright © 2011-2022 走看看