9.12. Reading and Writing Binary Data with Oracle


You need to read and write binary data from and to an Oracle database.


Use the techniques shown in the following example.

The sample code contains two event handlers:

Read Button.Click

Clears the controls on the form and builds a SQL statement to get the record for the specified ID from the Oracle table TBL0912. A connection is created and a command is built using the SQL statement and executed to build a DataReader. The BLOB is retrieved from the DataReader and displayed in the PictureBox on the form. The CLOB and NCLOB values are retrieved from the DataReader and displayed in text boxes on the form.

Write Button.Click

Gets the ID from the TextBox on the form. A BLOB is retrieved from a user-specified file and loaded into a Byte array. An Oracle DataAdapter is created and a new table is created using the FillSchema( ) command. A CommandBuilder is created from the DataAdapter. A new row is created where the BLOB value is set from the file specified by the user and the CLOB, and NCLOB values are set from the text boxes on the form. The new row is added to the table and the data updated back to the source.

The C# code is shown in Example 9-15.

Example 9-15. File: ReadWriteBinaryDataFromOracleForm.cs

// Namespaces, variables, and constants using System; using System.Configuration; using System.Drawing; using System.Windows.Forms; using System.Text; using System.IO; using System.Data; using System.Data.OracleClient; private OpenFileDialog ...

Get ADO.NET Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.