C # implements files and binaries to be transferred to and stored in the database

Source: Internet
Author: User

This article mainly describes the C # implementation of files and binary transfer and into the database, this article directly gives code examples, the code contains detailed comments, the need for friends can refer to the following

//这个方法是浏览文件对象      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 # implements files and binaries to be transferred to and stored in the database

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.