1 // testblob.cs - tests loading a binary file into an oracle blob and vice-versa
4 using System.Data.OracleClient;
10 static string infilename = @"mono-win32-setup-dark.bmp";
11 static string outfilename = @"mono-win32-setup-dark2.bmp";
12 static string connectionString = "Data Source=palis;User ID=scott;Password=tiger";
14 public static void Main (string[] args)
16 OracleConnection con = new OracleConnection();
17 con.ConnectionString = connectionString;
27 // read the BLOB into file "cs-parser2.cs"
28 public static void ReadBlob (OracleConnection connection)
30 if (File.Exists(outfilename) == true) {
31 Console.WriteLine("Filename already exists: " + outfilename);
35 OracleCommand rcmd = connection.CreateCommand ();
36 rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
37 OracleDataReader reader2 = rcmd.ExecuteReader ();
39 Console.WriteLine ("ERROR: RECORD NOT FOUND");
41 Console.WriteLine (" TESTING OracleLob OBJECT 2...");
42 OracleLob lob2 = reader2.GetOracleLob (0);
43 Console.WriteLine (" LENGTH: {0}", lob2.Length);
44 Console.WriteLine (" CHUNK SIZE: {0}", lob2.ChunkSize);
46 byte[] lobvalue = (byte[]) lob2.Value;
48 FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
49 BinaryWriter w = new BinaryWriter(fs);
58 public static void BLOBTest (OracleConnection connection)
60 Console.WriteLine (" BEGIN TRANSACTION ...");
62 OracleTransaction transaction = connection.BeginTransaction ();
64 Console.WriteLine (" Drop table BLOBTEST ...");
66 OracleCommand cmd2 = connection.CreateCommand ();
67 cmd2.Transaction = transaction;
68 cmd2.CommandText = "DROP TABLE BLOBTEST";
69 cmd2.ExecuteNonQuery ();
71 catch (OracleException oe1) {
72 // ignore if table already exists
75 Console.WriteLine (" CREATE TABLE ...");
77 OracleCommand create = connection.CreateCommand ();
78 create.Transaction = transaction;
79 create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
80 create.ExecuteNonQuery ();
82 Console.WriteLine (" INSERT RECORD ...");
84 OracleCommand insert = connection.CreateCommand ();
85 insert.Transaction = transaction;
86 insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
87 insert.ExecuteNonQuery ();
89 OracleCommand select = connection.CreateCommand ();
90 select.Transaction = transaction;
91 select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
92 Console.WriteLine (" SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
94 OracleDataReader reader = select.ExecuteReader ();
96 Console.WriteLine ("ERROR: RECORD NOT FOUND");
98 Console.WriteLine (" TESTING OracleLob OBJECT ...");
99 OracleLob lob = reader.GetOracleLob (0);
100 Console.WriteLine (" LENGTH: {0}", lob.Length);
101 Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
104 if (File.Exists(infilename) == false) {
105 Console.WriteLine("Filename does not exist: " + infilename);
109 FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
110 BinaryReader r = new BinaryReader(fs);
113 int bufferLen = 8192;
114 bytes = r.ReadBytes (bufferLen);
116 while(bytes.Length > 0) {
117 Console.WriteLine("byte count: " + bytes.Length.ToString());
118 lob.Write (bytes, 0, bytes.Length);
119 if (bytes.Length < bufferLen)
121 bytes = r.ReadBytes (bufferLen);
127 catch (Exception e) {
128 Console.WriteLine("The file could not be read:");
129 Console.WriteLine(e.Message);
134 Console.WriteLine (" CLOSING READER...");
137 transaction.Commit ();