testclob.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // testclob.cs - tests loading a text file into an oracle clob 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 TestClob
  8. {
  9. static string infilename = @"cs-parser.cs";
  10. static string outfilename = @"cs-parser2.cs";
  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. CLOBTest (con);
  18. ReadClob (con);
  19. con.Close();
  20. con = null;
  21. }
  22. // read the CLOB into file "cs-parser2.cs"
  23. public static void ReadClob (OracleConnection connection)
  24. {
  25. OracleCommand rcmd = connection.CreateCommand ();
  26. rcmd.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST";
  27. OracleDataReader reader2 = rcmd.ExecuteReader ();
  28. if (!reader2.Read ())
  29. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  30. Console.WriteLine (" TESTING OracleLob OBJECT 2...");
  31. OracleLob lob2 = reader2.GetOracleLob (0);
  32. Console.WriteLine (" LENGTH: {0}", lob2.Length);
  33. Console.WriteLine (" CHUNK SIZE: {0}", lob2.ChunkSize);
  34. string lobvalue = (string) lob2.Value;
  35. using (StreamWriter sw = new StreamWriter(outfilename)) {
  36. sw.Write(lobvalue);
  37. }
  38. lob2.Close ();
  39. reader2.Close ();
  40. }
  41. public static void CLOBTest (OracleConnection connection)
  42. {
  43. Console.WriteLine (" BEGIN TRANSACTION ...");
  44. OracleTransaction transaction = connection.BeginTransaction ();
  45. Console.WriteLine (" Drop table CLOBTEST ...");
  46. try {
  47. OracleCommand cmd2 = connection.CreateCommand ();
  48. cmd2.Transaction = transaction;
  49. cmd2.CommandText = "DROP TABLE CLOBTEST";
  50. cmd2.ExecuteNonQuery ();
  51. }
  52. catch (OracleException oe1) {
  53. // ignore if table already exists
  54. }
  55. Console.WriteLine (" CREATE TABLE ...");
  56. OracleCommand create = connection.CreateCommand ();
  57. create.Transaction = transaction;
  58. create.CommandText = "CREATE TABLE CLOBTEST (CLOB_COLUMN CLOB)";
  59. create.ExecuteNonQuery ();
  60. Console.WriteLine (" INSERT RECORD ...");
  61. OracleCommand insert = connection.CreateCommand ();
  62. insert.Transaction = transaction;
  63. insert.CommandText = "INSERT INTO CLOBTEST VALUES (EMPTY_CLOB())";
  64. insert.ExecuteNonQuery ();
  65. OracleCommand select = connection.CreateCommand ();
  66. select.Transaction = transaction;
  67. select.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST FOR UPDATE";
  68. Console.WriteLine (" SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
  69. OracleDataReader reader = select.ExecuteReader ();
  70. if (!reader.Read ())
  71. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  72. Console.WriteLine (" TESTING OracleLob OBJECT ...");
  73. OracleLob lob = reader.GetOracleLob (0);
  74. Console.WriteLine (" LENGTH: {0}", lob.Length);
  75. Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
  76. UnicodeEncoding encoding = new UnicodeEncoding ();
  77. try {
  78. // read file "cs-parser.cs" into the oracle clob
  79. using (StreamReader sr = new StreamReader(infilename)) {
  80. string sbuff = sr.ReadToEnd ();
  81. byte[] evalue = encoding.GetBytes (sbuff);
  82. lob.Write (evalue, 0, evalue.Length);
  83. }
  84. }
  85. catch (Exception e) {
  86. Console.WriteLine("The file could not be read:");
  87. Console.WriteLine(e.Message);
  88. }
  89. lob.Close ();
  90. Console.WriteLine (" CLOSING READER...");
  91. reader.Close ();
  92. transaction.Commit ();
  93. }
  94. }