testblob.cs 5.3 KB

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