intTypePromotion=1
zunia.vn Tuyển sinh 2024 dành cho Gen-Z zunia.vn zunia.vn
ADSENSE

Reading and Writing Binary Data with Oracle

Chia sẻ: Bui Tuan | Ngày: | Loại File: PDF | Số trang:5

84
lượt xem
4
download
 
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

[ Team LiB ] Recipe 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

Chủ đề:
Lưu

Nội dung Text: Reading and Writing Binary Data with Oracle

  1. [ Team LiB ] Recipe 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 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;
  2. 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);
  3. 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);
  4. 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); } } Discussion
  5. The 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. Table 9-4. Oracle LOB data type Data Description Type Oracle data type containing binary data with a maximum size of 4 GB. This Blob data type maps to a Byte array. Oracle data type containing character data based on the default character set Clob of the server with a maximum size of 4 GB. This data type maps to a String. Oracle data type containing character data based on the national character set NClob of the server with a maximum size of 4 GB. This data type maps to a String. 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 ]
ADSENSE

CÓ THỂ BẠN MUỐN DOWNLOAD

 

Đồng bộ tài khoản
2=>2