[ Team LiB ] |
Recipe 9.12 Reading and Writing Binary Data with OracleProblemYou need to read and write binary data from and to an Oracle database. SolutionUse the techniques shown in the following example. The sample code contains two event handlers:
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 ofd; private const String TABLENAME = "TBL0912"; private const String ID_FIELD = "ID"; private const String BLOBFIELD_FIELD = "BLOBFIELD"; private const String CLOBFIELD_FIELD = "CLOBFIELD"; private const String NCLOBFIELD_FIELD = "NCLOBFIELD"; // . . . private void readButton_Click(object sender, System.EventArgs e) { // Clear the controls. blobPictureBox.Image = null; clobTextBox.Clear( ); nclobTextBox.Clear( ); String sqlText = "SELECT * FROM " + TABLENAME + " WHERE ID = " + idTextBox.Text; // Create the connection and command. OracleConnection conn = new OracleConnection( ConfigurationSettings.AppSettings["Oracle_ConnectString"]); OracleCommand cmd = new OracleCommand(sqlText, conn); conn.Open( ); // Create the DataReader. OracleDataReader dr = cmd.ExecuteReader( ); // Iterate over the collection of rows in the DataReader. if(dr.Read( )) { // Retrieve the BLOB into a stream. Byte[] blob = null; if(!dr.IsDBNull(1)) blob = (Byte[])dr.GetOracleLob(1).Value; MemoryStream ms = new MemoryStream(blob); // Display the BLOB in the PictureBox. blobPictureBox.Image = Image.FromStream(ms); ms.Close( ); // Get the CLOB. if(!dr.IsDBNull(2)) clobTextBox.Text = dr.GetOracleLob(2).Value.ToString( ); // Get the NCLOB. if(!dr.IsDBNull(3)) nclobTextBox.Text = dr.GetOracleLob(3).Value.ToString( ); } else { MessageBox.Show("No record found.", "Access Oracle LOB Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } dr.Close( ); conn.Close( ); } private void writeButton_Click(object sender, System.EventArgs e) { // Get the user-supplied ID. int id; try { id = Convert.ToInt32(idTextBox.Text); } catch(System.Exception ex) { MessageBox.Show(ex.Message, "Access Oracle LOB Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } // Save the BLOB, CLOB, and NCLOB. if (ofd.ShowDialog( ) == DialogResult.OK) { // Get a BLOB from a user-specified file. FileStream fs = new FileStream(ofd.FileName, FileMode.OpenOrCreate, FileAccess.Read); Byte[] blob = new Byte[fs.Length]; fs.Read(blob, 0, blob.Length); fs.Close( ); // Create a DataAdapter and table. OracleDataAdapter da = new OracleDataAdapter("SELECT * FROM " + TABLENAME, ConfigurationSettings.AppSettings["Oracle_ConnectString"]); DataTable table = new DataTable( ); // Just get the schema. da.FillSchema(table, SchemaType.Source); OracleCommandBuilder cb = new OracleCommandBuilder(da); // Create a row containing the new BLOB, CLOB, and NCLOB data. DataRow row = table.NewRow( ); row[ID_FIELD] = id; row[BLOBFIELD_FIELD] = blob; if(clobTextBox.TextLength > 0) row[CLOBFIELD_FIELD] = clobTextBox.Text; if(nclobTextBox.TextLength > 0) row[NCLOBFIELD_FIELD] = nclobTextBox.Text; // Add the row to the table. table.Rows.Add(row); // Update the Oracle database using the DataAdapter. try { da.Update(table); } catch(System.Exception ex) { MessageBox.Show(ex.Message, "Access Oracle LOB Data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } MessageBox.Show("Record successfully created.", "Access Oracle LOB Data", MessageBoxButtons.OK, MessageBoxIcon.Information); } } DiscussionThe GetOracleLob( ) typed accessor method of the OracleDataReader gets the value of the specified column as an OracleLob object representing a Large Object Binary (LOB) data type stored on an Oracle server. An Oracle LOB can be one of three types as described in Table 9-4.
The Oracle .NET data provider handles CLOB and NCLOB data as Unicode. Each character is therefore two bytes long. See Recipe 9.12 for a general discussion about reading and writing BLOB data from and to a data source. |
[ Team LiB ] |