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
command
is built using the SQL statement and executed to build aDataReader
. TheBLOB
is retrieved from theDataReader
and displayed in thePictureBox
on the form. TheCLOB
andNCLOB
values are retrieved from theDataReader
and displayed in text boxes on the form.- Write
Button.Click
Gets the ID from the
TextBox
on the form. ABLOB
is retrieved from a user-specified file and loaded into aByte
array. An OracleDataAdapter
is created and a new table is created using theFillSchema( )
command. ACommandBuilder
is created from theDataAdapter
. A new row is created where theBLOB
value is set from the file specified by the user and theCLOB
, andNCLOB
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 the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.