Oracle
 sql >> Database >  >> RDS >> Oracle

Inserisci contenuti di grandi dimensioni nel database di Oracle

Il modo più semplice è usare la variabile bind. Ecco un esempio di base:

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);
    }
}