OracleAdapterTest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //
  2. // OracleAdapterTest.cs - tests select/insert/update/delete of
  3. // a DataSet/DataTable with
  4. // OracleDataAdapter and OracleCommandBuilder
  5. //
  6. // Author:
  7. // Daniel Morgan <[email protected]>
  8. //
  9. // Copyright (C) Daniel Morgan, 2005
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.IO;
  15. using System.Data;
  16. using System.Data.Common;
  17. using System.Data.OracleClient;
  18. using System.Text;
  19. class OracleAdapterTest
  20. {
  21. static string infilename = @"mono-win32-setup-dark.bmp";
  22. public static void Main(string[] args)
  23. {
  24. if(args.Length != 3) {
  25. Console.WriteLine("Usage: mono TestOracleClient database userid password");
  26. return;
  27. }
  28. string connectionString = String.Format(
  29. "Data Source={0};" +
  30. "User ID={1};" +
  31. "Password={2}",
  32. args[0], args[1], args[2]);
  33. OracleConnection con = new OracleConnection ();
  34. con.ConnectionString = connectionString;
  35. con.Open ();
  36. Setup (con);
  37. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  38. GetMetaData (con);
  39. Insert (con);
  40. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  41. Update (con);
  42. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  43. //Delete (con);
  44. //ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  45. con.Close ();
  46. }
  47. public static void GetMetaData(OracleConnection con)
  48. {
  49. OracleCommand cmd = null;
  50. OracleDataReader rdr = null;
  51. cmd = con.CreateCommand();
  52. cmd.CommandText = "select * from mono_adapter_test";
  53. Console.WriteLine("Read Schema With KeyInfo");
  54. rdr = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly);
  55. DataTable dt;
  56. dt = rdr.GetSchemaTable();
  57. foreach (DataRow schemaRow in dt.Rows) {
  58. foreach (DataColumn schemaCol in dt.Columns) {
  59. Console.WriteLine(schemaCol.ColumnName +
  60. " = " +
  61. schemaRow[schemaCol]);
  62. Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
  63. }
  64. Console.WriteLine("");
  65. }
  66. Console.WriteLine("Read Schema with No KeyInfo");
  67. rdr = cmd.ExecuteReader();
  68. dt = rdr.GetSchemaTable();
  69. foreach (DataRow schemaRow in dt.Rows) {
  70. foreach (DataColumn schemaCol in dt.Columns) {
  71. Console.WriteLine(schemaCol.ColumnName +
  72. " = " +
  73. schemaRow[schemaCol]);
  74. Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
  75. Console.WriteLine();
  76. }
  77. }
  78. }
  79. public static void Setup (OracleConnection con)
  80. {
  81. Console.WriteLine (" Drop table mono_adapter_test ...");
  82. try {
  83. OracleCommand cmd2 = con.CreateCommand ();
  84. cmd2.CommandText = "DROP TABLE mono_adapter_test";
  85. cmd2.ExecuteNonQuery ();
  86. }
  87. catch (OracleException oe1) {
  88. // ignore if table already exists
  89. }
  90. OracleCommand cmd = null;
  91. int rowsAffected = 0;
  92. Console.WriteLine(" Creating table mono_adapter_test...");
  93. cmd = new OracleCommand ();
  94. cmd.Connection = con;
  95. cmd.CommandText = "CREATE TABLE mono_adapter_test ( " +
  96. " varchar2_value VarChar2(32), " +
  97. " number_whole_value Number(18) PRIMARY KEY, " +
  98. " number_scaled_value Number(18,2), " +
  99. " number_integer_value Integer, " +
  100. " float_value Float, " +
  101. " date_value Date, " +
  102. " char_value Char(32), " +
  103. " clob_value Clob, " +
  104. " blob_value Blob ) ";
  105. rowsAffected = cmd.ExecuteNonQuery();
  106. Console.WriteLine(" Begin Trans for table mono_adapter_test...");
  107. OracleTransaction trans = con.BeginTransaction ();
  108. Console.WriteLine(" Inserting value into mono_adapter_test...");
  109. cmd = new OracleCommand();
  110. cmd.Connection = con;
  111. cmd.Transaction = trans;
  112. cmd.CommandText = "INSERT INTO mono_adapter_test " +
  113. " ( varchar2_value, " +
  114. " number_whole_value, " +
  115. " number_scaled_value, " +
  116. " number_integer_value, " +
  117. " float_value, " +
  118. " date_value, " +
  119. " char_value, " +
  120. " clob_value, " +
  121. " blob_value " +
  122. ") " +
  123. " VALUES( " +
  124. " 'Mono', " +
  125. " 11, " +
  126. " 456.78, " +
  127. " 8765, " +
  128. " 235.2, " +
  129. " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
  130. " 'US', " +
  131. " EMPTY_CLOB(), " +
  132. " EMPTY_BLOB() " +
  133. ")";
  134. rowsAffected = cmd.ExecuteNonQuery();
  135. Console.WriteLine(" Select/Update CLOB columns on table mono_adapter_test...");
  136. // update BLOB and CLOB columns
  137. OracleCommand select = con.CreateCommand ();
  138. select.Transaction = trans;
  139. select.CommandText = "SELECT CLOB_VALUE, BLOB_VALUE FROM mono_adapter_test FOR UPDATE";
  140. OracleDataReader reader = select.ExecuteReader ();
  141. if (!reader.Read ())
  142. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  143. // update clob_value
  144. Console.WriteLine(" Update CLOB column on table mono_adapter_test...");
  145. OracleLob clob = reader.GetOracleLob (0);
  146. byte[] bytes = null;
  147. UnicodeEncoding encoding = new UnicodeEncoding ();
  148. bytes = encoding.GetBytes ("Mono is fun!");
  149. clob.Write (bytes, 0, bytes.Length);
  150. clob.Close ();
  151. // update blob_value
  152. Console.WriteLine(" Update BLOB column on table mono_adapter_test...");
  153. OracleLob blob = reader.GetOracleLob (1);
  154. bytes = new byte[6] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x036 };
  155. blob.Write (bytes, 0, bytes.Length);
  156. blob.Close ();
  157. Console.WriteLine(" Commit trans for table mono_adapter_test...");
  158. trans.Commit ();
  159. CommitCursor (con);
  160. }
  161. public static void Insert (OracleConnection con)
  162. {
  163. Console.WriteLine("================================");
  164. Console.WriteLine("=== Adapter Insert =============");
  165. Console.WriteLine("================================");
  166. OracleTransaction transaction = con.BeginTransaction ();
  167. Console.WriteLine(" Create adapter...");
  168. OracleDataAdapter da = new OracleDataAdapter("select * from mono_adapter_test", con);
  169. da.SelectCommand.Transaction = transaction;
  170. Console.WriteLine(" Create command builder...");
  171. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  172. Console.WriteLine(" Create data set ...");
  173. DataSet ds = new DataSet();
  174. Console.WriteLine("Set missing schema action...");
  175. da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  176. Console.WriteLine("Get data from file...");
  177. FileStream fs = new FileStream(infilename, FileMode.OpenOrCreate, FileAccess.Read);
  178. Byte[] mydata = new Byte[fs.Length];
  179. fs.Read(mydata, 0, (int) fs.Length);
  180. fs.Close();
  181. Console.WriteLine("Fill data set via adapter...");
  182. da.Fill(ds, "mono_adapter_test");
  183. Console.WriteLine("New Row...");
  184. DataRow myRow;
  185. myRow = ds.Tables["mono_adapter_test"].NewRow();
  186. byte[] bytes = new byte[] { 0x45,0x46,0x47,0x48,0x49,0x50 };
  187. Console.WriteLine("Set values in the new DataRow...");
  188. myRow["varchar2_value"] = "OracleClient";
  189. myRow["number_whole_value"] = 22;
  190. myRow["number_scaled_value"] = 12.34;
  191. myRow["number_integer_value"] = 456;
  192. myRow["float_value"] = 98.76;
  193. myRow["date_value"] = new DateTime(2001,07,09);
  194. myRow["char_value"] = "Romeo";
  195. myRow["clob_value"] = "clobtest";
  196. myRow["blob_value"] = bytes;
  197. Console.WriteLine("Add DataRow to DataTable...");
  198. ds.Tables["mono_adapter_test"].Rows.Add(myRow);
  199. Console.WriteLine("da.Update(ds...");
  200. da.Update(ds, "mono_adapter_test");
  201. transaction.Commit();
  202. }
  203. public static void Update (OracleConnection con)
  204. {
  205. Console.WriteLine("================================");
  206. Console.WriteLine("=== Adapter Update =============");
  207. Console.WriteLine("================================");
  208. OracleTransaction transaction = con.BeginTransaction ();
  209. Console.WriteLine(" Create adapter...");
  210. OracleCommand selectCmd = con.CreateCommand ();
  211. selectCmd.Transaction = transaction;
  212. selectCmd.CommandText = "SELECT * FROM mono_adapter_test";
  213. OracleDataAdapter da = new OracleDataAdapter(selectCmd);
  214. Console.WriteLine(" Create command builder...");
  215. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  216. Console.WriteLine(" Create data set ...");
  217. DataSet ds = new DataSet();
  218. Console.WriteLine("Set missing schema action...");
  219. da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  220. Console.WriteLine("Fill data set via adapter...");
  221. da.Fill(ds, "mono_adapter_test");
  222. DataRow myRow;
  223. Console.WriteLine("New Row...");
  224. myRow = ds.Tables["mono_adapter_test"].Rows[0];
  225. Console.WriteLine("Tables Count: " + ds.Tables.Count.ToString());
  226. DataTable table = ds.Tables["mono_adapter_test"];
  227. DataRowCollection rows;
  228. rows = table.Rows;
  229. Console.WriteLine("Row Count: " + rows.Count.ToString());
  230. myRow = rows[0];
  231. byte[] bytes = new byte[] { 0x45,0x46,0x47,0x48,0x49,0x50 };
  232. Console.WriteLine("Set values in the new DataRow...");
  233. Console.WriteLine("Columns count: " + table.Columns.Count.ToString());
  234. myRow["varchar2_value"] = "Puppy Power!";
  235. myRow["number_whole_value"] = 33;
  236. myRow["number_scaled_value"] = 12.34;
  237. myRow["number_scaled_value"] = 12.34;
  238. myRow["number_integer_value"] = 456;
  239. myRow["float_value"] = 98.76;
  240. myRow["date_value"] = new DateTime(2001,07,09);
  241. myRow["char_value"] = "Romeo";
  242. myRow["clob_value"] = "clobtest";
  243. myRow["blob_value"] = bytes;
  244. Console.WriteLine("da.Update(ds...");
  245. da.Update(ds, "mono_adapter_test");
  246. transaction.Commit();
  247. }
  248. public static void Delete (OracleConnection con)
  249. {
  250. Console.WriteLine("================================");
  251. Console.WriteLine("=== Adapter Delete =============");
  252. Console.WriteLine("================================");
  253. OracleTransaction transaction = con.BeginTransaction ();
  254. Console.WriteLine(" Create adapter...");
  255. OracleDataAdapter da = new OracleDataAdapter("SELECT * FROM mono_adapter_test", con);
  256. da.SelectCommand.Transaction = transaction;
  257. Console.WriteLine(" Create command builder...");
  258. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  259. Console.WriteLine(" Create data set ...");
  260. DataSet ds = new DataSet();
  261. Console.WriteLine("Set missing schema action...");
  262. da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
  263. Console.WriteLine("Fill data set via adapter...");
  264. da.Fill(ds, "mono_adapter_test");
  265. Console.WriteLine("Get DataRow...");
  266. DataTable table = ds.Tables["mono_adapter_test"];
  267. DataRowCollection rows = table.Rows;
  268. DataRow myRow = rows[0];
  269. Console.WriteLine("row remove...");
  270. rows.Remove(myRow);
  271. Console.WriteLine("da.Update(table...");
  272. da.Update(table);
  273. Console.WriteLine("Commit...");
  274. transaction.Commit();
  275. }
  276. static void CommitCursor (OracleConnection con)
  277. {
  278. OracleCommand cmd = con.CreateCommand ();
  279. cmd.CommandText = "COMMIT";
  280. cmd.ExecuteNonQuery ();
  281. cmd.Dispose ();
  282. cmd = null;
  283. }
  284. static void ReadSimpleTest (OracleConnection con, string selectSql)
  285. {
  286. OracleCommand cmd = null;
  287. OracleDataReader reader = null;
  288. cmd = con.CreateCommand ();
  289. cmd.CommandText = selectSql;
  290. reader = cmd.ExecuteReader ();
  291. Console.WriteLine(" Results...");
  292. Console.WriteLine(" Schema");
  293. DataTable table;
  294. table = reader.GetSchemaTable ();
  295. for (int c = 0; c < reader.FieldCount; c++) {
  296. Console.WriteLine(" Column " + c.ToString ());
  297. DataRow row = table.Rows[c];
  298. string strColumnName = row["ColumnName"].ToString();
  299. string strBaseColumnName = row["BaseColumnName"].ToString();
  300. string strColumnSize = row["ColumnSize"].ToString();
  301. string strNumericScale = row["NumericScale"].ToString();
  302. string strNumericPrecision = row["NumericPrecision"].ToString();
  303. string strDataType = row["DataType"].ToString();
  304. string strBaseTableName = row["BaseTableName"].ToString();
  305. string strBaseSchemaName = row["BaseSchemaName"].ToString();
  306. Console.WriteLine(" ColumnName: " + strColumnName);
  307. Console.WriteLine(" BaseColumnName: " + strBaseColumnName);
  308. Console.WriteLine(" ColumnSize: " + strColumnSize);
  309. Console.WriteLine(" NumericScale: " + strNumericScale);
  310. Console.WriteLine(" NumericPrecision: " + strNumericPrecision);
  311. Console.WriteLine(" DataType: " + strDataType);
  312. Console.WriteLine(" BaseTableName: " + strBaseTableName);
  313. Console.WriteLine(" BaseSchemaName: " + strBaseSchemaName);
  314. }
  315. int r = 0;
  316. Console.WriteLine (" Data");
  317. while (reader.Read ()) {
  318. r++;
  319. Console.WriteLine (" Row: " + r.ToString ());
  320. for (int f = 0; f < reader.FieldCount; f++) {
  321. string sname = "";
  322. object ovalue = "";
  323. string svalue = "";
  324. string sDataType = "";
  325. string sFieldType = "";
  326. string sDataTypeName = "";
  327. string sOraDataType = "";
  328. sname = reader.GetName (f);
  329. if (reader.IsDBNull (f)) {
  330. ovalue = DBNull.Value;
  331. svalue = "";
  332. sDataType = "DBNull.Value";
  333. sOraDataType = "DBNull.Value";
  334. }
  335. else {
  336. ovalue = reader.GetOracleValue (f);
  337. object oravalue = null;
  338. sDataType = ovalue.GetType ().ToString ();
  339. switch (sDataType) {
  340. case "System.Data.OracleClient.OracleString":
  341. oravalue = ((OracleString) ovalue).Value;
  342. break;
  343. case "System.Data.OracleClient.OracleNumber":
  344. oravalue = ((OracleNumber) ovalue).Value;
  345. break;
  346. case "System.Data.OracleClient.OracleLob":
  347. OracleLob lob = (OracleLob) ovalue;
  348. oravalue = lob.Value;
  349. lob.Close ();
  350. break;
  351. case "System.Data.OracleClient.OracleDateTime":
  352. oravalue = ((OracleDateTime) ovalue).Value;
  353. break;
  354. default:
  355. oravalue = "*** no test available ***";
  356. break;
  357. }
  358. sOraDataType = oravalue.GetType ().ToString ();
  359. if (sOraDataType.Equals ("System.Byte[]"))
  360. svalue = GetHexString ((byte[]) oravalue);
  361. else
  362. svalue = oravalue.ToString();
  363. }
  364. sFieldType = reader.GetFieldType(f).ToString();
  365. sDataTypeName = reader.GetDataTypeName(f);
  366. Console.WriteLine(" Field: " + f.ToString());
  367. Console.WriteLine(" Name: " + sname);
  368. Console.WriteLine(" Value: " + svalue);
  369. //Console.WriteLine(" Oracle Data Type: " + sOraDataType);
  370. //Console.WriteLine(" Data Type: " + sDataType);
  371. //Console.WriteLine(" Field Type: " + sFieldType);
  372. //Console.WriteLine(" Data Type Name: " + sDataTypeName);
  373. }
  374. }
  375. if(r == 0)
  376. Console.WriteLine(" No data returned.");
  377. }
  378. public static string GetHexString (byte[] bytes)
  379. {
  380. string bvalue = "";
  381. StringBuilder sb2 = new StringBuilder();
  382. for (int z = 0; z < bytes.Length; z++) {
  383. byte byt = bytes[z];
  384. sb2.Append (byt.ToString("x"));
  385. }
  386. if (sb2.Length > 0)
  387. bvalue = "0x" + sb2.ToString ();
  388. return bvalue;
  389. }
  390. }