C # file and binary mutual transfer database write read-out

Source: Internet
Author: User

  1. This method is to browse the file object
  2. private void button1_click (object sender, EventArgs e)
  3. {
  4. //user open File Browse
  5. using (OpenFileDialog Dialog = new OpenFileDialog ())
  6. {
  7. //Only one file can be selected
  8. Dialog.  MultiSelect = false;
  9. //Select a file
  10. if (dialog. ShowDialog () = = DialogResult.OK)
  11. {
  12. Try
  13. {
  14. //Take the selected file path to Txtpath
  15. this.textBox1.Text = dialog.  FileName;
  16. }
  17. catch (Exception ex)
  18. {
  19. //Throw exception
  20. throw (ex);
  21. }
  22. }
  23. }
  24. }
  25. //Off
  26. private void button3_click (object sender, EventArgs e)
  27. {
  28. This .  Close ();
  29. }
  30. //Convert files into binary streams into the database
  31. private void button2_click (object sender, EventArgs e)
  32. {
  33. FileStream fs = new FileStream (TextBox1.Text, FileMode.Open);
  34. BinaryReader br = new BinaryReader (FS);
  35. byte[] Bydata = br. Readbytes ((int) fs.  Length);
  36. Fs. Close ();
  37. String conn = "server=.; Database=testdb; Uid=sa;  Pwd=sa ";
  38. SqlConnection myconn = new SqlConnection (conn);
  39. MyConn. Open ();
  40. string str = "INSERT into pro_table (pro_name,pro_file) VALUES (' Test file ', @file)";
  41. SqlCommand Mycomm = new SqlCommand (str, myconn);
  42. Mycomm.  Parameters.Add ("@file", Sqldbtype.binary, Bydata.length);
  43. Mycomm. parameters["@file"].  Value = Bydata;
  44. Mycomm. ExecuteNonQuery ();
  45. MyConn. Close ();
  46. }
  47. //From the database to read the binary stream to write to restore the file
  48. private void button4_click (object sender, EventArgs e)
  49. {
  50. String conn = "server=.; Database=testdb; Uid=sa;  Pwd=sa ";
  51. string str = "Select Pro_file from pro_table where pro_name= ' test file '";
  52. SqlConnection myconn = new SqlConnection (conn);
  53. SqlDataAdapter SDA = New SqlDataAdapter (STR, conn);
  54. DataSet myds = new DataSet ();
  55. MyConn. Open ();
  56. Sda. Fill (myds);
  57. MyConn. Close ();
  58. Byte[] Files = (byte[]) myds. Tables[0].   rows[0]["Pro_file"];
  59. BinaryWriter bw = new BinaryWriter (File.Open ("D:\\2.RDLC", FileMode.OpenOrCreate));
  60. Bw. Write (Files);
  61. Bw. Close ();
  62. }

This is the project to resolve the report rdlc into the database once again read out the Restore method (after testing to any file on the valid [audio files, compressed packages, Word documents, etc.])

Someone here might ask that RDLC is XML, why not just manipulate XML storage? In our project, it is possible to have the RDLC format with pictures in the RDLC, so we simply make a direct binary streaming process.

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.