Convert Oracle CLOB data to string in c#

橙三吉。 提交于 2021-01-29 18:09:08

问题


I am trying to run the below query which returns a DataTable object in C#.

select TO_CLOB(BLOB) FROM TestBlobData where BSIZE=5000

When I try to extract the value out of datatable object, a junk value is seen.

DataTable dataTable = RunQuery(QueryMentionedAbove);
var str = dataTable.Rows[0]["BLOB"].ToString();

When I look at Str , I could not see the converted string. The BLOB is actually a JSON string. I cannot use TO_CHAR or TO_NCHAR because my blob size will be greater than 4000. My expectation is to see the JSON string which I stored it as BLOB.

Please help me in converting the CLOB to string in C# code.


回答1:


You can use the function (the reverse of this answer):

CREATE FUNCTION blob_to_clob(
  value            IN BLOB,
  charset_id       IN INTEGER DEFAULT DBMS_LOB.DEFAULT_CSID,
  error_on_warning IN NUMBER  DEFAULT 0
) RETURN CLOB
IS
  result       CLOB;
  dest_offset  INTEGER := 1;
  src_offset   INTEGER := 1;
  lang_context INTEGER := DBMS_LOB.DEFAULT_LANG_CTX;
  warning      INTEGER;
  warning_msg  VARCHAR2(50);
BEGIN
  DBMS_LOB.CreateTemporary(
    lob_loc => result,
    cache   => TRUE
  );

  DBMS_LOB.CONVERTTOCLOB(
    dest_lob     => result,
    src_blob     => value,
    amount       => LENGTH( value ),
    dest_offset  => dest_offset,
    src_offset   => src_offset,
    blob_csid    => charset_id,
    lang_context => lang_context,
    warning      => warning
  );
  
  IF warning != DBMS_LOB.NO_WARNING THEN
    IF warning = DBMS_LOB.WARN_INCONVERTIBLE_CHAR THEN
      warning_msg := 'Warning: Inconvertible character.';
    ELSE
      warning_msg := 'Warning: (' || warning || ') during BLOB conversion.';
    END IF;
    
    IF error_on_warning = 0 THEN
      DBMS_OUTPUT.PUT_LINE( warning_msg );
    ELSE
      RAISE_APPLICATION_ERROR(
        -20567, -- random value between -20000 and -20999
        warning_msg
      );
    END IF;
  END IF;

  RETURN result;
END blob_to_clob;
/

Then, if you have the CLOB_TO_BLOB function from this answer and the data:

CREATE TABLE table_name ( value BLOB );

INSERT INTO table_name (value ) VALUES ( CLOB_TO_BLOB( 'abcdefg' ) );

Then:

SELECT BLOB_TO_CLOB( value ) FROM table_name;

Outputs:

| BLOB_TO_CLOB(VALUE) |
| :------------------ |
| abcdefg             |

db<>fiddle here



来源:https://stackoverflow.com/questions/64558633/convert-oracle-clob-data-to-string-in-c-sharp

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