Operation of accessing pictures (files) in Oracle with ASP.net 2.0

Source: Internet
Author: User
Tags oracleconnection tostring
Asp.net|oracle

Sometimes a picture or file must be stored in a database because of a need, such as security. Of course, in general, especially when the file is relatively large many people do not advocate the file in the form of binary stored in the database, the Oracle file access is sorted as follows (ideas and in SQL As in Server2000, the binary byte stream in which a picture or file is stored in the data, read out the corresponding field in the database to the bytes data, and then output:
1. Create a database table in Toad or Sqlplus.


1CREATE TABLE test_table
2 (
3 ID VARCHAR2 (BYTE),
4 NAME VARCHAR2 (BYTE),
5 PHOTO BLOB
6)
7

2, create a new ASPX page, put a FileUpload control on the page, named Fileup, place two buttons distributed as Btnsave (save), Btnread (read).
3. Execute the code in the Btnsave event to save the picture or file:


Save picture (file) to Oracle
1StringBuilder sbsql = new StringBuilder ("INSERT into test_table (Id,name,photo) VALUES (: Id,:name,:P Hoto)");
2 OracleConnection cn = new OracleConnection (STRCN);
3 OracleCommand cmd = CN. CreateCommand ();
4 Cmd.commandtext = sbsql.tostring ();
5 cmd. Parameters.Add (": ID", Oracletype.varchar, 36). Value = Guid.NewGuid (). ToString ();
6 cmd. Parameters.Add (": Name", Oracletype.varchar, 50). Value = Fileup.filename;;
7 int intlen = FileUp.PostedFile.ContentLength;
8 byte[] pic = new Byte[intlen];
9 fileUp.PostedFile.InputStream.Read (pic, 0, Intlen);
Ten cmd. Parameters.Add (":P hoto", Oracletype.blob). Value = pic;
One try
12 {
CN. Open ();
The cmd. ExecuteNonQuery ();
15}
catch (Exception ex)
17 {
Response.Write (ex. message);
19}
Finally
21 {
CN. Close ();
23}
24

4. The reading method is as follows:


Reading pictures from Oracle (file)
1OracleConnection cn = new OracleConnection (STRCN);
2OracleCommand cmd = CN. CreateCommand ();
3cmd.commandtext = "Select photo from Test_table";
4try
5{
6 CN. Open ();
7 MemoryStream stream = new MemoryStream ();
8 IDataReader reader = cmd. ExecuteReader ();
9 if (reader. Read ())
10 {
One byte[] pic = (byte[]) reader[0];
//byte[] pic = (byte[]) cmd. ExecuteScalar ();
Stream. Write (pic, 0, pic.) Length);
//bitmap Bitmap = new Bitmap (stream);
//response.contenttype = "Image/jpeg";
//bitmap.save (Response.outputstream, imageformat.jpeg);
17//The annotation section can display the picture in IE, instead of downloading the picture,
18//below the method to download the file directly
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename= Demo. JPG ");
Response.BinaryWrite (pic);
Response.End ();
23}
24
25}
26catch (Exception ex)
27{
Response.Write (ex. message);
29}
30finally
31{
CN. Close ();
33}
34



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.