Read Oracle BLOB field

会有一股神秘感。 提交于 2019-12-07 07:07:08

问题


I try to read an Oracle BLOB field and show the content i a richTextBox. The examples i find with google are almost the same but still i can't get it to work. I know that the BLOB field contains serialized data. This is what i have so far: (the connecetion en reader work fine)

private void button1_Click_1(object sender, EventArgs e)
        {
            //testen of een blob is uit te lezen


            OracleCommand cmd = new OracleCommand();
            cmd.Connection = OraConnection.conn;

            cmd.CommandText = "select id, blobfield from test_table where id = '20ED7EDB-406A-43E8-945B-5E63DFCBA7FF'";
            cmd.CommandType = CommandType.Text;

            OracleDataReader dr = cmd.ExecuteReader();

            dr.Read();
            OracleBlob BLOB1 = dr.GetOracleBlob(1);

            Byte[] Buffer = (Byte[])(dr.GetOracleBlob(1)).Value;

            string lookupValue = System.Text.ASCIIEncoding.ASCII.GetString(Buffer);

            richTextBox1.Text += lookupValue;  //shows: DQStream
            richTextBox1.Text += "";
            richTextBox1.Text += "1";
            richTextBox1.Text += dr.GetOracleBlob(1).Value;  //shows: System.Byte[]
            richTextBox1.Text += "";
        }

回答1:


OracleBlob is a Stream -- it inherits Stream.

OracleBlob b = dr.GetOracleBlob(1); 
var sr = new System.IO.StreamReader(b);
var content = sr.ReadToEnd();

You should be able to get the data this way.

Large blocks of data are typically delivered as a stream.

Oracle Docs: OracleBlob https://docs.oracle.com/cd/B19306_01/win.102/b14307/OracleBlobClass.htm

EDIT If you want to cast it as byte[], then try this:

Byte[] Buffer = (Byte[])(dr.GetOracleBlob(1)).Value; 
var content = new String(Encoding.UTF8.GetChars(Buffer));



回答2:


BLOB means "Binary Large Object" - it is a byte array. You can't assign it directly to a string or append it. Did you mean to use a CLOB perhaps?

Furthermore, RTF isn't serialized data or raw text. It contains font names, codes etc, which means you can't just prepend text to it and get a valid RTF file. An RTF may contain embedded OLE Objects, but that is very rare these days.

For example, the following snippet is a valid RTF document. Appending this to the Text property will display all the special characters. It's not Unicode either, it's plain old ANSI:

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard
This is some {\b bold} text.\par
}

Assuming that the field type is correct, and actually contains an RTF document, you can load its contents into the RTF using the LoadFile(Stream, RichTextBoxStreamType) method. An OracleBlob is a stream, which means you can write:

var blob=dr.GetOracleBlob(1)
richTextBox1.LoadFile(blob,RichTextBoxStreamType.RichText);

Once you load the document you can manipulate it and prepend whatever you want.



来源:https://stackoverflow.com/questions/41923977/read-oracle-blob-field

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