Odp.net Read and write Oracle BLOB fields

Source: Internet
Author: User
Tags oracleconnection


DEVELOPER:ODP.NET Serving Winning lobs:


Http://www.oracle.com/technetwork/issue-archive/2005/05-nov/o65odpnet-085139.html



Data Provider for. NET Developer ' s Guide:



https://docs.oracle.com/database/121/ODPNT/OracleBlobClass.htm#ODPNT4035



Read the code for a picture file from the Blob field:


 
 
 1        OracleConnection con = new OracleConnection(connStr);
 2         con.Open();
 3 
 4         // statement to get a blob
 5         string sql = "select yz from sysusers where yhdh=‘123‘";
 6 
 7         // create command object
 8         // InitialLOBFetchSize
 9         //  defaults to 0
10         OracleCommand cmd = new OracleCommand(sql, con);
11 
12         // create a datareader
13         using (OracleDataReader dr = cmd.ExecuteReader())
14         {
15             // read the single row result
16             dr.Read();
17             // use typed accessor to retrieve the blob
18             OracleBlob blob = dr.GetOracleBlob(0);
19 
20             // create a memory stream from the blob
21             using (MemoryStream ms = new MemoryStream(blob.Value))
22             {
23                 // set the image property equal to a bitmap
24                 // created from the memory stream
25                 pictureBox1.Image = new Bitmap(ms);
26             }
27         }


Save the file to the Blob field code:


 
1 con. The Open ();
2
3 // utilize transaction processing (must)
4 OracleTransaction transaction = con.begintransaction ();
5 string SQL ="select yz from sysusers where yhdh= '123' FOR UPDATE";
6 OracleCommand CMD = new OracleCommand(SQL, con);
7
8 using (OracleDataReader reader = cmd.executereader ())
9 {
Obtain the first row of data.
11 reader. The Read ();
12 / / Obtain a LOB.
13 OracleBlob = reader.getoracleblob (0);
14 blob. Erase ();
15 // writes the file to the BLOB
16 byte [] Buffer;
17 FileStream fs = new FileStream(@"d:\01.jpg", filemode. Open);
18 using (MemoryStream ms = new MemoryStream())
19 {
Int b;
21 while ((b = fs.readbyte ())! = 1)
22 {
23 Ms. WriteByte ((byte), b);
24}
25 Buffer = Ms. ToArray ();
26}
27
28 // Begin ChunkWrite to improve performance
29 // Index updates occur only once after EndChunkWrite
30 blob. The Position = 0;
31 blob. BeginChunkWrite (); // it is not necessary to enable BeginChunkWrite, only performance related
32 blob. Write (Buffer, 0, Buffer Length); Write a blob field from byte data
33 blob. EndChunkWrite ();
34 blob. The Close ();
35}
36 // commit the transaction
37 transaction.Com MIT ();
38 con. The Close ();





Odp.net Read and write Oracle BLOB fields


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.