C # How to write and read Oracle BLOB fields

Source: Internet
Author: User
1. Write BLOB: String File_name =   " D: \ 20080523.jpg " ;
String Conn_str =   " Provider = oraoledb. oracle.1; Password = agoodjob; persist Security info = true; user id = AMS; Data Source = IMS; extended properties = " ; // The provider must use oraoledb. oracle.1
Oledbconnection CN =   New Oledbconnection (conn_str );
CN. open ();

Oledbcommand cmd =   New Oledbcommand ( " Update blob set photo =? " , CN );
Cmd. Parameters. Add ( " Photo " , Oledbtype. Binary );

Filestream FS =   New Filestream (file_name, filemode. openorcreate, fileaccess. Read );
Byte [] Mydata =   New   Byte [Fs. Length];
FS. Read (mydata, 0 , System. Convert. toint32 (FS. Length ));
FS. Close ();

Cmd. Parameters [ " Photo " ]. Value = Mydata;
Try  
{
Int Result = Cmd. executenonquery ();
If (Result < 1 )
{
Oledbcommand cmd1 =   New Oledbcommand ( " Insert into blob (photo) values (? ) " , CN );
Parameters 1.parameters. Add ( " Photo " , Oledbtype. Binary );
Parameters 1.parameters [ " Photo " ]. Value = Mydata;
Result = Statement 1.executenonquery ();
}
}
Catch (Exception E1)
{
Page. registerstartupscript ( "" , " <Script language = 'javascript '> alert (' " + E1.message + " ') </SCRIPT> " );
}
Finally
{
CN. Close ();
}

2. Read BLOB: String Conn_str =   " Provider = oraoledb. oracle.1; Password = agoodjob; persist Security info = true; user id = AMS; Data Source = IMS; extended properties = " ; // The provider must use oraoledb. oracle.1

Oledbdataadapter da =   New Oledbdataadapter ();
Dataset DS =   New Dataset ();
Oledbconnection Conn =   New Oledbconnection (conn_str );
Oledbcommand cmd =   New Oledbcommand ( " Select photo from web_db.users where user_id = 1 " , Conn );
Da. selectcommand = CMD;
Cmd. Connection. open ();
Da. Fill (DS );

byte [] IMG = New byte [ 0 ];
datarow Dr;
Dr = DS. tables [ 0 ]. rows [ 0 ];

If ( ! DS. tables [ 0 ]. rows [ 0 ] [ " photo " ]. tostring (). equals ( "" ))
{< br> IMG = ( byte []) dr [ " photo " ];
}

Response. contenttype= "Image /*";
Response. binarywrite (IMG );

Conn. Close ();

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.