Oracle
 sql >> Datenbank >  >> RDS >> Oracle

Geben Sie große Inhalte in die Oracle-Datenbank ein

Der einfachste Weg ist die Verwendung von Bind-Variablen. Hier ist ein einfaches Beispiel:

CREATE TABLE clob_table (val CLOB);

void Main()
{
    using (var connection = new OracleConnection("DATA SOURCE=hq_pdb_tcp;PASSWORD=oracle;USER ID=HUSQVIK"))
    {
        using (var transaction = new TransactionScope())
        {
            connection.Open();

            using (var command = connection.CreateCommand())
            {
                command.CommandText = "INSERT INTO clob_table (val) VALUES (EMPTY_CLOB()) RETURNING val INTO :clob";
                var parameter = command.Parameters.Add("clob", OracleDbType.Clob, ParameterDirection.Output);

                command.ExecuteNonQuery();

                var clob = (OracleClob)parameter.Value;

                ChunkFile(@"LargeTextFile.txt", 8060, (b, l) => clob.Append(b, 0, l));
            }

            transaction.Complete();
        }
    }
}

private void ChunkFile(string fileName, int chunkSize, Action<char[], int> chunkAction)
{
    using (var stream = File.OpenText(fileName))
    {
        do
        {
            var buffer = new char[chunkSize];
            var length = stream.Read(buffer, 0, chunkSize);
            if (length == 0)
                return;

            chunkAction(buffer, length);
        }
        while (true);
    }
}