zoukankan      html  css  js  c++  java
  • C# 文件与二进制互转数据库写入读出

    1.        //这个方法是浏览文件对象  
    2.        private void button1_Click(object sender, EventArgs e)  
    3.        {  
    4.            //用户打开文件浏览  
    5.            using (OpenFileDialog dialog = new OpenFileDialog())  
    6.            {  
    7.                //只能单选一个文件  
    8.                dialog.Multiselect = false;  
    9.                //选择一个文件  
    10.                if (dialog.ShowDialog() == DialogResult.OK)  
    11.                {  
    12.                    try  
    13.                    {  
    14.                        //把选择的文件路径给txtPath  
    15.                        this.textBox1.Text = dialog.FileName;  
    16.                    }  
    17.                    catch (Exception ex)  
    18.                    {  
    19.                        //抛出异常  
    20.                        throw (ex);  
    21.                    }  
    22.                }  
    23.            }  
    24.        }  
    25.   
    26.        //关闭  
    27.        private void button3_Click(object sender, EventArgs e)  
    28.        {  
    29.            this.Close();  
    30.        }  
    31.   
    32.        //把文件转成二进制流出入数据库  
    33.        private void button2_Click(object sender, EventArgs e)  
    34.        {  
    35.            FileStream fs = new FileStream(textBox1.Text, FileMode.Open);  
    36.            BinaryReader br = new BinaryReader(fs);  
    37.            Byte[] byData = br.ReadBytes((int)fs.Length);  
    38.            fs.Close();  
    39.            string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";  
    40.            SqlConnection myconn = new SqlConnection(conn);  
    41.            myconn.Open();  
    42.            string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";  
    43.            SqlCommand mycomm = new SqlCommand(str, myconn);  
    44.            mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length);  
    45.            mycomm.Parameters["@file"].Value = byData;  
    46.            mycomm.ExecuteNonQuery();  
    47.            myconn.Close();  
    48.        }  
    49.   
    50.        //从数据库中把二进制流读出写入还原成文件  
    51.        private void button4_Click(object sender, EventArgs e)  
    52.        {  
    53.            string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";  
    54.            string str = "select pro_file from pro_table where pro_name='测试文件' ";  
    55.            SqlConnection myconn = new SqlConnection(conn);  
    56.            SqlDataAdapter sda = new SqlDataAdapter(str, conn);  
    57.            DataSet myds = new DataSet();  
    58.            myconn.Open();  
    59.            sda.Fill(myds);  
    60.            myconn.Close();  
    61.            Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"];   
    62.            BinaryWriter bw = new BinaryWriter(File.Open("D:\2.rdlc",FileMode.OpenOrCreate));  
    63.            bw.Write(Files);  
    64.            bw.Close();  
    65.                
    66.        }  

    这个是项目中解决把报表rdlc存入数据库后再次读出还原出来的方法(经过测试 对任何文件对有效[音频文件,压缩包,word文档等])

    这里可能有人会问rdlc是xml的为什么不直接操作XML的存储呢?主要是我们项目中rdlc中可能有带有图片的rdlc格式 所以索性就直接二进制流化处理了。

  • 相关阅读:
    WPF画辐射图
    WPF 获取表格里面的内容
    C# 动态生成Html地图文件
    C#如何关闭指定进程
    oracle EM 打不开 503 |OracleDBConsoleorcl 启动不了
    oracle windows 下修复无监听错误-12541/12514
    Oracle 命令汇总
    oracle 函数 bitand 与 decode
    一.Git 初步扫盲
    修改字段类型
  • 原文地址:https://www.cnblogs.com/jacker1979/p/3829703.html
Copyright © 2011-2022 走看看