将文件添加,修改,删除 From 数据库中

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;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //弹出选择文件对话框
        private void buttonShowDia_Click(object sender, EventArgs e)
        {
            try
            {
                //清空的文件路径
                this.textBoxmeetingFilePanth.Text = "";
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "Word格式(*.doc)|*.doc|PPT格式(*.ppt)|*.ppt|Excel格式(*.xls)|*.xls|记事本格式(*.txt)|*.txt";
                ofd.FilterIndex = 0;
                ofd.ReadOnlyChecked = false;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    //显示文件的据对路径
                    this.textBoxmeetingFilePanth.Text = ofd.FileName;
                    //判断文件是否符合要求
                    FileInfo fileInfo = new FileInfo(this.textBoxmeetingFilePanth.Text);
                    if (fileInfo.Extension != ".doc" && fileInfo.Extension != ".ppt" && fileInfo.Extension != ".xls" && fileInfo.Extension != ".txt")
                    {
                        MessageBox.Show("您上传的文件格式不对,请重传");
                        this.textBoxmeetingFilePanth.Text = "";
                        this.textBoxmeetingFilePanth.Text = "";//清空文件据对路径
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("添加文件失败,请检查:" + ex.Message, "消息");
            }
        }

         //定义一个方法获得文件的二进制数据
        private byte[]   getFileByte(string filePath)
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None);
            byte[] filebytes = new byte[fileStream.Length];
            fileStream.Read(filebytes, 0, filebytes.Length);
            return filebytes;
           
            #region[第二种方法活动文件的二进制]
            读文件
            先将传入的文件创建一个文件流
            //using (FileStream fileStream = new FileStream(SendFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
            //{
            //    //读文件流.并返回字符
            //    using (StreamReader streamReader = new StreamReader(fileStream))
            //    {
            //        return Encoding .ASCII .GetString ( streamReader.ReadToEnd());
            //    }
            //}          
            #endregion
        }

        //将数据插入到数据库中
        void buttonInsertFileToDB_Click(object sender, EventArgs e)
        {    
            SqlConnection sqlCon = new SqlConnection(@"Data Source=CHENHAO;Initial Catalog=tempSQL;Integrated Security=True");
            sqlCon.Open();
            string tempSQL = "insert into textWithimage(images) values (@images)";
            SqlCommand sqlCom = new SqlCommand(tempSQL, sqlCon);
            sqlCom.Parameters.Add("@images", SqlDbType.Image ).Value = this.getFileByte(this.textBoxmeetingFilePanth.Text);
            sqlCom.ExecuteNonQuery();
            sqlCon.Close();
       
        }

        //显示数据库中文件
        private void buttonShowFile_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(@"Data Source=CHENHAO;Initial Catalog=tempSQL;Integrated Security=True");
            sqlCon.Open();
            string tempSQL = "select images from textwithimage where ID="+Convert .ToInt32 (this .textBoxID .Text );
            SqlCommand sqlCom = new SqlCommand(tempSQL, sqlCon);
            byte [] fileByte =(byte []) sqlCom.ExecuteScalar();
            //创建一个指定位置的文件
            using (FileStream fileStr = new FileStream(@"C:/Users/Administrator/Desktop/2.doc", FileMode.Create))
            {
                fileStr.Write(fileByte, 0, fileByte.Length);
            }
            sqlCon.Close();
           
        }

        //修改数据库中的文件
        private void buttonAlterFile_Click(object sender, EventArgs e)
        {
            SqlConnection sqlCon = new SqlConnection(@"Data Source=CHENHAO;Initial Catalog=tempSQL;Integrated Security=True");
            sqlCon.Open();
            string tempSQL = "update textWithimage set images=@images where ID="+Convert .ToInt32 (this .textBoxID .Text .ToString ());
            SqlCommand sqlCom = new SqlCommand(tempSQL, sqlCon);
            sqlCom.Parameters.Add("@images", SqlDbType.Image).Value = this.getFileByte(this.textBoxmeetingFilePanth.Text);
            sqlCom.ExecuteNonQuery();
            sqlCon.Close();
        }

       
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值