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.
- Using system;
- Using system. Collections. Generic;
- Using system. componentmodel;
- Using system. Data;
- Using system. drawing;
- Using system. text;
- Using system. Windows. forms;
- Using system. IO;
- Using system. Data. oracleclient; // Add reference
- Namespace imagedemo
- {
- Public partial class form1: Form
- {
- Openfiledialog filedialog;
- String connstr = "";
- Public form1 ()
- {
- Connstr = "Data Source = xxx; user id = xxx; Password = xxx ";
- Initializecomponent ();
- }
- /// <Summary>
- /// Obtain the file to be uploaded
- /// </Summary>
- /// <Param name = "sender"> </param>
- /// <Param name = "E"> </param>
- Private void button2_click (Object sender, eventargs E)
- {
- Filedialog = new openfiledialog ();
- Filedialog. filterindex = 1;
- Filedialog. Filter = "JPG files (*. jpg) | *. jpg | all files (*. *) | *.*";
- If (filedialog. showdialog () = dialogresult. OK)
- {
- Txtpath. Text = filedialog. filename;
- }
- }
- /// <Summary>
- /// Upload to the database: Save image information to the database
- /// </Summary>
- /// <Param name = "sender"> </param>
- /// <Param name = "E"> </param>
- Private void button#click (Object sender, eventargs E)
- {
- Cursor. Current = cursors. waitcursor;
- // Obtain the binary information of the uploaded Image
- Byte [] buffer = file. readallbytes (txtpath. Text. Trim ());
- Random RD = new random ();
- String name = RD. Next (999). tostring ();
- String SQL = "insert into ivan_qq_user (ID, name, image) values ('" + guid. newguid (). tostring () + "','" + name + "',: image )";
- Using (oracleconnection conn = new oracleconnection (connstr ))
- {
- Conn. open ();
- Oraclecommand cmd = new oraclecommand (SQL, Conn );
- // Indicates that the parameter type is blob.
- Cmd. Parameters. Add (": Image", oracletype. Blob );
- Cmd. Parameters [": Image"]. value = buffer;
- Cmd. executenonquery ();
- }
- Previewimage (name );
- Cursor. Current = cursors. default;
- MessageBox. Show ("done .....");
- }
- /// <Summary>
- /// Preview image information: Read image information from the database
- /// </Summary>
- /// <Param name = "name"> </param>
- Private void previewimage (string name)
- {
- Isdapp01.isdapp01 ISD = new imagedemo. isdapp01.isdapp01 ();
- String SQL = "select image from ivan_qq_user where name = '" + name + "'";
- Using (oracleconnection conn = new oracleconnection (connstr ))
- {
- Conn. open ();
- Oraclecommand cmd = new oraclecommand (SQL, Conn );
- // Obtain data information and convert it to binary
- Byte [] filedata = cmd. executescalar () as byte [];
- Memorystream MS = new memorystream (filedata );
- // Convert binary information to image and show
- Image image = image. fromstream (MS );
- Picturebox1.image = image;
- }
- }
- }
- }