9.12. Reading and Writing Binary Data with Oracle
Problem
You need to read and write binary data from and to an Oracle database.
Solution
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
commandis built using the SQL statement and executed to build aDataReader. TheBLOBis retrieved from theDataReaderand displayed in thePictureBoxon the form. TheCLOBandNCLOBvalues are retrieved from theDataReaderand displayed in text boxes on the form.- Write
Button.Click Gets the ID from the
TextBoxon the form. ABLOBis retrieved from a user-specified file and loaded into aBytearray. An OracleDataAdapteris created and a new table is created using theFillSchema( )command. ACommandBuilderis created from theDataAdapter. A new row is created where theBLOBvalue is set from the file specified by the user and theCLOB, andNCLOBvalues 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access