C # operate the oracle lob field [from the recycle bin]
Last Update:2018-12-05
Source: Internet
Author: User
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 ();
}
}