TestOracleClient.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. //
  2. // TestOracleClient.cs - Tests Sytem.Data.OracleClient
  3. // data provider in Mono.
  4. //
  5. // Part of managed C#/.NET library System.Data.OracleClient.dll
  6. //
  7. // Part of the Mono class libraries at
  8. // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
  9. //
  10. // Tests:
  11. // Assembly: System.Data.OracleClient.dll
  12. // Namespace: System.Data.OracleClient
  13. //
  14. // Author:
  15. // Daniel Morgan <[email protected]>
  16. //
  17. // Copyright (C) Daniel Morgan, 2002
  18. //
  19. // Expected Results:
  20. // 3 new rows where ENAME being: 'conn3', 'conn9', and 'conn1'
  21. using System;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Data;
  25. using System.Data.OracleClient;
  26. using System.Text;
  27. namespace Test.OracleClient
  28. {
  29. public class OracleTest
  30. {
  31. public OracleTest()
  32. {
  33. }
  34. static void DoTest1(OracleConnection con, int conn)
  35. {
  36. string inst = conn.ToString();
  37. string insertSql =
  38. "insert into scott.emp " +
  39. "(empno, ename, job, sal, deptno) " +
  40. "values(123" + inst + "," +
  41. "'conn" + inst + "'," +
  42. "'homy" + inst + "'," +
  43. "321" + inst + ",20)";
  44. Console.WriteLine("insertSql: " + insertSql);
  45. OracleCommand cmd = new OracleCommand();
  46. cmd.Connection = con;
  47. cmd.CommandText = insertSql;
  48. cmd.ExecuteNonQuery();
  49. if(conn == 2)
  50. cmd.CommandText = "rollback";
  51. else
  52. cmd.CommandText = "commit";
  53. cmd.ExecuteNonQuery();
  54. }
  55. static void DoTest9(OracleConnection con) {
  56. string inst = "9";
  57. string insertSql =
  58. "insert into scott.emp " +
  59. "(empno, ename, job, sal, deptno) " +
  60. "values(123" + inst + "," +
  61. "'conn" + inst + "'," +
  62. "'homy" + inst + "'," +
  63. "321" + inst + ",20)";
  64. Console.WriteLine("insertSql: " + insertSql);
  65. OracleCommand cmd = new OracleCommand();
  66. cmd.Connection = con;
  67. cmd.CommandText = insertSql;
  68. cmd.ExecuteNonQuery();
  69. cmd.CommandText = "commit";
  70. cmd.ExecuteNonQuery();
  71. }
  72. static void ReadSimpleTest(OracleConnection con)
  73. {
  74. string selectSql =
  75. "SELECT ename, job FROM scott.emp";
  76. OracleCommand cmd = new OracleCommand();
  77. cmd.Connection = con;
  78. cmd.CommandText = selectSql;
  79. OracleDataReader reader = cmd.ExecuteReader();
  80. Console.WriteLine("Results...");
  81. Console.WriteLine("Schema");
  82. DataTable table;
  83. table = reader.GetSchemaTable();
  84. for(int c = 0; c < reader.FieldCount; c++) {
  85. Console.WriteLine(" Column " + c.ToString());
  86. DataRow row = table.Rows[c];
  87. string ColumnName = (string) row["ColumnName"];
  88. string BaseColumnName = (string) row["BaseColumnName"];
  89. int ColumnSize = (int) row["ColumnSize"];
  90. int NumericScale = Convert.ToInt32( row["NumericScale"]);
  91. int NumericPrecision = Convert.ToInt32(row["NumericPrecision"]);
  92. Type DataType = (Type) row["DataType"];
  93. Console.WriteLine(" ColumnName: " + ColumnName);
  94. Console.WriteLine(" BaseColumnName: " + BaseColumnName);
  95. Console.WriteLine(" ColumnSize: " + ColumnSize.ToString());
  96. Console.WriteLine(" NumericScale: " + NumericScale.ToString());
  97. Console.WriteLine(" NumericPrecision: " + NumericPrecision.ToString());
  98. Console.WriteLine(" DataType: " + DataType.ToString());
  99. }
  100. int row = 0;
  101. Console.WriteLine("Data");
  102. while(reader.Read()) {
  103. row++;
  104. Console.WriteLine(" Row: " + row.ToString());
  105. for(int f = 0; f < reader.FieldCount; f++) {
  106. object ovalue;
  107. string svalue;
  108. ovalue = reader.GetValue(0);
  109. svalue = ovalue.ToString();
  110. Console.WriteLine(" Field: " + f.ToString());
  111. Console.WriteLine(" Value: " + svalue);
  112. }
  113. }
  114. if(row == 0)
  115. Console.WriteLine("No data returned.");
  116. }
  117. static void DataAdapterTest (OracleConnection connection)
  118. {
  119. OracleCommand command = connection.CreateCommand ();
  120. command.CommandText = "SELECT * FROM EMP";
  121. OracleDataAdapter adapter = new OracleDataAdapter (command);
  122. DataSet dataSet = new DataSet ("EMP");
  123. adapter.Fill (dataSet);
  124. DataTable table = dataSet.Tables [0];
  125. int rowCount = 0;
  126. foreach (DataRow row in table.Rows) {
  127. Console.WriteLine ("row {0}", rowCount + 1);
  128. for (int i = 0; i < table.Columns.Count; i += 1) {
  129. Console.WriteLine ("{0}:{1}", table.Columns [i].ColumnName, row [i]);
  130. }
  131. Console.WriteLine ();
  132. rowCount += 1;
  133. }
  134. }
  135. static void RollbackTest (OracleConnection connection)
  136. {
  137. OracleTransaction transaction = connection.BeginTransaction ();
  138. OracleCommand insert = connection.CreateCommand ();
  139. insert.Transaction = transaction;
  140. insert.CommandText = "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
  141. Console.WriteLine ("Inserting record ...");
  142. insert.ExecuteNonQuery ();
  143. OracleCommand select = connection.CreateCommand ();
  144. select.CommandText = "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
  145. select.Transaction = transaction;
  146. OracleDataReader reader = select.ExecuteReader ();
  147. reader.Read ();
  148. Console.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", reader.GetValue (0));
  149. reader.Close ();
  150. Console.WriteLine ("Rolling back transaction ...");
  151. transaction.Rollback ();
  152. select = connection.CreateCommand ();
  153. select.CommandText = "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
  154. reader = select.ExecuteReader ();
  155. reader.Read ();
  156. Console.WriteLine ("Row count SHOULD BE 0, VALUE IS {0}", reader.GetValue (0));
  157. reader.Close ();
  158. }
  159. static void CommitTest (OracleConnection connection)
  160. {
  161. OracleTransaction transaction = connection.BeginTransaction ();
  162. OracleCommand insert = connection.CreateCommand ();
  163. insert.Transaction = transaction;
  164. insert.CommandText = "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
  165. Console.WriteLine ("Inserting record ...");
  166. insert.ExecuteNonQuery ();
  167. OracleCommand select = connection.CreateCommand ();
  168. select.CommandText = "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
  169. select.Transaction = transaction;
  170. Console.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
  171. Console.WriteLine ("Committing transaction ...");
  172. transaction.Commit ();
  173. select = connection.CreateCommand ();
  174. select.CommandText = "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
  175. Console.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
  176. transaction = connection.BeginTransaction ();
  177. OracleCommand delete = connection.CreateCommand ();
  178. delete.Transaction = transaction;
  179. delete.CommandText = "DELETE FROM EMP WHERE EMPNO = 8787";
  180. delete.ExecuteNonQuery ();
  181. transaction.Commit ();
  182. }
  183. public static void ParameterTest (OracleConnection connection)
  184. {
  185. OracleTransaction transaction = connection.BeginTransaction ();
  186. OracleCommand insert = connection.CreateCommand ();
  187. insert.Transaction = transaction;
  188. insert.CommandText = "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (:P1, :P2, :P3)";
  189. insert.Parameters.Add (":P1", 8888);
  190. insert.Parameters.Add (":P2", "danmorg");
  191. insert.Parameters.Add (":P3", "Monoist");
  192. Console.WriteLine ("INSERTING DATA WITH PARAMETERS...");
  193. Console.WriteLine (insert.CommandText);
  194. insert.Prepare ();
  195. insert.ExecuteNonQuery ();
  196. OracleCommand select = connection.CreateCommand ();
  197. select.Transaction = transaction;
  198. select.CommandText = "SELECT ENAME, JOB FROM EMP WHERE EMPNO=:P1";
  199. select.Parameters.Add (":P1", 8888);
  200. Console.WriteLine ("VERIFYING RESULTS ...");
  201. OracleDataReader reader = select.ExecuteReader ();
  202. if (!reader.Read ())
  203. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  204. Console.WriteLine ("ENAME - SHOULD BE danmorg, is {0}", reader.GetValue (0));
  205. Console.WriteLine ("JOB - SHOULD BE Monoist, is {0}", reader.GetValue (1));
  206. reader.Close ();
  207. Console.WriteLine ("ROLLBACK TRANSACTION...");
  208. transaction.Rollback ();
  209. }
  210. public static void LOBTest (OracleConnection connection)
  211. {
  212. Console.WriteLine ("BEGIN TRANSACTION ...");
  213. OracleTransaction transaction = connection.BeginTransaction ();
  214. Console.WriteLine ("CREATE TABLE ...");
  215. OracleCommand create = connection.CreateCommand ();
  216. create.Transaction = transaction;
  217. create.CommandText = "CREATE TABLE LOBTEST (CLOB_COLUMN CLOB)";
  218. create.ExecuteNonQuery ();
  219. Console.WriteLine ("INSERT RECORD ...");
  220. OracleCommand insert = connection.CreateCommand ();
  221. insert.Transaction = transaction;
  222. insert.CommandText = "INSERT INTO LOBTEST VALUES (EMPTY_CLOB())";
  223. insert.ExecuteNonQuery ();
  224. OracleCommand select = connection.CreateCommand ();
  225. select.Transaction = transaction;
  226. select.CommandText = "SELECT CLOB_COLUMN FROM LOBTEST FOR UPDATE";
  227. Console.WriteLine ("SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
  228. OracleDataReader reader = select.ExecuteReader ();
  229. if (!reader.Read ())
  230. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  231. Console.WriteLine ("TESTING OracleLob OBJECT ...");
  232. OracleLob lob = reader.GetOracleLob (0);
  233. Console.WriteLine ("LENGTH: {0}", lob.Length);
  234. Console.WriteLine ("CHUNK SIZE: {0}", lob.ChunkSize);
  235. //Console.WriteLine ("ABOUT TO READ VALUE ... SHOULD BE ''");
  236. UnicodeEncoding encoding = new UnicodeEncoding ();
  237. byte[] value = new byte [lob.Length * 2];
  238. //lob.Read (value, 0, (int) lob.Length * 2);
  239. //Console.WriteLine ("VALUE: {0}", encoding.GetString (value));
  240. Console.WriteLine ("CURRENT POSITION: {0}", lob.Position);
  241. Console.WriteLine ("UPDATING VALUE TO 'TEST ME!'");
  242. value = encoding.GetBytes ("TEST ME!");
  243. lob.Write (value, 0, value.Length);
  244. Console.WriteLine ("CURRENT POSITION: {0}", lob.Position);
  245. Console.WriteLine ("RE-READ VALUE...");
  246. lob.Seek (1, SeekOrigin.Begin);
  247. Console.WriteLine ("CURRENT POSITION: {0}", lob.Position);
  248. value = new byte [lob.Length * 2];
  249. lob.Read (value, 0, value.Length);
  250. Console.WriteLine ("VALUE: {0}", encoding.GetString (value));
  251. Console.WriteLine ("CURRENT POSITION: {0}", lob.Position);
  252. Console.WriteLine ("CLOSE OracleLob...");
  253. lob.Close ();
  254. Console.WriteLine ("CLOSING READER...");
  255. reader.Close ();
  256. transaction.Commit ();
  257. Console.WriteLine ("DROP TABLE...");
  258. OracleCommand command = connection.CreateCommand ();
  259. command.CommandText = "DROP TABLE LOBTEST";
  260. command.ExecuteNonQuery ();
  261. }
  262. static void Wait(string msg)
  263. {
  264. Console.WriteLine(msg);
  265. Console.WriteLine("Waiting... Press Enter to continue...");
  266. string nothing = Console.ReadLine();
  267. }
  268. [STAThread]
  269. static void Main(string[] args)
  270. {
  271. if(args.Length != 3) {
  272. Console.WriteLine("Usage: mono TestOracleClient database userid password");
  273. return;
  274. }
  275. string connectionString = String.Format(
  276. "Data Source={0};" +
  277. "User ID={1};" +
  278. "Password={2}",
  279. args[0], args[1], args[2]);
  280. Wait("Verify database.");
  281. OracleConnection con1 = new OracleConnection();
  282. con1.ConnectionString = connectionString;
  283. con1.Open();
  284. Wait("Verify 1 connection.");
  285. OracleConnection con2 = new OracleConnection();
  286. con2.ConnectionString = connectionString;
  287. con2.Open();
  288. Wait("Verify 2 connections.");
  289. OracleConnection con3 = new OracleConnection();
  290. con3.ConnectionString = connectionString;
  291. con3.Open();
  292. Wait("Verify 3 connections.");
  293. //DoTest1(con1, 1);
  294. //DoTest1(con2, 2);
  295. //DoTest1(con3, 3);
  296. //DoTest9(con1);
  297. Console.WriteLine ("LOB Test BEGIN...");
  298. LOBTest (con1);
  299. Console.WriteLine ("LOB Test END.");
  300. Wait ("Press enter to continue ...");
  301. Console.WriteLine ("Read Simple Test BEGIN...");
  302. ReadSimpleTest(con1);
  303. Console.WriteLine ("Read Simple Test END.");
  304. Wait ("Press enter to continue ...");
  305. Console.WriteLine ("DataAdapter Test BEGIN...");
  306. DataAdapterTest(con1);
  307. Console.WriteLine ("DataAdapter Test END.");
  308. Console.WriteLine ("Rollback Test BEGIN...");
  309. RollbackTest(con1);
  310. Console.WriteLine ("Rollback Test END.");
  311. Console.WriteLine ("Commit Test BEGIN...");
  312. CommitTest(con1);
  313. Console.WriteLine ("Commit Test END.");
  314. Console.WriteLine ("Parameter Test BEGIN...");
  315. ParameterTest(con1);
  316. Console.WriteLine ("Parameter Test END.");
  317. Wait("Verify Proper Results.");
  318. con1.Close();
  319. Wait("Verify 2 connections left.");
  320. con2.Close();
  321. Wait("Verify 1 connection left.");
  322. con3.Close();
  323. Wait("Verify all disconnected.");
  324. }
  325. }
  326. }