C # operate the oracle lob field [from the recycle bin]

Source: Internet
Author: User
Tags oracleconnection
Public static void WriteClob (string table_name, string fieldname, string table_id, string id, int column, string content)
{
OracleConnection myCn = OracleModel. GetConnection (DbKey. ORA );
OracleCommand myCmd = new OracleCommand ();
Try
{
MyCn. Open ();
}
Catch (System. Data. OracleClient. OracleException e)
{
Throw new Exception (e. Message );
}
OracleTransaction myTrans = myCn. BeginTransaction ();
Try
{
MyCmd. Connection = myCn;
MyCmd. Transaction = myTrans;
MyCmd. CommandText = "SELECT" + fieldname + "FROM" + table_name + "where" + table_id + "= '" + id + "' for update ";
OracleDataReader reader = myCmd. ExecuteReader ();
Using (reader)
{
Reader. Read ();
OracleLob CLOB = reader. GetOracleLob (column); // The column is the column of the lob type.
Encoding utf16 = Encoding. Unicode;
Byte [] buffer = utf16.GetBytes (content );
If (buffer. Length % 2 = 0)
CLOB. Write (buffer, 0, buffer. Length); // The parameter must be an even number.
Else
CLOB. Write (buffer, 0, (buffer. Length-1 ));
MyTrans. Commit ();
}
}
Catch (System. Data. OracleClient. OracleException e)
{
MyTrans. Rollback ();
}
Finally
{
MyCmd. Dispose ();
MyCn. Close ();
}
}
Public static string ReadClob (string table_name, string fieldname, string table_id, string id, int column)
{
OracleConnection myCn = OracleModel. GetConnection (DbKey. ORA );
OracleCommand myCmd = new OracleCommand ();
Try
{
MyCn. Open ();
}
Catch (System. Data. OracleClient. OracleException e)
{
Throw new Exception (e. Message );
}
OracleTransaction myTrans = myCn. BeginTransaction ();
Try
{
MyCmd. Connection = myCn;
MyCmd. Transaction = myTrans;
MyCmd. CommandText = "SELECT" + fieldname + "FROM" + table_name + "where" + table_id + "= '" + id + "'";
OracleDataReader reader = myCmd. ExecuteReader ();
Using (reader)
{
Reader. Read ();
OracleLob CLOB = reader. GetOracleLob (column );

Byte [] buffer = new byte [(int) CLOB. Length];
CLOB. Read (buffer, 0, (int) CLOB. Length );

Encoding utf16 = Encoding. Unicode;
String final = utf16.GetString (buffer );
MyTrans. Commit ();
Return final;
}
}
Catch (System. Data. OracleClient. OracleException e)
{
MyTrans. Rollback ();
Return "";
}
Finally
{
MyCmd. Dispose ();
MyCn. 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.