zoukankan      html  css  js  c++  java
  • 【转】C# 二进制读写操作实例代码

    一、C#把文件当作二进制流写进数据库
            SqlConnection myconnection = new SqlConnection(strsql);
            myconnection.Open();
            SqlCommand mycommand = new SqlCommand();
            
            FileInfo myfile = new FileInfo("D://**.*");
            FileStream mystream = myfile.OpenRead();
            byte[] mybyte = new byte[myfile.Length];
            mystream.Read(mybyte, 0, Convert.ToInt32(mystream.Length));
            
            strselect = "insert into DF_XML(DT,XML) values(getdate(),@file)";
            mycommand = new SqlCommand(strselect, myconnection);
            SqlParameter myparam0 = new SqlParameter("@file", SqlDbType.Binary);
            myparam0.Value = http://www.ite5e.com/mybyte;
            mycommand.Parameters.Add(myparam0);
            mycommand.ExecuteNonQuery();
            mystream.Close();
            myconnection.Dispose();
            myconnection.Close();

    二、C#把二进制流从进数据库读出并写入文件
            string strsql = "data source=127.0.0.1;Integrated Security=SSPI;Initial Catalog=MYDataBase";
            byte[] mybyte = null;
            FileStream myfs = new FileStream("D://data.xml", FileMode.CreateNew);
            BinaryWriter writefile = new BinaryWriter(myfs);
            string strselect = "select XML from DF_XML where ID=4";
            SqlConnection myconnection = new SqlConnection(strsql);
            myconnection.Open();
            SqlCommand mycommand = new SqlCommand(strselect, myconnection);
            SqlDataReader myreader = mycommand.ExecuteReader();
            if (myreader.Read())
            {
                mybyte = (byte[])myreader[0];
            }
            myreader.Close();
            myconnection.Close();
            writefile.Write(mybyte, 0, mybyte.Length);
            writefile.Close();
            myfs.Close();

  • 相关阅读:
    [.Net 5.0] 笔记
    java计算两个字符串日期的相差天数
    记一个使用fyne-cross编译的坑
    windows gcc 遇到的问题解决
    异步处理在支付环节的应用
    怎么修复 "replaceAll is not a function" JavaScript Error?
    Js生成随机数 生成随机字符串的5种方法
    json-bigint的介绍和使用-解决Javascript的有关大整数问题
    国产操作系统——银河麒麟V10 SP1使用小结
    【转载】 银河麒麟V10系统安装U盘制作
  • 原文地址:https://www.cnblogs.com/xiangniu/p/1982165.html
Copyright © 2011-2022 走看看