以下是我的两种实现方式:
方式一:
该方式进行的操作是,将一大于4Kb的txt文件写入Clob对象或者从该Clob字段读取出来并创建一个txt文件来保存。这些操作通过分段读取来实现。
// Lock the result set using the "FOR UPDATE" clause
cmd.CommandText = "SELECT " + Clobcol + " FROM " + Table + " FOR UPDATE";
reader = cmd.ExecuteReader();
reader.Read();
clob = reader.GetOracleClob(0);
clob.Erase();
string content = string.Empty;
using (StreamReader sr = new StreamReader(srcPath))
{
content = sr.ReadToEnd();
}
char[] buffer = new char[BufferLength];
// Save the content into memory
using (MemoryStream ms = new MemoryStream(Encoding.GetEncoding("UTF-8").GetBytes(content)))
{
int readCounts = 0;
using (StreamReader sr = new StreamReader(ms))
{
while (sr.Peek() > -1)
{
readCounts = sr.Read(buffer, 0, BufferLength);
clob.Write(buffer, 0, readCounts);
}
clob.Flush();
}
}
Console.WriteLine("Save Massive data Succeeded!");
// Commit transaction
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cmd.Dispose();
connection.Close();
connection.Dispose();
}
}
public void FetchMassiveData()
{
// Declare Oracle objects
OracleConnection connection = new OracleConnection(ConnStr);
OracleCommand cmd = new OracleCommand("", connection);
OracleTransaction transaction;
OracleDataReader reader;
OracleClob clob;
try
{
connection.Open();
Console.WriteLine("Connected to database.../n");
// Start a transaction
transaction = connection.BeginTransaction();
cmd.CommandText = "SELECT " + Clobcol + " FROM " + Table;
reader = cmd.ExecuteReader();
reader.Read();
clob = reader.GetOracleClob(0);
int readCounts;
char[] buffer = new char[BufferLength];
using (FileStream fs = new FileStream(desPath, FileMode.Create))
{
StreamWriter sw = new StreamWriter(fs);
while ((readCounts = clob.Read(buffer, 0, BufferLength)) > 0)
{
sw.Write(buffer, 0, readCounts);
}
sw.Flush();
sw.Close();
}
Console.WriteLine("Fetch Massive data Succeeded!");
// Commit transaction
transaction.Commit();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cmd.Dispose