Enter large content to oracle database

你说的曾经没有我的故事 提交于 2020-02-02 13:39:28

问题


I have a .Net application connecting oracle database and want to enter more than 4000 character to a column in DB.

I tried with CLOB still getting

input string too long.

I am using SQL query to enter data any help.


回答1:


Simplest way is using bind variable. Here is basic example:

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



回答2:


I tried with CLOB still getting input string too long.

That's not true.

From documentation,

A character large object containing single-byte or multibyte characters. Both fixed-width and variable-width character sets are supported, both using the database character set. Maximum size is (4 gigabytes - 1) * (database block size).

For example,

SQL> CREATE TABLE t_clob(col CLOB);

Table created.

SQL> INSERT
  2  INTO t_clob VALUES
  3    (
  4      TO_CLOB(RPAD('*', 4000, '*'))
  5      ||RPAD('*', 4000, '*')
  6      ||RPAD('*', 4000, '*')
  7    );

1 row created.

SQL> SELECT LENGTH(col) FROM t_clob;

LENGTH(COL)
-----------
      12000

Starting with Oracle 12c, the maximum size of VARCHAR2 is now extended to 32767 bytes. By default the parameter MAX_STRING_SIZE is STANDARD which can hold up to 4000 bytes.

SQL> show parameter MAX_STRING_SIZE

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
max_string_size                      string      STANDARD

You could alter the parameter value to EXTENDED and increase the maximum value of VARCHAR2 to 32767 bytes.

There are mainly two important steps:

ALTER SYSTEM SET max_string_size=extended;

@?/rdbms/admin/utl32k


来源:https://stackoverflow.com/questions/33686095/enter-large-content-to-oracle-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!