1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
// testblob.cs - tests loading a binary file into an oracle blob and vice-versa
using System;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.IO;
class TestBlob
{
static string infilename = @"../../../tools/mono-win32-setup-dark.bmp";
static string outfilename = @"mono-win32-setup-dark2.bmp";
static string connectionString = "Data Source=testdb;User ID=scott;Password=PLACEHOLDER";
static byte[] bytes1 = null;
public static void Main (string[] args)
{
OracleConnection con = new OracleConnection();
con.ConnectionString = connectionString;
con.Open();
BLOBTest (con);
ReadBlob (con);
con.Close();
con = null;
}
// read the BLOB into file "cs-parser2.cs"
public static void ReadBlob (OracleConnection connection)
{
if (File.Exists(outfilename) == true) {
Console.WriteLine("Filename already exists: " + outfilename);
return;
}
OracleCommand rcmd = connection.CreateCommand ();
rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
OracleDataReader reader2 = rcmd.ExecuteReader ();
if (!reader2.Read ())
Console.WriteLine ("ERROR: RECORD NOT FOUND");
Console.WriteLine (" TESTING OracleLob OBJECT 2...");
OracleLob lob2 = reader2.GetOracleLob (0);
Console.WriteLine (" LENGTH: {0}", lob2.Length);
Console.WriteLine (" CHUNK SIZE: {0}", lob2.ChunkSize);
byte[] lobvalue = (byte[]) lob2.Value;
if (ByteArrayCompare(bytes1, lobvalue) == true)
Console.WriteLine("bytes1 and bytes2 are equal: good");
else
Console.WriteLine("bytes1 and bytes2 are not equal: bad");
FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
BinaryWriter w = new BinaryWriter(fs);
w.Write(lobvalue);
w.Close();
fs.Close();
lob2.Close ();
reader2.Close ();
}
public static void BLOBTest (OracleConnection connection)
{
Console.WriteLine (" BEGIN TRANSACTION ...");
OracleTransaction transaction = connection.BeginTransaction ();
Console.WriteLine (" Drop table BLOBTEST ...");
try {
OracleCommand cmd2 = connection.CreateCommand ();
cmd2.Transaction = transaction;
cmd2.CommandText = "DROP TABLE BLOBTEST";
cmd2.ExecuteNonQuery ();
}
catch (OracleException) {
// ignore if table already exists
}
Console.WriteLine (" CREATE TABLE ...");
OracleCommand create = connection.CreateCommand ();
create.Transaction = transaction;
create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
create.ExecuteNonQuery ();
Console.WriteLine (" INSERT RECORD ...");
OracleCommand insert = connection.CreateCommand ();
insert.Transaction = transaction;
insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
insert.ExecuteNonQuery ();
OracleCommand select = connection.CreateCommand ();
select.Transaction = transaction;
select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
Console.WriteLine (" SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
OracleDataReader reader = select.ExecuteReader ();
if (!reader.Read ())
Console.WriteLine ("ERROR: RECORD NOT FOUND");
Console.WriteLine (" TESTING OracleLob OBJECT ...");
OracleLob lob = reader.GetOracleLob (0);
Console.WriteLine (" LENGTH: {0}", lob.Length);
Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
//try {
if (File.Exists(infilename) == false) {
Console.WriteLine("Filename does not exist: " + infilename);
return;
}
FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs);
byte[] bytes = null;
int bufferLen = 8192;
bytes = r.ReadBytes (bufferLen);
while(bytes.Length > 0) {
Console.WriteLine("byte count: " + bytes.Length.ToString());
lob.Write (bytes, 0, bytes.Length);
bytes1 = ByteArrayCombine (bytes1, bytes);
if (bytes.Length < bufferLen)
break;
bytes = r.ReadBytes (bufferLen);
}
r.Close();
fs.Close ();
//}
//catch (Exception e) {
// Console.WriteLine("The file could not be read:");
// Console.WriteLine(e.Message);
//}
lob.Close ();
Console.WriteLine (" CLOSING READER...");
reader.Close ();
transaction.Commit ();
transaction = null;
lob = null;
reader.Dispose();
reader = null;
create = null;
insert = null;
select = null;
}
static byte[] ByteArrayCombine (byte[] b1, byte[] b2)
{
if (b1 == null)
b1 = new byte[0];
if (b2 == null)
b2 = new byte[0];
byte[] bytes = new byte[b1.Length + b2.Length];
int i = 0;
for (int j = 0; j < b1.Length; j++) {
bytes[i] = b1[j];
i++;
}
for (int k = 0; k < b2.Length; k++) {
bytes[i] = b2[k];
i++;
}
return bytes;
}
static bool ByteArrayCompare(byte[] ba1, byte[] ba2)
{
if (ba1 == null && ba2 == null)
return true;
if (ba1 == null)
return false;
if (ba2 == null)
return false;
if (ba1.Length != ba2.Length)
return false;
for (int i = 0; i < ba1.Length; i++)
{
Console.WriteLine("i: " + i.ToString() + " ba1: " + ba1[i].ToString() + " ba2: " + ba2[i].ToString());
}
for (int i = 0; i < ba1.Length; i++)
{
if (ba1[i] != ba2[i])
return false;
}
return true;
}
}
|