1 // testblob.cs - tests loading a binary file into an oracle blob and vice-versa
4 using System.Data.OracleClient;
10 static string infilename = @"../../../tools/mono-win32-setup-dark.bmp";
11 static string outfilename = @"mono-win32-setup-dark2.bmp";
12 static string connectionString = "Data Source=testdb;User ID=scott;Password=tiger";
13 static byte[] bytes1 = null;
15 public static void Main (string[] args)
17 OracleConnection con = new OracleConnection();
18 con.ConnectionString = connectionString;
28 // read the BLOB into file "cs-parser2.cs"
29 public static void ReadBlob (OracleConnection connection)
31 if (File.Exists(outfilename) == true) {
32 Console.WriteLine("Filename already exists: " + outfilename);
36 OracleCommand rcmd = connection.CreateCommand ();
37 rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
38 OracleDataReader reader2 = rcmd.ExecuteReader ();
40 Console.WriteLine ("ERROR: RECORD NOT FOUND");
42 Console.WriteLine (" TESTING OracleLob OBJECT 2...");
43 OracleLob lob2 = reader2.GetOracleLob (0);
44 Console.WriteLine (" LENGTH: {0}", lob2.Length);
45 Console.WriteLine (" CHUNK SIZE: {0}", lob2.ChunkSize);
47 byte[] lobvalue = (byte[]) lob2.Value;
49 if (ByteArrayCompare(bytes1, lobvalue) == true)
50 Console.WriteLine("bytes1 and bytes2 are equal: good");
52 Console.WriteLine("bytes1 and bytes2 are not equal: bad");
54 FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
55 BinaryWriter w = new BinaryWriter(fs);
64 public static void BLOBTest (OracleConnection connection)
66 Console.WriteLine (" BEGIN TRANSACTION ...");
68 OracleTransaction transaction = connection.BeginTransaction ();
70 Console.WriteLine (" Drop table BLOBTEST ...");
72 OracleCommand cmd2 = connection.CreateCommand ();
73 cmd2.Transaction = transaction;
74 cmd2.CommandText = "DROP TABLE BLOBTEST";
75 cmd2.ExecuteNonQuery ();
77 catch (OracleException) {
78 // ignore if table already exists
81 Console.WriteLine (" CREATE TABLE ...");
83 OracleCommand create = connection.CreateCommand ();
84 create.Transaction = transaction;
85 create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
86 create.ExecuteNonQuery ();
88 Console.WriteLine (" INSERT RECORD ...");
90 OracleCommand insert = connection.CreateCommand ();
91 insert.Transaction = transaction;
92 insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
93 insert.ExecuteNonQuery ();
95 OracleCommand select = connection.CreateCommand ();
96 select.Transaction = transaction;
97 select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
98 Console.WriteLine (" SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
100 OracleDataReader reader = select.ExecuteReader ();
102 Console.WriteLine ("ERROR: RECORD NOT FOUND");
104 Console.WriteLine (" TESTING OracleLob OBJECT ...");
105 OracleLob lob = reader.GetOracleLob (0);
106 Console.WriteLine (" LENGTH: {0}", lob.Length);
107 Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
110 if (File.Exists(infilename) == false) {
111 Console.WriteLine("Filename does not exist: " + infilename);
115 FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
116 BinaryReader r = new BinaryReader(fs);
119 int bufferLen = 8192;
120 bytes = r.ReadBytes (bufferLen);
122 while(bytes.Length > 0) {
123 Console.WriteLine("byte count: " + bytes.Length.ToString());
124 lob.Write (bytes, 0, bytes.Length);
125 bytes1 = ByteArrayCombine (bytes1, bytes);
126 if (bytes.Length < bufferLen)
128 bytes = r.ReadBytes (bufferLen);
134 //catch (Exception e) {
135 // Console.WriteLine("The file could not be read:");
136 // Console.WriteLine(e.Message);
141 Console.WriteLine (" CLOSING READER...");
144 transaction.Commit ();
154 static byte[] ByteArrayCombine (byte[] b1, byte[] b2)
161 byte[] bytes = new byte[b1.Length + b2.Length];
163 for (int j = 0; j < b1.Length; j++) {
167 for (int k = 0; k < b2.Length; k++) {
174 static bool ByteArrayCompare(byte[] ba1, byte[] ba2)
176 if (ba1 == null && ba2 == null)
185 if (ba1.Length != ba2.Length)
188 for (int i = 0; i < ba1.Length; i++)
190 Console.WriteLine("i: " + i.ToString() + " ba1: " + ba1[i].ToString() + " ba2: " + ba2[i].ToString());
193 for (int i = 0; i < ba1.Length; i++)
195 if (ba1[i] != ba2[i])