Oracle Database Access Image

Source: Internet
Author: User
Tags oracleconnection

There are two ways for a database to access image information:

1) convert to binary data storage, so first set the Data Type of the corresponding data field to blob.

2) The server sets the path to save the uploaded file. Only the file path is stored in the database.

Here we use the first method to demonstrate how to store and retrieve images. Simple annotations have been made for the use of several classes.

  1. Using system;
  2. Using system. Collections. Generic;
  3. Using system. componentmodel;
  4. Using system. Data;
  5. Using system. drawing;
  6. Using system. text;
  7. Using system. Windows. forms;
  8. Using system. IO;
  9. Using system. Data. oracleclient; // Add reference
  10. Namespace imagedemo
  11. {
  12. Public partial class form1: Form
  13. {
  14. Openfiledialog filedialog;
  15. String connstr = "";
  16. Public form1 ()
  17. {
  18. Connstr = "Data Source = xxx; user id = xxx; Password = xxx ";
  19. Initializecomponent ();
  20. }
  21. /// <Summary>
  22. /// Obtain the file to be uploaded
  23. /// </Summary>
  24. /// <Param name = "sender"> </param>
  25. /// <Param name = "E"> </param>
  26. Private void button2_click (Object sender, eventargs E)
  27. {
  28. Filedialog = new openfiledialog ();
  29. Filedialog. filterindex = 1;
  30. Filedialog. Filter = "JPG files (*. jpg) | *. jpg | all files (*. *) | *.*";
  31. If (filedialog. showdialog () = dialogresult. OK)
  32. {
  33. Txtpath. Text = filedialog. filename;
  34. }
  35. }
  36. /// <Summary>
  37. /// Upload to the database: Save image information to the database
  38. /// </Summary>
  39. /// <Param name = "sender"> </param>
  40. /// <Param name = "E"> </param>
  41. Private void button#click (Object sender, eventargs E)
  42. {
  43. Cursor. Current = cursors. waitcursor;
  44. // Obtain the binary information of the uploaded Image
  45. Byte [] buffer = file. readallbytes (txtpath. Text. Trim ());
  46. Random RD = new random ();
  47. String name = RD. Next (999). tostring ();
  48. String SQL = "insert into ivan_qq_user (ID, name, image) values ('" + guid. newguid (). tostring () + "','" + name + "',: image )";
  49. Using (oracleconnection conn = new oracleconnection (connstr ))
  50. {
  51. Conn. open ();
  52. Oraclecommand cmd = new oraclecommand (SQL, Conn );
  53. // Indicates that the parameter type is blob.
  54. Cmd. Parameters. Add (": Image", oracletype. Blob );
  55. Cmd. Parameters [": Image"]. value = buffer;
  56. Cmd. executenonquery ();
  57. }
  58. Previewimage (name );
  59. Cursor. Current = cursors. default;
  60. MessageBox. Show ("done .....");
  61. }
  62. /// <Summary>
  63. /// Preview image information: Read image information from the database
  64. /// </Summary>
  65. /// <Param name = "name"> </param>
  66. Private void previewimage (string name)
  67. {
  68. Isdapp01.isdapp01 ISD = new imagedemo. isdapp01.isdapp01 ();
  69. String SQL = "select image from ivan_qq_user where name = '" + name + "'";
  70. Using (oracleconnection conn = new oracleconnection (connstr ))
  71. {
  72. Conn. open ();
  73. Oraclecommand cmd = new oraclecommand (SQL, Conn );
  74. // Obtain data information and convert it to binary
  75. Byte [] filedata = cmd. executescalar () as byte [];
  76. Memorystream MS = new memorystream (filedata );
  77. // Convert binary information to image and show
  78. Image image = image. fromstream (MS );
  79. Picturebox1.image = image;
  80. }
  81. }
  82. }
  83. }
Related Article

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.