SoFunction
Updated on 2025-03-08

C# implements files and binary transfer and storage into database

C# implements files and binary transfer and storage into database

Updated: June 26, 2015 10:13:45 Submission: junjie
This article mainly introduces the implementation of C# file and binary transfer and storage into the database. This article directly gives code examples, and the code contains detailed comments. Friends who need it can refer to it.
//This method is to browse file objects    private void button1_Click(object sender, EventArgs e)
    {
      //User opens file browsing      using (OpenFileDialog dialog = new OpenFileDialog())
      {
        //Only one file can be selected         = false;
        //Select a file        if (() == )
        {
          try
          {
            //Give the selected file path to txtPath            this. = ;
          }
          catch (Exception ex)
          {
            //throw an exception            throw (ex);
          }
        }
      }
    }

    //closure    private void button3_Click(object sender, EventArgs e)
    {
      ();
    }

    //Convert the file into binary streaming and exiting the database    private void button2_Click(object sender, EventArgs e)
    {
      FileStream fs = new FileStream(, );
      BinaryReader br = new BinaryReader(fs);
      Byte[] byData = ((int));
      ();
      string conn = "server=.;database=testDB;Uid=sa;Pwd=sa ";
      SqlConnection myconn = new SqlConnection(conn);
      ();
      string str = "insert into pro_table (pro_name,pro_file) values('Test file',@file)";
      SqlCommand mycomm = new SqlCommand(str, myconn);
      ("@file", , );
      ["@file"].Value = byData;
      ();
      ();
    }

    //Read the binary stream from the database and write it to restore it to a file    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='Test file' ";
      SqlConnection myconn = new SqlConnection(conn);
      SqlDataAdapter sda = new SqlDataAdapter(str, conn);
      DataSet myds = new DataSet();
      ();
      (myds);
      ();
      Byte[] Files = (Byte[])[0].Rows[0]["pro_file"]; 
      BinaryWriter bw = new BinaryWriter(("D:\\",));
      (Files);
      ();
       
    }
  • C#
  • document
  • Binary Transfer
  • Save to the database

Related Articles

  • Simple steps to develop Windows Forms Application in C#

    This article mainly introduces the simple operation steps for C# to develop Windows Forms applications, which are of good reference value and hope it will be helpful to everyone. Let's take a look with the editor
    2021-04-04
  • C# Fiddler plug-in implements offline browsing function of websites

    This article mainly introduces the principles and methods of the C# Fiddler plug-in to implement the offline browsing function of websites. It has good reference value, let's take a look with the editor below
    2017-02-02
  • C# implements the method of exporting List data to xml files [with demo source code download]

    This article mainly introduces C# to export List data to xml files, involving C#’s related operation techniques for list classes and xml files, and comes with a complete demo source code for readers to download and refer to. Friends who need it can refer to it.
    2016-08-08
  • Sample code for implementing segment tree in C#

    Line segment tree is a data structure commonly used to maintain interval information. This article mainly introduces the example code for C# to implement line segment tree, which has certain reference value. If you are interested, you can learn about it.
    2023-11-11
  • C# How to judge how many percent of a day or a year has passed

    This article mainly introduces the method of C# to determine how many percent of a day or a year has passed. It involves C#'s calculation and judgment skills for time and date. It has certain reference value. Friends who need it can refer to it.
    2015-08-08
  • C# generates pie chart and adds text description example code

    This article mainly introduces C#'s method of generating pie charts and adding text descriptions. It is very practical. Friends who need it can refer to it.
    2014-07-07
  • String StringBuilder Use of StringBuffer class in C#

    This article briefly introduces the usage of the three classes String StringBuilder StringBuffer in C#. If you need it, please refer to it.
    2017-05-05
  • C# WPF uses Clip attribute to realize screenshot box function

    This article mainly introduces in detail how C# WPF uses Clip attributes to implement screenshot box function. The sample code in the article is explained in detail. Interested friends can follow the editor to learn it.
    2024-01-01
  • Unity uses polymorphism to create calculator functions

    This article mainly introduces the function of Unity using polymorphism to create calculator. The sample code in the article is introduced in detail and has certain reference value. Interested friends can refer to it.
    2021-08-08
  • C# uses cmd to call 7z software to compress and decompress files

    This article introduces C# to compress and decompress files through cmd calling 7z software. The example code is introduced in very detailed. It has certain reference value for everyone's study or work. Friends who need it can refer to it.
    2022-04-04

Latest Comments