2
0

testblob.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = @"mono-win32-setup-dark.bmp";
  10. static string outfilename = @"mono-win32-setup-dark2.bmp";
  11. static string connectionString = "Data Source=palis;User ID=scott;Password=tiger";
  12. public static void Main (string[] args)
  13. {
  14. OracleConnection con = new OracleConnection();
  15. con.ConnectionString = connectionString;
  16. con.Open();
  17. BLOBTest (con);
  18. ReadBlob (con);
  19. con.Close();
  20. con = null;
  21. }
  22. // read the BLOB into file "cs-parser2.cs"
  23. public static void ReadBlob (OracleConnection connection)
  24. {
  25. if (File.Exists(outfilename) == true) {
  26. Console.WriteLine("Filename already exists: " + outfilename);
  27. return;
  28. }
  29. OracleCommand rcmd = connection.CreateCommand ();
  30. rcmd.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST";
  31. OracleDataReader reader2 = rcmd.ExecuteReader ();
  32. if (!reader2.Read ())
  33. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  34. Console.WriteLine (" TESTING OracleLob OBJECT 2...");
  35. OracleLob lob2 = reader2.GetOracleLob (0);
  36. Console.WriteLine (" LENGTH: {0}", lob2.Length);
  37. Console.WriteLine (" CHUNK SIZE: {0}", lob2.ChunkSize);
  38. byte[] lobvalue = (byte[]) lob2.Value;
  39. FileStream fs = new FileStream(outfilename, FileMode.CreateNew);
  40. BinaryWriter w = new BinaryWriter(fs);
  41. w.Write(lobvalue);
  42. w.Close();
  43. fs.Close();
  44. lob2.Close ();
  45. reader2.Close ();
  46. }
  47. public static void BLOBTest (OracleConnection connection)
  48. {
  49. Console.WriteLine (" BEGIN TRANSACTION ...");
  50. OracleTransaction transaction = connection.BeginTransaction ();
  51. Console.WriteLine (" Drop table BLOBTEST ...");
  52. try {
  53. OracleCommand cmd2 = connection.CreateCommand ();
  54. cmd2.Transaction = transaction;
  55. cmd2.CommandText = "DROP TABLE BLOBTEST";
  56. cmd2.ExecuteNonQuery ();
  57. }
  58. catch (OracleException oe1) {
  59. // ignore if table already exists
  60. }
  61. Console.WriteLine (" CREATE TABLE ...");
  62. OracleCommand create = connection.CreateCommand ();
  63. create.Transaction = transaction;
  64. create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
  65. create.ExecuteNonQuery ();
  66. Console.WriteLine (" INSERT RECORD ...");
  67. OracleCommand insert = connection.CreateCommand ();
  68. insert.Transaction = transaction;
  69. insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
  70. insert.ExecuteNonQuery ();
  71. OracleCommand select = connection.CreateCommand ();
  72. select.Transaction = transaction;
  73. select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
  74. Console.WriteLine (" SELECTING A BLOB (Binary Large Object) VALUE FROM BLOBTEST");
  75. OracleDataReader reader = select.ExecuteReader ();
  76. if (!reader.Read ())
  77. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  78. Console.WriteLine (" TESTING OracleLob OBJECT ...");
  79. OracleLob lob = reader.GetOracleLob (0);
  80. Console.WriteLine (" LENGTH: {0}", lob.Length);
  81. Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
  82. try {
  83. if (File.Exists(infilename) == false) {
  84. Console.WriteLine("Filename does not exist: " + infilename);
  85. return;
  86. }
  87. FileStream fs = new FileStream(infilename, FileMode.Open, FileAccess.Read);
  88. BinaryReader r = new BinaryReader(fs);
  89. byte[] bytes = null;
  90. int bufferLen = 8192;
  91. bytes = r.ReadBytes (bufferLen);
  92. while(bytes.Length > 0) {
  93. Console.WriteLine("byte count: " + bytes.Length.ToString());
  94. lob.Write (bytes, 0, bytes.Length);
  95. if (bytes.Length < bufferLen)
  96. break;
  97. bytes = r.ReadBytes (bufferLen);
  98. }
  99. r.Close();
  100. fs.Close ();
  101. }
  102. catch (Exception e) {
  103. Console.WriteLine("The file could not be read:");
  104. Console.WriteLine(e.Message);
  105. }
  106. lob.Close ();
  107. Console.WriteLine (" CLOSING READER...");
  108. reader.Close ();
  109. transaction.Commit ();
  110. }
  111. }