这篇文章主要介绍了C#实现文件与二进制互转并存入数据库,本文直接给出代码实例,代码中包含详细注释,需要的朋友可以参考下

//这个方法是浏览文件对象
    private void button1_Click(object sender, EventArgs e)
    {
      //用户打开文件浏览
      using (OpenFileDialog dialog = new OpenFileDialog())
      {
        //只能单选一个文件
        dialog.Multiselect = false;
        //选择一个文件
        if (dialog.ShowDialog() == DialogResult.OK)
        {
          try
          {
            //把选择的文件路径给txtPath
            this.textBox1.Text = dialog.FileName;
          }
          catch (Exception ex)
          {
            //抛出异常
            throw (ex);
          }
        }
      }
    }
 
    //关闭
    private void button3_Click(object sender, EventArgs e)
    {
      this.Close();
    }
 
    //把文件转成二进制流出入数据库
    private void button2_Click(object sender, EventArgs e)
    {
      FileStream fs = new FileStream(textBox1.Text, FileMode.Open);
      BinaryReader br = new BinaryReader(fs);
      Byte[] byData = br.ReadBytes((int)fs.Length);
      fs.Close();
      string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
      SqlConnection myconn = new SqlConnection(conn);
      myconn.Open();
      string str = "insert into pro_table (pro_name,pro_file) values('测试文件',@file)";
      SqlCommand mycomm = new SqlCommand(str, myconn);
      mycomm.Parameters.Add("@file", SqlDbType.Binary, byData.Length);
      mycomm.Parameters["@file"].Value = byData;
      mycomm.ExecuteNonQuery();
      myconn.Close();
    }
 
    //从数据库中把二进制流读出写入还原成文件
    private void button4_Click(object sender, EventArgs e)
    {
      string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
      string str = "select pro_file from pro_table where pro_name='测试文件' ";
      SqlConnection myconn = new SqlConnection(conn);
      SqlDataAdapter sda = new SqlDataAdapter(str, conn);
      DataSet myds = new DataSet();
      myconn.Open();
      sda.Fill(myds);
      myconn.Close();
      Byte[] Files = (Byte[])myds.Tables[0].Rows[0]["pro_file"];
      BinaryWriter bw = new BinaryWriter(File.Open("D:\\2.rdlc",FileMode.OpenOrCreate));
      bw.Write(Files);
      bw.Close();
        
    }

C#实现文件与二进制互转并存入数据库的更多相关文章

  1. 【转载】C#将图片以二进制流的方式存入数据库

    在C#开发应用程序的过程中,图片一般会存放在文件系统中,当然图片也可以二进制的方式存放到数据库中,不过一般不建议存放在数据库中,因为图片占用的空间还是挺大的,特殊情况下可以考虑将图片存在数据.此文将介 ...

  2. C# 文件与二进制互转数据库写入读出

    //这个方法是浏览文件对象 private void button1_Click(object sender, EventArgs e) { //用户打开文件浏览 using (OpenFileDia ...

  3. Springboot(九).多文件上传下载文件(并将url存入数据库表中)

    一.   文件上传 这里我们使用request.getSession().getServletContext().getRealPath("/static")的方式来设置文件的存储 ...

  4. svn 修改文件的二进制或文本标记

    在添加一个文件的时候,svn自动标记成了二进制文件.当时也没在意就提交了.结果修改了之后不能进行svn diff操作,说是二进制文件不能进行diff操作.在网上找了个方法,修改这个文件的标记属性,把这 ...

  5. C# 压缩文件与字节互转

    public class ZipBin { public byte[] bytes; //C#读取压缩文件(将压缩文件转换为二进制 public void GetZipToByte(string in ...

  6. c#常用工具类:文件和二进制转换

    //================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// ...

  7. C# 文件与二进制之间的转换

    /// <summary> /// 工具类:文件与二进制流间的转换 /// </summary> public class FileBinaryConvertHelper { ...

  8. HTML5新特性之文件和二进制数据的操作 Blob对象

    HTML5新特性之文件和二进制数据的操作 1.Blob对象 2.FileList对象 3.File对象 4.FileReader 对象 5.URL对象

  9. hexdump 工具使用 和 .txt 文件的二进制查看

    最近使用txt文件进行数据处理的时候,突然发现txt文件是怎样编码数据的了,它是以二进制来进行存储的吗?为了知道这个情况,我使用hexdump工具进行查看txt文件的二进制形式,并顺道进行学习了hex ...

随机推荐

  1. Open Source VOIP applications, both clients and servers (开源sip server & sip client 和开发库)

    SIP Proxies SBO SIP Proxy Bypass All types of Internet Firewall JAIN-SIP Proxy Mini-SIP-Proxy A very ...

  2. sql join on 与where

    转载:http://www.cnblogs.com/Jessy/p/3525419.html left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join : ...

  3. 面向对象封装 classmethod和staticmethod方法

    接口类 接口类:是规范子类的一个模板,只要接口类中定义的,就应该在子类中实现接口类不能被实例化,它只能被继承支持多继承接口隔离原则:使用多个专门的接口,而不使用单一的总接口.即客户端不应该依赖那些不需 ...

  4. Codeforces Round #395 (Div. 2) D. Timofey and rectangles

    地址:http://codeforces.com/contest/764/problem/D 题目: D. Timofey and rectangles time limit per test 2 s ...

  5. 记录一次代码错误,elastic search的INDEX需要使用小写字母

    Caused by: org.elasticsearch.hadoop.EsHadoopIllegalArgumentException: Cannot determine write shards ...

  6. jQuery图片分组切换焦点图

    在线演示 本地下载

  7. 基于HTML5和SVG的手机菜单动画

    在线演示 本地下载

  8. [POI2012] BEZ-Minimalist Security

    一张n个点m条边的无向图,有点权有边权都是非负,且每条边的权值小于等于两个顶点的权值和,现在要将每个点减一个非负整数使得每条边权等于两个顶点的点权和,问最大修改代价和最小修改代价 思路神的一匹,完全想 ...

  9. zabbix 安装配置

    zabbix的配置文件一般有三种:zabbixserver的配置文件      zabbix_server.confzabbixproxy的配置文件      zabbix_proxy.confzab ...

  10. Jquery 动态生成的元素绑定事件

    使用 on方法 $("#id1").on("click","#id2",function(){}) 这里要注意的是#id1必须是原来就存在的 ...