Reading and Writing Binary Data with Oracle
Số trang: 5
Loại file: pdf
Dung lượng: 17.63 KB
Lượt xem: 2
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
[ 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
Nội dung trích xuất từ tài liệu:
Reading and Writing Binary Data with Oracle[ 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: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 constantsusing 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 ofthe 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 asdescribed 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. ThisBlob data type maps to a Byte array. Oracle data type containing character data based on the def ...
Nội dung trích xuất từ tài liệu:
Reading and Writing Binary Data with Oracle[ 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: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 constantsusing 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 ofthe 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 asdescribed 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. ThisBlob data type maps to a Byte array. Oracle data type containing character data based on the def ...
Tìm kiếm theo từ khóa liên quan:
công nghệ thông tin kỹ thuật lập trình Oreilly Ado Dot Net Cookbook Ebook-Lib Reading and Writing Binary Data with OracleTài liệu có liên quan:
-
52 trang 469 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 369 0 0 -
96 trang 335 0 0
-
74 trang 329 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 322 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 321 1 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 311 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 305 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 304 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 297 0 0