public static void WriteLob(string table_name,string table_id,int id,int column,string content)
{
OracleConnection myCn = new OracleConnection(strConn);
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 * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
OracleDataReader reader = myCmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob CLOB = reader.GetOracleLob(column);//column为lob类型所在列
Encoding UTF8 = Encoding.UTF8;//使用UTF-8进行编码
byte[] buffer= UTF8.GetBytes(content);
if(buffer.Length % 2 ==0)
CLOB.Write(buffer, 0, buffer.Length); //这里的参数必须为偶数
else
CLOB.Write(buffer, 0, (buffer.Length - 1));
//CLOB.Position= 0;
myTrans.Commit();
}
}
catch(System.Data.OracleClient.OracleException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}
public static string ReadLob(string table_name,string table_id,int id,int column)
{
OracleConnection myCn = new OracleConnection(strConn);
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 * FROM "+ table_name +" where "+ table_id +" = "+ id +" FOR UPDATE";
OracleDataReader reader = myCmd.ExecuteReader();
using(reader)
{
reader.Read();
OracleLob CLOB = reader.GetOracleLob(column);
int[] content = new int[(int)CLOB.Length];
byte[] buffer = new byte[(int)CLOB.Length];
for(int i = 0;i<(int)CLOB.Length;i++)
{
content = CLOB.ReadByte();
buffer= (byte)content;
}
Encoding utf8 = Encoding.UTF8;
string final = utf8.GetString(buffer);
myTrans.Commit();
return final;
}
}
catch(System.Data.OracleClient.OracleException e)
{
myTrans.Rollback();
throw new Exception(e.Message);
}
finally
{
myCmd.Dispose();
myCn.Close();
}
}