2 // TestOracleClient.cs - Tests Sytem.Data.OracleClient
3 // data provider in Mono.
5 // Part of managed C#/.NET library System.Data.OracleClient.dll
7 // Part of the Mono class libraries at
8 // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
11 // Assembly: System.Data.OracleClient.dll
12 // Namespace: System.Data.OracleClient
15 // Daniel Morgan <danmorg@sc.rr.com>
17 // Copyright (C) Daniel Morgan, 2002
21 // 3 new rows where ENAME being: 'conn3', 'conn9', and 'conn1'
25 using System
.Runtime
.InteropServices
;
27 using System
.Data
.OracleClient
;
30 namespace Test
.OracleClient
32 public class OracleTest
39 static void DoTest1(OracleConnection con
, int conn
)
41 string inst
= conn
.ToString();
44 "insert into scott.emp " +
45 "(empno, ename, job, sal, deptno) " +
46 "values(123" + inst
+ "," +
47 "'conn" + inst
+ "'," +
48 "'homy" + inst
+ "'," +
49 "321" + inst
+ ",20)";
51 Console
.WriteLine("insertSql: " + insertSql
);
52 OracleCommand cmd
= new OracleCommand();
55 cmd
.CommandText
= insertSql
;
56 cmd
.ExecuteNonQuery();
59 cmd
.CommandText
= "rollback";
61 cmd
.CommandText
= "commit";
62 cmd
.ExecuteNonQuery();
65 static void DoTest9(OracleConnection con
) {
69 "insert into scott.emp " +
70 "(empno, ename, job, sal, deptno) " +
71 "values(123" + inst
+ "," +
72 "'conn" + inst
+ "'," +
73 "'homy" + inst
+ "'," +
74 "321" + inst
+ ",20)";
76 Console
.WriteLine("insertSql: " + insertSql
);
77 OracleCommand cmd
= new OracleCommand();
80 cmd
.CommandText
= insertSql
;
81 cmd
.ExecuteNonQuery();
83 cmd
.CommandText
= "commit";
84 cmd
.ExecuteNonQuery();
87 static void ReadSimpleTest(OracleConnection con
)
90 "SELECT ename, job FROM scott.emp";
91 OracleCommand cmd
= new OracleCommand();
93 cmd
.CommandText
= selectSql
;
94 OracleDataReader reader
= cmd
.ExecuteReader();
95 Console
.WriteLine("Results...");
96 Console
.WriteLine("Schema");
98 table
= reader
.GetSchemaTable();
99 for(int c
= 0; c
< reader
.FieldCount
; c
++) {
100 Console
.WriteLine(" Column " + c
.ToString());
101 DataRow row
= table
.Rows
[c
];
103 string ColumnName
= (string) row
["ColumnName"];
104 string BaseColumnName
= (string) row
["BaseColumnName"];
105 int ColumnSize
= (int) row
["ColumnSize"];
106 int NumericScale
= Convert
.ToInt32( row
["NumericScale"]);
107 int NumericPrecision
= Convert
.ToInt32(row
["NumericPrecision"]);
108 Type DataType
= (Type
) row
["DataType"];
110 Console
.WriteLine(" ColumnName: " + ColumnName
);
111 Console
.WriteLine(" BaseColumnName: " + BaseColumnName
);
112 Console
.WriteLine(" ColumnSize: " + ColumnSize
.ToString());
113 Console
.WriteLine(" NumericScale: " + NumericScale
.ToString());
114 Console
.WriteLine(" NumericPrecision: " + NumericPrecision
.ToString());
115 Console
.WriteLine(" DataType: " + DataType
.ToString());
119 Console
.WriteLine("Data");
120 while(reader
.Read()) {
122 Console
.WriteLine(" Row: " + row
.ToString());
123 for(int f
= 0; f
< reader
.FieldCount
; f
++) {
126 ovalue
= reader
.GetValue(0);
127 svalue
= ovalue
.ToString();
128 Console
.WriteLine(" Field: " + f
.ToString());
129 Console
.WriteLine(" Value: " + svalue
);
133 Console
.WriteLine("No data returned.");
136 static void DataAdapterTest (OracleConnection connection
)
138 OracleCommand command
= connection
.CreateCommand ();
139 command
.CommandText
= "SELECT * FROM EMP";
140 OracleDataAdapter adapter
= new OracleDataAdapter (command
);
142 DataSet dataSet
= new DataSet ("EMP");
144 adapter
.Fill (dataSet
);
146 DataTable table
= dataSet
.Tables
[0];
148 foreach (DataRow row
in table
.Rows
) {
149 Console
.WriteLine ("row {0}", rowCount
+ 1);
150 for (int i
= 0; i
< table
.Columns
.Count
; i
+= 1) {
151 Console
.WriteLine ("{0}:{1}", table
.Columns
[i
].ColumnName
, row
[i
]);
153 Console
.WriteLine ();
158 static void RollbackTest (OracleConnection connection
)
160 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')";
166 Console
.WriteLine ("Inserting record ...");
168 insert
.ExecuteNonQuery ();
170 OracleCommand
select = connection
.CreateCommand ();
171 select.CommandText
= "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
172 select.Transaction
= transaction
;
173 OracleDataReader reader
= select.ExecuteReader ();
176 Console
.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", reader
.GetValue (0));
179 Console
.WriteLine ("Rolling back transaction ...");
181 transaction
.Rollback ();
183 select = connection
.CreateCommand ();
184 select.CommandText
= "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
186 reader
= select.ExecuteReader ();
188 Console
.WriteLine ("Row count SHOULD BE 0, VALUE IS {0}", reader
.GetValue (0));
192 static void CommitTest (OracleConnection connection
)
194 OracleTransaction transaction
= connection
.BeginTransaction ();
196 OracleCommand insert
= connection
.CreateCommand ();
197 insert
.Transaction
= transaction
;
198 insert
.CommandText
= "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
200 Console
.WriteLine ("Inserting record ...");
202 insert
.ExecuteNonQuery ();
204 OracleCommand
select = connection
.CreateCommand ();
205 select.CommandText
= "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
206 select.Transaction
= transaction
;
208 Console
.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
210 Console
.WriteLine ("Committing transaction ...");
212 transaction
.Commit ();
214 select = connection
.CreateCommand ();
215 select.CommandText
= "SELECT COUNT(*) FROM EMP WHERE EMPNO = 8787";
217 Console
.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
218 transaction
= connection
.BeginTransaction ();
219 OracleCommand delete
= connection
.CreateCommand ();
220 delete
.Transaction
= transaction
;
221 delete
.CommandText
= "DELETE FROM EMP WHERE EMPNO = 8787";
222 delete
.ExecuteNonQuery ();
223 transaction
.Commit ();
226 public static void ParameterTest (OracleConnection connection
)
228 OracleTransaction transaction
= connection
.BeginTransaction ();
229 OracleCommand insert
= connection
.CreateCommand ();
230 insert
.Transaction
= transaction
;
232 insert
.CommandText
= "INSERT INTO EMP (EMPNO, ENAME, JOB) VALUES (:P1, :P2, :P3)";
233 insert
.Parameters
.Add (":P1", 8888);
234 insert
.Parameters
.Add (":P2", "danmorg");
235 insert
.Parameters
.Add (":P3", "Monoist");
237 Console
.WriteLine ("INSERTING DATA WITH PARAMETERS...");
238 Console
.WriteLine (insert
.CommandText
);
240 insert
.ExecuteNonQuery ();
242 OracleCommand
select = connection
.CreateCommand ();
243 select.Transaction
= transaction
;
245 select.CommandText
= "SELECT ENAME, JOB FROM EMP WHERE EMPNO=:P1";
246 select.Parameters
.Add (":P1", 8888);
248 Console
.WriteLine ("VERIFYING RESULTS ...");
250 OracleDataReader reader
= select.ExecuteReader ();
252 Console
.WriteLine ("ERROR: RECORD NOT FOUND");
254 Console
.WriteLine ("ENAME - SHOULD BE danmorg, is {0}", reader
.GetValue (0));
255 Console
.WriteLine ("JOB - SHOULD BE Monoist, is {0}", reader
.GetValue (1));
259 Console
.WriteLine ("ROLLBACK TRANSACTION...");
261 transaction
.Rollback ();
264 public static void LOBTest (OracleConnection connection
)
266 Console
.WriteLine ("BEGIN TRANSACTION ...");
268 OracleTransaction transaction
= connection
.BeginTransaction ();
270 Console
.WriteLine ("CREATE TABLE ...");
272 OracleCommand create
= connection
.CreateCommand ();
273 create
.Transaction
= transaction
;
274 create
.CommandText
= "CREATE TABLE LOBTEST (CLOB_COLUMN CLOB)";
275 create
.ExecuteNonQuery ();
277 Console
.WriteLine ("INSERT RECORD ...");
279 OracleCommand insert
= connection
.CreateCommand ();
280 insert
.Transaction
= transaction
;
281 insert
.CommandText
= "INSERT INTO LOBTEST VALUES (EMPTY_CLOB())";
282 insert
.ExecuteNonQuery ();
284 OracleCommand
select = connection
.CreateCommand ();
285 select.Transaction
= transaction
;
286 select.CommandText
= "SELECT CLOB_COLUMN FROM LOBTEST FOR UPDATE";
287 Console
.WriteLine ("SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
289 OracleDataReader reader
= select.ExecuteReader ();
291 Console
.WriteLine ("ERROR: RECORD NOT FOUND");
293 Console
.WriteLine ("TESTING OracleLob OBJECT ...");
294 OracleLob lob
= reader
.GetOracleLob (0);
295 Console
.WriteLine ("LENGTH: {0}", lob
.Length
);
296 Console
.WriteLine ("CHUNK SIZE: {0}", lob
.ChunkSize
);
297 //Console.WriteLine ("ABOUT TO READ VALUE ... SHOULD BE ''");
299 UnicodeEncoding encoding
= new UnicodeEncoding ();
301 byte[] value = new byte [lob
.Length
* 2];
302 //lob.Read (value, 0, (int) lob.Length * 2);
303 //Console.WriteLine ("VALUE: {0}", encoding.GetString (value));
305 Console
.WriteLine ("CURRENT POSITION: {0}", lob
.Position
);
306 Console
.WriteLine ("UPDATING VALUE TO 'TEST ME!'");
307 value = encoding
.GetBytes ("TEST ME!");
308 lob
.Write (value, 0, value.Length
);
310 Console
.WriteLine ("CURRENT POSITION: {0}", lob
.Position
);
311 Console
.WriteLine ("RE-READ VALUE...");
312 lob
.Seek (1, SeekOrigin
.Begin
);
314 Console
.WriteLine ("CURRENT POSITION: {0}", lob
.Position
);
315 value = new byte [lob
.Length
* 2];
316 lob
.Read (value, 0, value.Length
);
317 Console
.WriteLine ("VALUE: {0}", encoding
.GetString (value));
318 Console
.WriteLine ("CURRENT POSITION: {0}", lob
.Position
);
320 Console
.WriteLine ("CLOSE OracleLob...");
323 Console
.WriteLine ("CLOSING READER...");
325 transaction
.Commit ();
327 Console
.WriteLine ("DROP TABLE...");
329 OracleCommand command
= connection
.CreateCommand ();
330 command
.CommandText
= "DROP TABLE LOBTEST";
331 command
.ExecuteNonQuery ();
335 static void Wait(string msg
)
337 Console
.WriteLine(msg
);
338 Console
.WriteLine("Waiting... Press Enter to continue...");
339 string nothing
= Console
.ReadLine();
343 static void Main(string[] args
)
345 if(args
.Length
!= 3) {
346 Console
.WriteLine("Usage: mono TestOracleClient database userid password");
350 string connectionString
= String
.Format(
354 args
[0], args
[1], args
[2]);
356 Wait("Verify database.");
358 OracleConnection con1
= new OracleConnection();
359 con1
.ConnectionString
= connectionString
;
362 Wait("Verify 1 connection.");
364 OracleConnection con2
= new OracleConnection();
365 con2
.ConnectionString
= connectionString
;
368 Wait("Verify 2 connections.");
370 OracleConnection con3
= new OracleConnection();
371 con3
.ConnectionString
= connectionString
;
374 Wait("Verify 3 connections.");
382 Console
.WriteLine ("LOB Test BEGIN...");
384 Console
.WriteLine ("LOB Test END.");
385 Wait ("Press enter to continue ...");
387 Console
.WriteLine ("Read Simple Test BEGIN...");
388 ReadSimpleTest(con1
);
389 Console
.WriteLine ("Read Simple Test END.");
391 Wait ("Press enter to continue ...");
393 Console
.WriteLine ("DataAdapter Test BEGIN...");
394 DataAdapterTest(con1
);
395 Console
.WriteLine ("DataAdapter Test END.");
397 Console
.WriteLine ("Rollback Test BEGIN...");
399 Console
.WriteLine ("Rollback Test END.");
401 Console
.WriteLine ("Commit Test BEGIN...");
403 Console
.WriteLine ("Commit Test END.");
405 Console
.WriteLine ("Parameter Test BEGIN...");
407 Console
.WriteLine ("Parameter Test END.");
411 Wait("Verify Proper Results.");
415 Wait("Verify 2 connections left.");
419 Wait("Verify 1 connection left.");
423 Wait("Verify all disconnected.");