TestOracleClient.cs 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345
  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. // To Compile:
  15. // mcs TestOracleClient.cs /r:System.Data.dll /r:System.Data.OracleClient.dll /nowarn:0168
  16. //
  17. // Author:
  18. // Daniel Morgan <[email protected]>
  19. //
  20. // Copyright (C) Daniel Morgan, 2002, 2004-2005, 2008
  21. //
  22. using System;
  23. using System.IO;
  24. using System.Runtime.InteropServices;
  25. using System.Data;
  26. using System.Data.OracleClient;
  27. using System.Text;
  28. using System.Threading;
  29. namespace Test.OracleClient
  30. {
  31. public class OracleTest
  32. {
  33. private static Thread t = null;
  34. private static string conStr;
  35. public static readonly int MAX_CONNECTIONS = 30; // max connections default to 100, but I will set to 30.
  36. public OracleTest()
  37. {
  38. }
  39. static void MonoTest(OracleConnection con)
  40. {
  41. Console.WriteLine (" Drop table MONO_ORACLE_TEST ...");
  42. try {
  43. OracleCommand cmd2 = con.CreateCommand ();
  44. cmd2.CommandText = "DROP TABLE MONO_ORACLE_TEST";
  45. cmd2.ExecuteNonQuery ();
  46. }
  47. catch (OracleException) {
  48. // ignore if table already exists
  49. }
  50. OracleCommand cmd = null;
  51. Console.WriteLine(" Creating table MONO_ORACLE_TEST...");
  52. cmd = new OracleCommand();
  53. cmd.Connection = con;
  54. cmd.CommandText = "CREATE TABLE MONO_ORACLE_TEST ( " +
  55. " varchar2_value VarChar2(32), " +
  56. " long_value long, " +
  57. " number_whole_value Number(18), " +
  58. " number_scaled_value Number(18,2), " +
  59. " number_integer_value Integer, " +
  60. " float_value Float, " +
  61. " date_value Date, " +
  62. " char_value Char(32), " +
  63. " clob_value Clob, " +
  64. " blob_value Blob, " +
  65. " clob_empty_value Clob, " +
  66. " blob_empty_value Blob, " +
  67. " varchar2_null_value VarChar2(32), " +
  68. " number_whole_null_value Number(18), " +
  69. " number_scaled_null_value Number(18,2), " +
  70. " number_integer_null_value Integer, " +
  71. " float_null_value Float, " +
  72. " date_null_value Date, " +
  73. " char_null_value Char(32), " +
  74. " clob_null_value Clob, " +
  75. " blob_null_value Blob " +
  76. ")";
  77. cmd.ExecuteNonQuery();
  78. Console.WriteLine(" Begin Trans for table MONO_ORACLE_TEST...");
  79. OracleTransaction trans = con.BeginTransaction ();
  80. Console.WriteLine(" Inserting value into MONO_ORACLE_TEST...");
  81. cmd = new OracleCommand();
  82. cmd.Connection = con;
  83. cmd.Transaction = trans;
  84. cmd.CommandText = "INSERT INTO mono_oracle_test " +
  85. " ( varchar2_value, " +
  86. " long_value, " +
  87. " number_whole_value, " +
  88. " number_scaled_value, " +
  89. " number_integer_value, " +
  90. " float_value, " +
  91. " date_value, " +
  92. " char_value, " +
  93. " clob_value, " +
  94. " blob_value, " +
  95. " clob_empty_value, " +
  96. " blob_empty_value " +
  97. ") " +
  98. " VALUES( " +
  99. " 'Mono', " +
  100. " 'This is a LONG column', " +
  101. " 123, " +
  102. " 456.78, " +
  103. " 8765, " +
  104. " 235.2, " +
  105. " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
  106. " 'US', " +
  107. " EMPTY_CLOB(), " +
  108. " EMPTY_BLOB()," +
  109. " EMPTY_CLOB(), " +
  110. " EMPTY_BLOB()" +
  111. ")";
  112. cmd.ExecuteNonQuery();
  113. Console.WriteLine(" Select/Update CLOB columns on table MONO_ORACLE_TEST...");
  114. // update BLOB and CLOB columns
  115. OracleCommand select = con.CreateCommand ();
  116. select.Transaction = trans;
  117. select.CommandText = "SELECT CLOB_VALUE, BLOB_VALUE FROM MONO_ORACLE_TEST FOR UPDATE";
  118. OracleDataReader reader = select.ExecuteReader ();
  119. if (!reader.Read ())
  120. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  121. // update clob_value
  122. Console.WriteLine(" Update CLOB column on table MONO_ORACLE_TEST...");
  123. OracleLob clob = reader.GetOracleLob (0);
  124. byte[] bytes = null;
  125. UnicodeEncoding encoding = new UnicodeEncoding ();
  126. bytes = encoding.GetBytes ("Mono is fun!");
  127. clob.Write (bytes, 0, bytes.Length);
  128. clob.Close ();
  129. // update blob_value
  130. Console.WriteLine(" Update BLOB column on table MONO_ORACLE_TEST...");
  131. OracleLob blob = reader.GetOracleLob (1);
  132. bytes = new byte[6] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x036 };
  133. blob.Write (bytes, 0, bytes.Length);
  134. blob.Close ();
  135. Console.WriteLine(" Commit trans for table MONO_ORACLE_TEST...");
  136. trans.Commit ();
  137. // OracleCommand.ExecuteReader of MONO_ORACLE_TEST table
  138. Console.WriteLine(" Read simple test for table MONO_ORACLE_TEST...");
  139. ReadSimpleTest(con, "SELECT * FROM MONO_ORACLE_TEST");
  140. // OracleCommand.ExecuteScalar
  141. Console.WriteLine(" -ExecuteScalar tests...");
  142. string varchar2_value = (string) ReadScalar (con,"SELECT MAX(varchar2_value) FROM MONO_ORACLE_TEST");
  143. Console.WriteLine(" String Value: " + varchar2_value);
  144. Console.WriteLine(" Read Scalar: number_whole_value");
  145. decimal number_whole_value = (decimal)
  146. ReadScalar (con,"SELECT MAX(number_whole_value) FROM MONO_ORACLE_TEST");
  147. Console.WriteLine(" Int32 Value: " + number_whole_value.ToString());
  148. Console.WriteLine(" Read Scalar: number_scaled_value");
  149. decimal number_scaled_value = (decimal)
  150. ReadScalar (con,"SELECT number_scaled_value FROM MONO_ORACLE_TEST");
  151. Console.WriteLine(" Decimal Value: " + number_scaled_value.ToString());
  152. Console.WriteLine(" Read Scalar: date_value");
  153. DateTime date_value = (DateTime)
  154. ReadScalar (con,"SELECT date_value FROM MONO_ORACLE_TEST");
  155. Console.WriteLine(" DateTime Value: " + date_value.ToString());
  156. Console.WriteLine(" Read Scalar: clob_value");
  157. string clob_value = (string)
  158. ReadScalar (con,"SELECT clob_value FROM MONO_ORACLE_TEST");
  159. Console.WriteLine(" CLOB Value: " + clob_value);
  160. Console.WriteLine(" Read Scalar: blob_value");
  161. byte[] blob_value = (byte[])
  162. ReadScalar (con,"SELECT blob_value FROM MONO_ORACLE_TEST");
  163. string sblob_value = GetHexString (blob_value);
  164. Console.WriteLine(" BLOB Value: " + sblob_value);
  165. // OracleCommand.ExecuteOracleScalar
  166. Console.WriteLine(" -ExecuteOracleScalar tests...");
  167. Console.WriteLine(" Read Oracle Scalar: varchar2_value");
  168. ReadOracleScalar (con,"SELECT MAX(varchar2_value) FROM MONO_ORACLE_TEST");
  169. Console.WriteLine(" Read Oracle Scalar: number_whole_value");
  170. ReadOracleScalar (con,"SELECT MAX(number_whole_value) FROM MONO_ORACLE_TEST");
  171. Console.WriteLine(" Read Oracle Scalar: number_scaled_value");
  172. ReadOracleScalar (con,"SELECT number_scaled_value FROM MONO_ORACLE_TEST");
  173. Console.WriteLine(" Read Oracle Scalar: date_value");
  174. ReadOracleScalar (con,"SELECT date_value FROM MONO_ORACLE_TEST");
  175. Console.WriteLine(" Read Oracle Scalar: clob_value");
  176. ReadOracleScalar (con,"SELECT clob_value FROM MONO_ORACLE_TEST");
  177. Console.WriteLine(" Read Oracle Scalar: blob_value");
  178. ReadOracleScalar (con,"SELECT blob_value FROM MONO_ORACLE_TEST");
  179. }
  180. static object ReadScalar (OracleConnection con, string selectSql)
  181. {
  182. OracleCommand cmd = null;
  183. cmd = con.CreateCommand();
  184. cmd.CommandText = selectSql;
  185. object o = cmd.ExecuteScalar ();
  186. string dataType = o.GetType ().ToString ();
  187. Console.WriteLine (" DataType: " + dataType);
  188. return o;
  189. }
  190. static void ReadOracleScalar (OracleConnection con, string selectSql)
  191. {
  192. OracleCommand cmd = null;
  193. cmd = con.CreateCommand();
  194. cmd.CommandText = selectSql;
  195. object o = cmd.ExecuteOracleScalar ();
  196. string dataType = o.GetType ().ToString ();
  197. Console.WriteLine (" DataType: " + dataType);
  198. if (dataType.Equals("System.Data.OracleClient.OracleLob"))
  199. o = ((OracleLob) o).Value;
  200. if (o.GetType ().ToString ().Equals ("System.Byte[]"))
  201. o = GetHexString ((byte[])o);
  202. Console.WriteLine (" Value: " + o.ToString ());
  203. }
  204. static void ReadSimpleTest(OracleConnection con, string selectSql)
  205. {
  206. OracleCommand cmd = null;
  207. OracleDataReader reader = null;
  208. cmd = con.CreateCommand();
  209. cmd.CommandText = selectSql;
  210. reader = cmd.ExecuteReader();
  211. Console.WriteLine(" Results...");
  212. Console.WriteLine(" Schema");
  213. DataTable table;
  214. table = reader.GetSchemaTable();
  215. for(int c = 0; c < reader.FieldCount; c++) {
  216. Console.WriteLine(" Column " + c.ToString());
  217. DataRow row = table.Rows[c];
  218. string strColumnName = row["ColumnName"].ToString();
  219. string strBaseColumnName = row["BaseColumnName"].ToString();
  220. string strColumnSize = row["ColumnSize"].ToString();
  221. string strNumericScale = row["NumericScale"].ToString();
  222. string strNumericPrecision = row["NumericPrecision"].ToString();
  223. string strDataType = row["DataType"].ToString();
  224. Console.WriteLine(" ColumnName: " + strColumnName);
  225. Console.WriteLine(" BaseColumnName: " + strBaseColumnName);
  226. Console.WriteLine(" ColumnSize: " + strColumnSize);
  227. Console.WriteLine(" NumericScale: " + strNumericScale);
  228. Console.WriteLine(" NumericPrecision: " + strNumericPrecision);
  229. Console.WriteLine(" DataType: " + strDataType);
  230. }
  231. int r = 0;
  232. Console.WriteLine (" Data");
  233. while (reader.Read ()) {
  234. r++;
  235. Console.WriteLine (" Row: " + r.ToString ());
  236. for (int f = 0; f < reader.FieldCount; f++) {
  237. string sname = "";
  238. object ovalue = "";
  239. string svalue = "";
  240. string sDataType = "";
  241. string sFieldType = "";
  242. string sDataTypeName = "";
  243. string sOraDataType = "";
  244. sname = reader.GetName (f);
  245. if (reader.IsDBNull (f)) {
  246. ovalue = DBNull.Value;
  247. svalue = "";
  248. sDataType = "DBNull.Value";
  249. sOraDataType = "DBNull.Value";
  250. }
  251. else {
  252. //ovalue = reader.GetValue (f);
  253. ovalue = reader.GetOracleValue (f);
  254. object oravalue = null;
  255. sDataType = ovalue.GetType ().ToString ();
  256. switch (sDataType) {
  257. case "System.Data.OracleClient.OracleString":
  258. oravalue = ((OracleString) ovalue).Value;
  259. break;
  260. case "System.Data.OracleClient.OracleNumber":
  261. oravalue = ((OracleNumber) ovalue).Value;
  262. break;
  263. case "System.Data.OracleClient.OracleLob":
  264. OracleLob lob = (OracleLob) ovalue;
  265. oravalue = lob.Value;
  266. lob.Close ();
  267. break;
  268. case "System.Data.OracleClient.OracleDateTime":
  269. oravalue = ((OracleDateTime) ovalue).Value;
  270. break;
  271. case "System.Byte[]":
  272. oravalue = GetHexString((byte[])ovalue);
  273. break;
  274. case "System.Decimal":
  275. //Console.WriteLine(" *** Get Decimal, Int16, Int32, Int64, Float, Double, ...");
  276. decimal dec = reader.GetDecimal (f);
  277. //Console.WriteLine(" GetDecimal: " + dec.ToString ());
  278. oravalue = (object) dec;
  279. /*
  280. try {
  281. reader.GetInt16 (f);
  282. } catch (NotSupportedException e) {
  283. Console.WriteLine (" ** Expected exception caught for GetInt16: NotSupportedException: " + e.Message);
  284. }
  285. try {
  286. long lng = reader.GetInt64 (f);
  287. Console.WriteLine(" GetInt64: " + lng.ToString ());
  288. int n = reader.GetInt32 (f);
  289. Console.WriteLine(" GetInt32: " + n.ToString ());
  290. float flt = reader.GetFloat (f);
  291. Console.WriteLine(" GetFloat: " + flt.ToString ());
  292. double dbl = reader.GetDouble (f);
  293. Console.WriteLine(" GetDouble: " + dbl.ToString ());
  294. } catch (OverflowException oe1) {
  295. Console.WriteLine (" ** Overflow exception for numbers to big or too small: " + oe1.Message);
  296. }
  297. */
  298. break;
  299. default:
  300. oravalue = ovalue.ToString ();
  301. break;
  302. }
  303. sOraDataType = oravalue.GetType ().ToString ();
  304. if (sOraDataType.Equals ("System.Byte[]"))
  305. svalue = GetHexString ((byte[]) oravalue);
  306. else
  307. svalue = oravalue.ToString();
  308. }
  309. sFieldType = reader.GetFieldType(f).ToString();
  310. sDataTypeName = reader.GetDataTypeName(f);
  311. Console.WriteLine(" Field: " + f.ToString());
  312. Console.WriteLine(" Name: " + sname);
  313. Console.WriteLine(" Value: " + svalue);
  314. Console.WriteLine(" Oracle Data Type: " + sOraDataType);
  315. Console.WriteLine(" Data Type: " + sDataType);
  316. Console.WriteLine(" Field Type: " + sFieldType);
  317. Console.WriteLine(" Data Type Name: " + sDataTypeName);
  318. }
  319. }
  320. if(r == 0)
  321. Console.WriteLine(" No data returned.");
  322. }
  323. static void DataAdapterTest (OracleConnection connection)
  324. {
  325. Console.WriteLine(" Create select command...");
  326. OracleCommand command = connection.CreateCommand ();
  327. command.CommandText = "SELECT * FROM SCOTT.EMP";
  328. Console.WriteLine(" Create data adapter...");
  329. OracleDataAdapter adapter = new OracleDataAdapter (command);
  330. Console.WriteLine(" Create DataSet...");
  331. DataSet dataSet = new DataSet ("EMP");
  332. Console.WriteLine(" Fill DataSet via data adapter...");
  333. adapter.Fill (dataSet);
  334. Console.WriteLine(" Get DataTable...");
  335. DataTable table = dataSet.Tables [0];
  336. Console.WriteLine(" Display each row...");
  337. int rowCount = 0;
  338. foreach (DataRow row in table.Rows) {
  339. Console.WriteLine (" row {0}", rowCount + 1);
  340. for (int i = 0; i < table.Columns.Count; i += 1) {
  341. Console.WriteLine (" {0}: {1}", table.Columns [i].ColumnName, row [i]);
  342. }
  343. Console.WriteLine ();
  344. rowCount += 1;
  345. }
  346. }
  347. public static void DataAdapterTest2 (OracleConnection con)
  348. {
  349. DataAdapterTest2_Setup (con);
  350. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  351. GetMetaData (con, "SELECT * FROM mono_adapter_test");
  352. DataAdapterTest2_Insert (con);
  353. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  354. DataAdapterTest2_Update (con);
  355. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  356. DataAdapterTest2_Delete (con);
  357. ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
  358. }
  359. public static void GetMetaData (OracleConnection con, string sql)
  360. {
  361. OracleCommand cmd = null;
  362. OracleDataReader rdr = null;
  363. cmd = con.CreateCommand();
  364. cmd.CommandText = sql;
  365. Console.WriteLine("Read Schema With KeyInfo");
  366. rdr = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly);
  367. DataTable dt;
  368. dt = rdr.GetSchemaTable();
  369. foreach (DataRow schemaRow in dt.Rows) {
  370. foreach (DataColumn schemaCol in dt.Columns) {
  371. Console.WriteLine(schemaCol.ColumnName +
  372. " = " +
  373. schemaRow[schemaCol]);
  374. Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
  375. }
  376. Console.WriteLine("");
  377. }
  378. Console.WriteLine("Read Schema with No KeyInfo");
  379. rdr = cmd.ExecuteReader();
  380. dt = rdr.GetSchemaTable();
  381. foreach (DataRow schemaRow in dt.Rows) {
  382. foreach (DataColumn schemaCol in dt.Columns) {
  383. Console.WriteLine(schemaCol.ColumnName +
  384. " = " +
  385. schemaRow[schemaCol]);
  386. Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
  387. Console.WriteLine();
  388. }
  389. }
  390. }
  391. public static void DataAdapterTest2_Setup (OracleConnection con)
  392. {
  393. Console.WriteLine (" Drop table mono_adapter_test ...");
  394. try {
  395. OracleCommand cmd2 = con.CreateCommand ();
  396. cmd2.CommandText = "DROP TABLE mono_adapter_test";
  397. cmd2.ExecuteNonQuery ();
  398. }
  399. catch (OracleException) {
  400. // ignore if table already exists
  401. }
  402. OracleCommand cmd = null;
  403. Console.WriteLine(" Creating table mono_adapter_test...");
  404. cmd = new OracleCommand ();
  405. cmd.Connection = con;
  406. cmd.CommandText = "CREATE TABLE mono_adapter_test ( " +
  407. " varchar2_value VarChar2(32), " +
  408. " number_whole_value Number(18) PRIMARY KEY, " +
  409. " number_scaled_value Number(18,2), " +
  410. " number_integer_value Integer, " +
  411. " float_value Float, " +
  412. " date_value Date, " +
  413. " clob_value Clob, " +
  414. " blob_value Blob ) ";
  415. // FIXME: char_value does not work
  416. /*
  417. cmd.CommandText = "CREATE TABLE mono_adapter_test ( " +
  418. " varchar2_value VarChar2(32), " +
  419. " number_whole_value Number(18) PRIMARY KEY, " +
  420. " number_scaled_value Number(18,2), " +
  421. " number_integer_value Integer, " +
  422. " float_value Float, " +
  423. " date_value Date, " +
  424. " char_value Char(32), " +
  425. " clob_value Clob, " +
  426. " blob_value Blob ) ";
  427. */
  428. cmd.ExecuteNonQuery();
  429. Console.WriteLine(" Begin Trans for table mono_adapter_test...");
  430. OracleTransaction trans = con.BeginTransaction ();
  431. Console.WriteLine(" Inserting value into mono_adapter_test...");
  432. cmd = new OracleCommand();
  433. cmd.Connection = con;
  434. cmd.Transaction = trans;
  435. cmd.CommandText = "INSERT INTO mono_adapter_test " +
  436. " ( varchar2_value, " +
  437. " number_whole_value, " +
  438. " number_scaled_value, " +
  439. " number_integer_value, " +
  440. " float_value, " +
  441. " date_value, " +
  442. " clob_value, " +
  443. " blob_value " +
  444. ") " +
  445. " VALUES( " +
  446. " 'Mono', " +
  447. " 11, " +
  448. " 456.78, " +
  449. " 8765, " +
  450. " 235.2, " +
  451. " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
  452. " EMPTY_CLOB(), " +
  453. " EMPTY_BLOB() " +
  454. ")";
  455. /*
  456. cmd.CommandText = "INSERT INTO mono_adapter_test " +
  457. " ( varchar2_value, " +
  458. " number_whole_value, " +
  459. " number_scaled_value, " +
  460. " number_integer_value, " +
  461. " float_value, " +
  462. " date_value, " +
  463. " char_value, " +
  464. " clob_value, " +
  465. " blob_value " +
  466. ") " +
  467. " VALUES( " +
  468. " 'Mono', " +
  469. " 11, " +
  470. " 456.78, " +
  471. " 8765, " +
  472. " 235.2, " +
  473. " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
  474. " 'US', " +
  475. " EMPTY_CLOB(), " +
  476. " EMPTY_BLOB() " +
  477. ")";
  478. */
  479. cmd.ExecuteNonQuery();
  480. Console.WriteLine(" Select/Update CLOB columns on table mono_adapter_test...");
  481. // update BLOB and CLOB columns
  482. OracleCommand select = con.CreateCommand ();
  483. select.Transaction = trans;
  484. select.CommandText = "SELECT CLOB_VALUE, BLOB_VALUE FROM mono_adapter_test FOR UPDATE";
  485. OracleDataReader reader = select.ExecuteReader ();
  486. if (!reader.Read ())
  487. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  488. // update clob_value
  489. Console.WriteLine(" Update CLOB column on table mono_adapter_test...");
  490. OracleLob clob = reader.GetOracleLob (0);
  491. byte[] bytes = null;
  492. UnicodeEncoding encoding = new UnicodeEncoding ();
  493. bytes = encoding.GetBytes ("Mono is fun!");
  494. clob.Write (bytes, 0, bytes.Length);
  495. clob.Close ();
  496. // update blob_value
  497. Console.WriteLine(" Update BLOB column on table mono_adapter_test...");
  498. OracleLob blob = reader.GetOracleLob (1);
  499. bytes = new byte[6] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x036 };
  500. blob.Write (bytes, 0, bytes.Length);
  501. blob.Close ();
  502. Console.WriteLine(" Commit trans for table mono_adapter_test...");
  503. trans.Commit ();
  504. CommitCursor (con);
  505. }
  506. public static void DataAdapterTest2_Insert (OracleConnection con)
  507. {
  508. Console.WriteLine("================================");
  509. Console.WriteLine("=== Adapter Insert =============");
  510. Console.WriteLine("================================");
  511. OracleTransaction transaction = con.BeginTransaction ();
  512. Console.WriteLine(" Create adapter...");
  513. OracleDataAdapter da = new OracleDataAdapter("select * from mono_adapter_test", con);
  514. da.SelectCommand.Transaction = transaction;
  515. Console.WriteLine(" Create command builder...");
  516. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  517. Console.WriteLine(" Create data set ...");
  518. DataSet ds = new DataSet();
  519. Console.WriteLine(" Fill data set via adapter...");
  520. da.Fill(ds, "mono_adapter_test");
  521. Console.WriteLine(" New Row...");
  522. DataRow myRow;
  523. myRow = ds.Tables["mono_adapter_test"].NewRow();
  524. byte[] bytes = new byte[] { 0x45,0x46,0x47,0x48,0x49,0x50 };
  525. Console.WriteLine(" Set values in the new DataRow...");
  526. myRow["varchar2_value"] = "OracleClient";
  527. myRow["number_whole_value"] = 22;
  528. myRow["number_scaled_value"] = 12.34;
  529. myRow["number_integer_value"] = 456;
  530. myRow["float_value"] = 98.76;
  531. myRow["date_value"] = new DateTime(2001,07,09);
  532. Console.WriteLine(" *** FIXME; char value not working");
  533. //myRow["char_value"] = "Romeo";
  534. myRow["clob_value"] = "clobtest";
  535. myRow["blob_value"] = bytes;
  536. Console.WriteLine(" Add DataRow to DataTable...");
  537. ds.Tables["mono_adapter_test"].Rows.Add(myRow);
  538. Console.WriteLine("da.Update(ds...");
  539. da.Update(ds, "mono_adapter_test");
  540. transaction.Commit();
  541. mycb.Dispose();
  542. mycb = null;
  543. }
  544. public static void DataAdapterTest2_Update (OracleConnection con)
  545. {
  546. Console.WriteLine("================================");
  547. Console.WriteLine("=== Adapter Update =============");
  548. Console.WriteLine("================================");
  549. OracleTransaction transaction = con.BeginTransaction ();
  550. Console.WriteLine(" Create adapter...");
  551. OracleCommand selectCmd = con.CreateCommand ();
  552. selectCmd.Transaction = transaction;
  553. selectCmd.CommandText = "SELECT * FROM mono_adapter_test";
  554. OracleDataAdapter da = new OracleDataAdapter(selectCmd);
  555. Console.WriteLine(" Create command builder...");
  556. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  557. Console.WriteLine(" Create data set ...");
  558. DataSet ds = new DataSet();
  559. Console.WriteLine(" Set missing schema action...");
  560. Console.WriteLine(" Fill data set via adapter...");
  561. da.Fill(ds, "mono_adapter_test");
  562. DataRow myRow;
  563. Console.WriteLine(" New Row...");
  564. myRow = ds.Tables["mono_adapter_test"].Rows[0];
  565. Console.WriteLine("Tables Count: " + ds.Tables.Count.ToString());
  566. DataTable table = ds.Tables["mono_adapter_test"];
  567. DataRowCollection rows;
  568. rows = table.Rows;
  569. Console.WriteLine(" Row Count: " + rows.Count.ToString());
  570. myRow = rows[0];
  571. byte[] bytes = new byte[] { 0x62,0x63,0x64,0x65,0x66,0x67 };
  572. Console.WriteLine(" Set values in the new DataRow...");
  573. myRow["varchar2_value"] = "Super Power!";
  574. myRow["number_scaled_value"] = 12.35;
  575. myRow["number_integer_value"] = 457;
  576. myRow["float_value"] = 198.76;
  577. myRow["date_value"] = new DateTime(2002,08,09);
  578. //myRow["char_value"] = "Juliet";
  579. myRow["clob_value"] = "this is a clob";
  580. myRow["blob_value"] = bytes;
  581. Console.WriteLine("da.Update(ds...");
  582. da.Update(ds, "mono_adapter_test");
  583. transaction.Commit();
  584. mycb.Dispose();
  585. mycb = null;
  586. }
  587. public static void DataAdapterTest2_Delete (OracleConnection con)
  588. {
  589. Console.WriteLine("================================");
  590. Console.WriteLine("=== Adapter Delete =============");
  591. Console.WriteLine("================================");
  592. OracleTransaction transaction = con.BeginTransaction ();
  593. Console.WriteLine(" Create adapter...");
  594. OracleDataAdapter da = new OracleDataAdapter("SELECT * FROM mono_adapter_test", con);
  595. Console.WriteLine(" Create command builder...");
  596. OracleCommandBuilder mycb = new OracleCommandBuilder(da);
  597. Console.WriteLine(" set transr...");
  598. da.SelectCommand.Transaction = transaction;
  599. Console.WriteLine(" Create data set ...");
  600. DataSet ds = new DataSet();
  601. Console.WriteLine("Fill data set via adapter...");
  602. da.Fill(ds, "mono_adapter_test");
  603. Console.WriteLine("delete row...");
  604. ds.Tables["mono_adapter_test"].Rows[0].Delete();
  605. Console.WriteLine("da.Update(table...");
  606. da.Update(ds, "mono_adapter_test");
  607. Console.WriteLine("Commit...");
  608. transaction.Commit();
  609. mycb.Dispose();
  610. mycb = null;
  611. }
  612. static void TestNonQueryUsingExecuteReader(OracleConnection con)
  613. {
  614. OracleDataReader reader = null;
  615. OracleTransaction trans = null;
  616. Console.WriteLine(" drop table mono_adapter_test...");
  617. OracleCommand cmd = con.CreateCommand();
  618. cmd.CommandText = "DROP TABLE MONO_ADAPTER_TEST";
  619. trans = con.BeginTransaction();
  620. cmd.Transaction = trans;
  621. try {
  622. reader = cmd.ExecuteReader();
  623. Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
  624. reader.Read();
  625. Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
  626. reader.Close();
  627. Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
  628. trans.Commit();
  629. }
  630. catch(OracleException e) {
  631. Console.WriteLine(" OracleException caught: " + e.Message);
  632. trans.Commit();
  633. }
  634. Console.WriteLine(" Create table mono_adapter_test...");
  635. cmd.CommandText = "CREATE TABLE MONO_ADAPTER_TEST ( " +
  636. " varchar2_value VarChar2(32), " +
  637. " number_whole_value Number(18,0) PRIMARY KEY ) ";
  638. trans = con.BeginTransaction();
  639. cmd.Transaction = trans;
  640. reader = cmd.ExecuteReader();
  641. Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
  642. reader.Read();
  643. Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
  644. reader.Close();
  645. Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
  646. trans.Commit();
  647. Console.WriteLine("Insert into table mono_adapter_test...");
  648. string sql =
  649. "INSERT INTO MONO_ADAPTER_TEST " +
  650. "(VARCHAR2_VALUE,NUMBER_WHOLE_VALUE) " +
  651. "VALUES(:p1,:p2)";
  652. OracleCommand cmd2 = con.CreateCommand();
  653. trans = con.BeginTransaction();
  654. cmd2.Transaction = trans;
  655. cmd2.CommandText = sql;
  656. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.VarChar, 32);
  657. myParameter1.Direction = ParameterDirection.Input;
  658. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Number);
  659. myParameter2.Direction = ParameterDirection.Input;
  660. myParameter2.Value = 182;
  661. myParameter1.Value = "Mono";
  662. cmd2.Parameters.Add (myParameter1);
  663. cmd2.Parameters.Add (myParameter2);
  664. // insert 1 record
  665. reader = cmd2.ExecuteReader();
  666. Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
  667. reader.Read();
  668. Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
  669. reader.Close();
  670. Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
  671. // insert another record
  672. Console.WriteLine(" Insert another record...");
  673. myParameter2.Value = 183;
  674. myParameter1.Value = "Oracle";
  675. reader = cmd2.ExecuteReader();
  676. Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
  677. reader.Read();
  678. Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
  679. reader.Close();
  680. Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
  681. trans.Commit();
  682. trans = null;
  683. ReadSimpleTest(con, "SELECT * FROM MONO_ADAPTER_TEST");
  684. }
  685. static void CommitCursor (OracleConnection con)
  686. {
  687. OracleCommand cmd = con.CreateCommand ();
  688. cmd.CommandText = "COMMIT";
  689. cmd.ExecuteNonQuery ();
  690. cmd.Dispose ();
  691. cmd = null;
  692. }
  693. static void RollbackTest (OracleConnection connection)
  694. {
  695. OracleTransaction transaction = connection.BeginTransaction ();
  696. OracleCommand insert = connection.CreateCommand ();
  697. insert.Transaction = transaction;
  698. insert.CommandText = "INSERT INTO SCOTT.EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
  699. Console.WriteLine (" Inserting record ...");
  700. insert.ExecuteNonQuery ();
  701. OracleCommand select = connection.CreateCommand ();
  702. select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
  703. select.Transaction = transaction;
  704. OracleDataReader reader = select.ExecuteReader ();
  705. reader.Read ();
  706. Console.WriteLine (" Row count SHOULD BE 1, VALUE IS {0}", reader.GetValue (0));
  707. reader.Close ();
  708. Console.WriteLine (" Rolling back transaction ...");
  709. transaction.Rollback ();
  710. select = connection.CreateCommand ();
  711. select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
  712. reader = select.ExecuteReader ();
  713. reader.Read ();
  714. Console.WriteLine (" Row count SHOULD BE 0, VALUE IS {0}", reader.GetValue (0));
  715. reader.Close ();
  716. }
  717. static void CommitTest (OracleConnection connection)
  718. {
  719. OracleTransaction transaction = connection.BeginTransaction ();
  720. OracleCommand insert = connection.CreateCommand ();
  721. insert.Transaction = transaction;
  722. insert.CommandText = "INSERT INTO SCOTT.EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
  723. Console.WriteLine (" Inserting record ...");
  724. insert.ExecuteNonQuery ();
  725. OracleCommand select = connection.CreateCommand ();
  726. select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
  727. select.Transaction = transaction;
  728. Console.WriteLine (" Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
  729. Console.WriteLine (" Committing transaction ...");
  730. transaction.Commit ();
  731. select = connection.CreateCommand ();
  732. select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
  733. Console.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
  734. transaction = connection.BeginTransaction ();
  735. OracleCommand delete = connection.CreateCommand ();
  736. delete.Transaction = transaction;
  737. delete.CommandText = "DELETE FROM SCOTT.EMP WHERE EMPNO = 8787";
  738. delete.ExecuteNonQuery ();
  739. transaction.Commit ();
  740. }
  741. public static void ParameterTest2 (OracleConnection connection)
  742. {
  743. Console.WriteLine(" Setting NLS_DATE_FORMAT...");
  744. OracleCommand cmd2 = connection.CreateCommand();
  745. cmd2.CommandText = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
  746. cmd2.ExecuteNonQuery ();
  747. Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
  748. try {
  749. cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE7";
  750. cmd2.ExecuteNonQuery ();
  751. }
  752. catch(OracleException) {
  753. // ignore if table already exists
  754. }
  755. Console.WriteLine(" Create table MONO_TEST_TABLE7...");
  756. cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE7(" +
  757. " COL1 VARCHAR2(8) NOT NULL, " +
  758. " COL2 VARCHAR2(32), " +
  759. " COL3 NUMBER(18,2), " +
  760. " COL4 NUMBER(18,2), " +
  761. " COL5 DATE NOT NULL, " +
  762. " COL6 DATE, " +
  763. " COL7 BLOB NOT NULL, " +
  764. " COL8 BLOB, " +
  765. " COL9 CLOB NOT NULL, " +
  766. " COL10 CLOB " +
  767. ")";
  768. cmd2.ExecuteNonQuery ();
  769. Console.WriteLine(" COMMIT...");
  770. cmd2.CommandText = "COMMIT";
  771. cmd2.ExecuteNonQuery ();
  772. Console.WriteLine(" create insert command...");
  773. OracleTransaction trans = connection.BeginTransaction ();
  774. OracleCommand cmd = connection.CreateCommand ();
  775. cmd.Transaction = trans;
  776. cmd.CommandText = "INSERT INTO MONO_TEST_TABLE7 " +
  777. "(COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10) " +
  778. "VALUES(:P1,:P2,:P3,:P4,:P5,:P6,:P7,:P8,:P9,:P10)";
  779. Console.WriteLine(" Add parameters...");
  780. OracleParameter parm1 = cmd.Parameters.Add (":P1", OracleType.VarChar, 8);
  781. OracleParameter parm2 = cmd.Parameters.Add (":P2", OracleType.VarChar, 32);
  782. OracleParameter parm3 = cmd.Parameters.Add (":P3", OracleType.Number);
  783. OracleParameter parm4 = cmd.Parameters.Add (":P4", OracleType.Number);
  784. OracleParameter parm5 = cmd.Parameters.Add (":P5", OracleType.DateTime);
  785. OracleParameter parm6 = cmd.Parameters.Add (":P6", OracleType.DateTime);
  786. // FIXME: fix BLOBs and CLOBs in OracleParameter
  787. OracleParameter parm7 = cmd.Parameters.Add (":P7", OracleType.Blob);
  788. OracleParameter parm8 = cmd.Parameters.Add (":P8", OracleType.Blob);
  789. OracleParameter parm9 = cmd.Parameters.Add (":P9", OracleType.Clob);
  790. OracleParameter parm10 = cmd.Parameters.Add (":P10", OracleType.Clob);
  791. // TODO: implement out, return, and ref parameters
  792. string s = "Mono";
  793. decimal d = 123456789012345.678M;
  794. DateTime dt = DateTime.Now;
  795. string clob = "Clob";
  796. byte[] blob = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
  797. Console.WriteLine(" Set Values...");
  798. parm1.Value = s;
  799. parm2.Value = DBNull.Value;
  800. parm3.Value = d;
  801. parm4.Value = DBNull.Value;
  802. parm5.Value = dt;
  803. parm6.Value = DBNull.Value;
  804. parm7.Value = blob;
  805. parm8.Value = DBNull.Value;
  806. parm9.Value = clob;
  807. parm10.Value = DBNull.Value;
  808. Console.WriteLine(" ExecuteNonQuery...");
  809. cmd.ExecuteNonQuery ();
  810. trans.Commit();
  811. }
  812. public static void ParameterTest (OracleConnection connection)
  813. {
  814. Console.WriteLine(" Setting NLS_DATE_FORMAT...");
  815. OracleCommand cmd2 = connection.CreateCommand();
  816. cmd2.CommandText = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
  817. cmd2.ExecuteNonQuery ();
  818. Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
  819. try {
  820. cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE7";
  821. cmd2.ExecuteNonQuery ();
  822. }
  823. catch(OracleException) {
  824. // ignore if table already exists
  825. }
  826. Console.WriteLine(" Create table MONO_TEST_TABLE7...");
  827. cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE7(" +
  828. " COL1 VARCHAR2(8) NOT NULL, " +
  829. " COL2 VARCHAR2(32), " +
  830. " COL3 NUMBER(18,2) NOT NULL, " +
  831. " COL4 NUMBER(18,2), " +
  832. " COL5 DATE NOT NULL, " +
  833. " COL6 DATE, " +
  834. " COL7 BLOB NOT NULL, " +
  835. " COL8 BLOB, " +
  836. " COL9 CLOB NOT NULL, " +
  837. " COL10 CLOB " +
  838. ")";
  839. cmd2.ExecuteNonQuery ();
  840. Console.WriteLine(" COMMIT...");
  841. cmd2.CommandText = "COMMIT";
  842. cmd2.ExecuteNonQuery ();
  843. Console.WriteLine(" create insert command...");
  844. OracleTransaction trans = connection.BeginTransaction ();
  845. OracleCommand cmd = connection.CreateCommand ();
  846. cmd.Transaction = trans;
  847. cmd.CommandText = "INSERT INTO MONO_TEST_TABLE7 " +
  848. "(COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10) " +
  849. "VALUES(:P1,:P2,:P3,:P4,:P5,:P6,:P7,:P8,:P9,:P10)";
  850. Console.WriteLine(" Add parameters...");
  851. OracleParameter parm1 = cmd.Parameters.Add (":P1", OracleType.VarChar, 8);
  852. OracleParameter parm2 = cmd.Parameters.Add (":P2", OracleType.VarChar, 32);
  853. OracleParameter parm3 = cmd.Parameters.Add (":P3", OracleType.Number);
  854. OracleParameter parm4 = cmd.Parameters.Add (":P4", OracleType.Number);
  855. OracleParameter parm5 = cmd.Parameters.Add (":P5", OracleType.DateTime);
  856. OracleParameter parm6 = cmd.Parameters.Add (":P6", OracleType.DateTime);
  857. // FIXME: fix BLOBs and CLOBs in OracleParameter
  858. OracleParameter parm7 = cmd.Parameters.Add (":P7", OracleType.Blob);
  859. OracleParameter parm8 = cmd.Parameters.Add (":P8", OracleType.Blob);
  860. OracleParameter parm9 = cmd.Parameters.Add (":P9", OracleType.Clob);
  861. OracleParameter parm10 = cmd.Parameters.Add (":P10", OracleType.Clob);
  862. // TODO: implement out, return, and ref parameters
  863. string s = "Mono";
  864. decimal d = 123456789012345.678M;
  865. DateTime dt = DateTime.Now;
  866. string clob = "Clob";
  867. byte[] blob = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
  868. Console.WriteLine(" Set Values...");
  869. parm1.Value = s;
  870. parm2.Value = DBNull.Value;
  871. parm3.Value = d;
  872. parm4.Value = DBNull.Value;
  873. parm5.Value = dt;
  874. parm6.Value = DBNull.Value;
  875. parm7.Value = blob;
  876. parm8.Value = DBNull.Value;
  877. parm9.Value = clob;
  878. parm10.Value = DBNull.Value;
  879. Console.WriteLine(" ExecuteNonQuery...");
  880. cmd.ExecuteNonQuery ();
  881. trans.Commit();
  882. }
  883. public static void CLOBTest (OracleConnection connection)
  884. {
  885. Console.WriteLine (" BEGIN TRANSACTION ...");
  886. OracleTransaction transaction = connection.BeginTransaction ();
  887. Console.WriteLine (" Drop table CLOBTEST ...");
  888. try {
  889. OracleCommand cmd2 = connection.CreateCommand ();
  890. cmd2.Transaction = transaction;
  891. cmd2.CommandText = "DROP TABLE CLOBTEST";
  892. cmd2.ExecuteNonQuery ();
  893. }
  894. catch (OracleException) {
  895. // ignore if table already exists
  896. }
  897. Console.WriteLine (" CREATE TABLE ...");
  898. OracleCommand create = connection.CreateCommand ();
  899. create.Transaction = transaction;
  900. create.CommandText = "CREATE TABLE CLOBTEST (CLOB_COLUMN CLOB)";
  901. create.ExecuteNonQuery ();
  902. Console.WriteLine (" INSERT RECORD ...");
  903. OracleCommand insert = connection.CreateCommand ();
  904. insert.Transaction = transaction;
  905. insert.CommandText = "INSERT INTO CLOBTEST VALUES (EMPTY_CLOB())";
  906. insert.ExecuteNonQuery ();
  907. OracleCommand select = connection.CreateCommand ();
  908. select.Transaction = transaction;
  909. select.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST FOR UPDATE";
  910. Console.WriteLine (" SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
  911. OracleDataReader reader = select.ExecuteReader ();
  912. if (!reader.Read ())
  913. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  914. Console.WriteLine (" TESTING OracleLob OBJECT ...");
  915. OracleLob lob = reader.GetOracleLob (0);
  916. Console.WriteLine (" LENGTH: {0}", lob.Length);
  917. Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
  918. UnicodeEncoding encoding = new UnicodeEncoding ();
  919. byte[] value = new byte [lob.Length * 2];
  920. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  921. Console.WriteLine (" UPDATING VALUE TO 'TEST ME!'");
  922. value = encoding.GetBytes ("TEST ME!");
  923. lob.Write (value, 0, value.Length);
  924. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  925. Console.WriteLine (" RE-READ VALUE...");
  926. lob.Seek (1, SeekOrigin.Begin);
  927. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  928. value = new byte [lob.Length * 2];
  929. lob.Read (value, 0, value.Length);
  930. Console.WriteLine (" VALUE: {0}", encoding.GetString (value));
  931. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  932. Console.WriteLine (" CLOSE OracleLob...");
  933. lob.Close ();
  934. Console.WriteLine (" CLOSING READER...");
  935. reader.Close ();
  936. transaction.Commit ();
  937. }
  938. public static void BLOBTest (OracleConnection connection)
  939. {
  940. Console.WriteLine (" BEGIN TRANSACTION ...");
  941. OracleTransaction transaction = connection.BeginTransaction ();
  942. Console.WriteLine (" Drop table BLOBTEST ...");
  943. try {
  944. OracleCommand cmd2 = connection.CreateCommand ();
  945. cmd2.Transaction = transaction;
  946. cmd2.CommandText = "DROP TABLE BLOBTEST";
  947. cmd2.ExecuteNonQuery ();
  948. }
  949. catch (OracleException) {
  950. // ignore if table already exists
  951. }
  952. Console.WriteLine (" CREATE TABLE ...");
  953. OracleCommand create = connection.CreateCommand ();
  954. create.Transaction = transaction;
  955. create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
  956. create.ExecuteNonQuery ();
  957. Console.WriteLine (" INSERT RECORD ...");
  958. OracleCommand insert = connection.CreateCommand ();
  959. insert.Transaction = transaction;
  960. insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
  961. insert.ExecuteNonQuery ();
  962. OracleCommand select = connection.CreateCommand ();
  963. select.Transaction = transaction;
  964. select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
  965. Console.WriteLine (" SELECTING A BLOB (Binary) VALUE FROM BLOBTEST");
  966. OracleDataReader reader = select.ExecuteReader ();
  967. if (!reader.Read ())
  968. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  969. Console.WriteLine (" TESTING OracleLob OBJECT ...");
  970. OracleLob lob = reader.GetOracleLob (0);
  971. byte[] value = null;
  972. string bvalue = "";
  973. Console.WriteLine (" UPDATING VALUE");
  974. byte[] bytes = new byte[6];
  975. bytes[0] = 0x31;
  976. bytes[1] = 0x32;
  977. bytes[2] = 0x33;
  978. bytes[3] = 0x34;
  979. bytes[4] = 0x35;
  980. bytes[5] = 0x36;
  981. lob.Write (bytes, 0, bytes.Length);
  982. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  983. Console.WriteLine (" RE-READ VALUE...");
  984. lob.Seek (1, SeekOrigin.Begin);
  985. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  986. value = new byte [lob.Length];
  987. lob.Read (value, 0, value.Length);
  988. bvalue = "";
  989. if (value.GetType ().ToString ().Equals ("System.Byte[]"))
  990. bvalue = GetHexString (value);
  991. Console.WriteLine (" Bytes: " + bvalue);
  992. Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
  993. Console.WriteLine (" CLOSE OracleLob...");
  994. lob.Close ();
  995. Console.WriteLine (" CLOSING READER...");
  996. reader.Close ();
  997. transaction.Commit ();
  998. }
  999. static void Wait(string msg)
  1000. {
  1001. Console.WriteLine(msg);
  1002. if (msg.Equals(""))
  1003. Console.WriteLine("Waiting... Press Enter to continue...");
  1004. Console.ReadLine();
  1005. }
  1006. // use this function to read a byte array into a string
  1007. // for easy display of binary data, such as, a BLOB value
  1008. public static string GetHexString (byte[] bytes)
  1009. {
  1010. string bvalue = "";
  1011. StringBuilder sb2 = new StringBuilder();
  1012. for (int z = 0; z < bytes.Length; z++) {
  1013. byte byt = bytes[z];
  1014. if (byt < 0x10)
  1015. sb2.Append ("0");
  1016. sb2.Append (byt.ToString("x"));
  1017. }
  1018. if (sb2.Length > 0)
  1019. bvalue = "0x" + sb2.ToString ();
  1020. return bvalue;
  1021. }
  1022. static void StoredProcedureTest1 (OracleConnection con)
  1023. {
  1024. // test stored procedure with no parameters
  1025. OracleCommand cmd2 = con.CreateCommand ();
  1026. Console.WriteLine(" Drop table MONO_TEST_TABLE1...");
  1027. try {
  1028. cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE1";
  1029. cmd2.ExecuteNonQuery ();
  1030. }
  1031. catch(OracleException) {
  1032. // ignore if table did not exist
  1033. }
  1034. Console.WriteLine(" Drop procedure SP_TEST1...");
  1035. try {
  1036. cmd2.CommandText = "DROP PROCEDURE SP_TEST1";
  1037. cmd2.ExecuteNonQuery ();
  1038. }
  1039. catch(OracleException) {
  1040. // ignore if procedure did not exist
  1041. }
  1042. Console.WriteLine(" Create table MONO_TEST_TABLE1...");
  1043. cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE1 (" +
  1044. " COL1 VARCHAR2(8), "+
  1045. " COL2 VARCHAR2(32))";
  1046. cmd2.ExecuteNonQuery ();
  1047. Console.WriteLine(" Create stored procedure SP_TEST1...");
  1048. cmd2.CommandText = "CREATE PROCEDURE SP_TEST1 " +
  1049. " IS " +
  1050. " BEGIN " +
  1051. " INSERT INTO MONO_TEST_TABLE1 (COL1,COL2) VALUES ('aaa','bbbb');" +
  1052. " COMMIT;" +
  1053. " END;";
  1054. cmd2.ExecuteNonQuery ();
  1055. Console.WriteLine("COMMIT...");
  1056. cmd2.CommandText = "COMMIT";
  1057. cmd2.ExecuteNonQuery ();
  1058. Console.WriteLine(" Call stored procedure sp_test1...");
  1059. OracleCommand cmd3 = con.CreateCommand ();
  1060. cmd3.CommandType = CommandType.StoredProcedure;
  1061. cmd3.CommandText = "sp_test1";
  1062. cmd3.ExecuteNonQuery ();
  1063. }
  1064. static void StoredProcedureTest2 (OracleConnection con)
  1065. {
  1066. // test stored procedure with 2 parameters
  1067. Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
  1068. OracleCommand cmd2 = con.CreateCommand ();
  1069. try {
  1070. cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE2";
  1071. cmd2.ExecuteNonQuery ();
  1072. }
  1073. catch(OracleException) {
  1074. // ignore if table already exists
  1075. }
  1076. Console.WriteLine(" Drop procedure SP_TEST2...");
  1077. try {
  1078. cmd2.CommandText = "DROP PROCEDURE SP_TEST2";
  1079. cmd2.ExecuteNonQuery ();
  1080. }
  1081. catch(OracleException) {
  1082. // ignore if procedure does not exists
  1083. }
  1084. Console.WriteLine(" Create table MONO_TEST_TABLE2...");
  1085. cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE2 (" +
  1086. " COL1 VARCHAR2(8), "+
  1087. " COL2 VARCHAR2(32))";
  1088. cmd2.ExecuteNonQuery ();
  1089. Console.WriteLine(" Create stored procedure SP_TEST2...");
  1090. cmd2.CommandText = "CREATE PROCEDURE SP_TEST2(parm1 VARCHAR2,parm2 VARCHAR2) " +
  1091. " IS " +
  1092. " BEGIN " +
  1093. " INSERT INTO MONO_TEST_TABLE2 (COL1,COL2) VALUES (parm1,parm2);" +
  1094. " COMMIT;" +
  1095. " END;";
  1096. cmd2.ExecuteNonQuery ();
  1097. Console.WriteLine(" COMMIT...");
  1098. cmd2.CommandText = "COMMIT";
  1099. cmd2.ExecuteNonQuery ();
  1100. Console.WriteLine(" Call stored procedure SP_TEST2 with two parameters...");
  1101. OracleCommand cmd3 = con.CreateCommand ();
  1102. cmd3.CommandType = CommandType.StoredProcedure;
  1103. cmd3.CommandText = "sp_test2";
  1104. OracleParameter myParameter1 = new OracleParameter("parm1", OracleType.VarChar);
  1105. myParameter1.Value = "yyy13";
  1106. myParameter1.Size = 8;
  1107. myParameter1.Direction = ParameterDirection.Input;
  1108. OracleParameter myParameter2 = new OracleParameter("parm2", OracleType.VarChar);
  1109. myParameter2.Value = "iii13";
  1110. myParameter2.Size = 32;
  1111. myParameter2.Direction = ParameterDirection.Input;
  1112. cmd3.Parameters.Add (myParameter1);
  1113. cmd3.Parameters.Add (myParameter2);
  1114. cmd3.ExecuteNonQuery ();
  1115. }
  1116. static void OutParmTest1(OracleConnection con)
  1117. {
  1118. // test stored fuctions with 4 parameters
  1119. // 1. input varchar2
  1120. // 2. output varchar2
  1121. // 3. input output varchar2
  1122. // 4. return varchar2
  1123. Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST1 for testing VARCHAR2 Input, Output, InputOutput, Return parameters...");
  1124. OracleCommand cmd2 = con.CreateCommand();
  1125. cmd2.CommandText =
  1126. "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM1(parm1 IN VARCHAR2, parm2 OUT VARCHAR2, parm3 IN OUT VARCHAR2) RETURN VARCHAR2 " +
  1127. "IS " +
  1128. " returnValue VARCHAR2(32) := 'Anywhere';" +
  1129. "BEGIN " +
  1130. " IF parm1 IS NULL THEN " +
  1131. " parm2 := 'parm1 is null'; " +
  1132. " ELSE " +
  1133. " parm2 := 'One' || parm1 || 'Three'; " +
  1134. " END IF; " +
  1135. " IF parm3 IS NOT NULL THEN " +
  1136. " parm3 := parm2 || parm3 || 'Five'; " +
  1137. " ELSE " +
  1138. " parm3 := 'parm3 in was NULL'; " +
  1139. " END IF; " +
  1140. " IF parm1 IS NOT NULL THEN " +
  1141. " IF parm1 = '999' THEN " +
  1142. " parm2 := NULL; " +
  1143. " parm3 := NULL; " +
  1144. " returnValue := NULL; " +
  1145. " END IF; " +
  1146. " END IF; " +
  1147. " RETURN returnValue; " +
  1148. "END;";
  1149. cmd2.ExecuteNonQuery();
  1150. Console.WriteLine(" COMMIT...");
  1151. cmd2.CommandText = "COMMIT";
  1152. cmd2.ExecuteNonQuery();
  1153. Console.WriteLine(" Call stored function SF_TESTOUTPARM1 with 4 parameters...");
  1154. OracleCommand cmd3 = con.CreateCommand();
  1155. cmd3.CommandType = CommandType.Text;
  1156. cmd3.CommandText =
  1157. "BEGIN " +
  1158. " :ReturnValue := SF_TESTOUTPARM1(:p1, :p2, :p3); " +
  1159. "END;";
  1160. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.VarChar);
  1161. myParameter1.Value = "Two";
  1162. myParameter1.Size = 32;
  1163. myParameter1.Direction = ParameterDirection.Input;
  1164. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.VarChar);
  1165. myParameter2.Size = 32;
  1166. myParameter2.Direction = ParameterDirection.Output;
  1167. OracleParameter myParameter3 = new OracleParameter("p3", OracleType.VarChar);
  1168. myParameter3.Value = "Four";
  1169. myParameter3.Size = 32;
  1170. myParameter3.Direction = ParameterDirection.InputOutput;
  1171. OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.VarChar);
  1172. myParameter4.Size = 32;
  1173. myParameter4.Direction = ParameterDirection.ReturnValue;
  1174. cmd3.Parameters.Add(myParameter1);
  1175. cmd3.Parameters.Add(myParameter2);
  1176. cmd3.Parameters.Add(myParameter3);
  1177. cmd3.Parameters.Add(myParameter4);
  1178. cmd3.ExecuteNonQuery();
  1179. string outValue = (string)myParameter2.Value;
  1180. string inOutValue = (string)myParameter3.Value;
  1181. string returnValue = (string)myParameter4.Value;
  1182. Console.WriteLine(" 1Out Value should be: OneTwoThree");
  1183. Console.WriteLine(" 1Out Value: " + outValue);
  1184. Console.WriteLine(" 1InOut Value should be: OneTwoThreeFourFive");
  1185. Console.WriteLine(" 1InOut Value: " + inOutValue);
  1186. Console.WriteLine(" 1Return Value should be: Anywhere");
  1187. Console.WriteLine(" 1Return Value: " + returnValue);
  1188. Console.WriteLine();
  1189. myParameter1.Value = DBNull.Value;
  1190. myParameter3.Value = "Hello";
  1191. cmd3.ExecuteNonQuery();
  1192. outValue = (string)myParameter2.Value;
  1193. inOutValue = (string)myParameter3.Value;
  1194. returnValue = (string)myParameter4.Value;
  1195. Console.WriteLine(" 2Out Value should be: parm1 is null");
  1196. Console.WriteLine(" 2Out Value: " + outValue);
  1197. Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
  1198. Console.WriteLine(" 2InOut Value: " + inOutValue);
  1199. Console.WriteLine(" 2Return Value should be: Anywhere");
  1200. Console.WriteLine(" 2Return Value: " + returnValue);
  1201. Console.WriteLine();
  1202. myParameter1.Value = "999";
  1203. myParameter3.Value = "Bye";
  1204. cmd3.ExecuteNonQuery();
  1205. if (myParameter2.Value == DBNull.Value)
  1206. outValue = "Value is DBNull.Value";
  1207. else
  1208. outValue = (string)myParameter2.Value;
  1209. if( myParameter3.Value == DBNull.Value)
  1210. inOutValue = "Value is DBNull.Value";
  1211. else
  1212. inOutValue = (string)myParameter3.Value;
  1213. if (myParameter4.Value == DBNull.Value)
  1214. returnValue = "Value is DBNull.Value";
  1215. else
  1216. returnValue = (string)myParameter4.Value;
  1217. Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
  1218. Console.WriteLine(" 3Out Value: " + outValue);
  1219. Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
  1220. Console.WriteLine(" 3InOut Value: " + inOutValue);
  1221. Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
  1222. Console.WriteLine(" 3Return Value: " + returnValue);
  1223. Console.WriteLine();
  1224. myParameter1.Value = "***";
  1225. myParameter3.Value = DBNull.Value;
  1226. cmd3.ExecuteNonQuery();
  1227. outValue = (string)myParameter2.Value;
  1228. inOutValue = (string)myParameter3.Value;
  1229. returnValue = (string)myParameter4.Value;
  1230. Console.WriteLine(" 4Out Value should be: One***Three");
  1231. Console.WriteLine(" 4Out Value: " + outValue);
  1232. Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
  1233. Console.WriteLine(" 4InOut Value: " + inOutValue);
  1234. Console.WriteLine(" 4Return Value should be: Anywhere");
  1235. Console.WriteLine(" 4Return Value: " + returnValue);
  1236. Console.WriteLine();
  1237. }
  1238. static void OutParmTest2 (OracleConnection con)
  1239. {
  1240. // test stored function with 4 parameters
  1241. // 1. input number(18,2)
  1242. // 2. output number(18,2)
  1243. // 3. input output number (18,2)
  1244. // 4. return number (18,2)
  1245. Console.WriteLine(" Create stored function SF_TESTOUTPARM2 to test NUMBER parameters...");
  1246. // stored procedure addes two numbers
  1247. OracleCommand cmd2 = con.CreateCommand();
  1248. cmd2.CommandText =
  1249. "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM2(parm1 IN NUMBER, parm2 OUT NUMBER, parm3 IN OUT NUMBER) RETURN NUMBER " +
  1250. "IS " +
  1251. " returnValue NUMBER := 123.45; " +
  1252. "BEGIN " +
  1253. " IF parm1 IS NULL THEN " +
  1254. " parm2 := 18; " +
  1255. " parm3 := parm3 + 8000; " +
  1256. " returnValue := 78; " +
  1257. " ELSIF parm1 = 999 THEN " +
  1258. " parm2 := NULL;" +
  1259. " parm3 := NULL;" +
  1260. " returnValue := NULL;" +
  1261. " ELSIF parm3 IS NULL THEN " +
  1262. " parm2 := 0; " +
  1263. " parm3 := 1234567890123.12345678; " +
  1264. " ELSE " +
  1265. " parm2 := parm1 + 3; " +
  1266. " parm3 := parm3 + 70; " +
  1267. " END IF;" +
  1268. " RETURN returnValue;" +
  1269. "END;";
  1270. cmd2.ExecuteNonQuery();
  1271. Console.WriteLine(" COMMIT...");
  1272. cmd2.CommandText = "COMMIT";
  1273. cmd2.ExecuteNonQuery();
  1274. Console.WriteLine(" Call stored function SP_TESTOUTPARM2 with 4 parameters...");
  1275. OracleCommand cmd3 = con.CreateCommand();
  1276. cmd3.CommandType = CommandType.Text;
  1277. cmd3.CommandText =
  1278. "BEGIN " +
  1279. " :returnValue := SF_TESTOUTPARM2(:p1, :p2, :p3);" +
  1280. "END;";
  1281. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Number);
  1282. myParameter1.Value = 2.2;
  1283. myParameter1.Direction = ParameterDirection.Input;
  1284. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Number);
  1285. myParameter2.Direction = ParameterDirection.Output;
  1286. OracleParameter myParameter3 = new OracleParameter("p3", OracleType.Number);
  1287. myParameter3.Value = 33.4;
  1288. myParameter3.Direction = ParameterDirection.InputOutput;
  1289. OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.Number);
  1290. myParameter4.Direction = ParameterDirection.ReturnValue;
  1291. cmd3.Parameters.Add(myParameter1);
  1292. cmd3.Parameters.Add(myParameter2);
  1293. cmd3.Parameters.Add(myParameter3);
  1294. cmd3.Parameters.Add(myParameter4);
  1295. cmd3.ExecuteNonQuery();
  1296. decimal outValue = (decimal)myParameter2.Value;
  1297. decimal inOutValue = (decimal)myParameter3.Value;
  1298. decimal returnValue = (decimal)myParameter4.Value;
  1299. Console.WriteLine(" 1Out Value should be: 5.20");
  1300. Console.WriteLine(" 1Out Value: {0}", outValue);
  1301. Console.WriteLine(" 1InOut Value should be: 103.40");
  1302. Console.WriteLine(" 1InOut Value: {0}", inOutValue);
  1303. Console.WriteLine(" 1Return Value should be: 123.45");
  1304. Console.WriteLine(" 1Return Value: {0}", returnValue);
  1305. Console.WriteLine();
  1306. myParameter1.Value = DBNull.Value;
  1307. myParameter3.Value = 23;
  1308. cmd3.ExecuteNonQuery();
  1309. outValue = (decimal)myParameter2.Value;
  1310. inOutValue = (decimal)myParameter3.Value;
  1311. returnValue = (decimal)myParameter4.Value;
  1312. Console.WriteLine(" 2Out Value should be: 18");
  1313. Console.WriteLine(" 2Out Value: {0}", outValue);
  1314. Console.WriteLine(" 2InOut Value should be: 8023");
  1315. Console.WriteLine(" 2InOut Value: {0}", inOutValue);
  1316. Console.WriteLine(" 2Return Value should be: 78");
  1317. Console.WriteLine(" 2Return Value: {0}", returnValue);
  1318. Console.WriteLine();
  1319. string soutValue = "";
  1320. string sinOutValue = "";
  1321. string sreturnValue = "";
  1322. myParameter1.Value = 999;
  1323. myParameter3.Value = 66;
  1324. cmd3.ExecuteNonQuery();
  1325. if (myParameter2.Value == DBNull.Value)
  1326. soutValue = "DBNull.Value";
  1327. else
  1328. soutValue = myParameter2.Value.ToString();
  1329. if (myParameter3.Value == DBNull.Value)
  1330. sinOutValue = "DBNull.Value";
  1331. else
  1332. sinOutValue = myParameter3.Value.ToString();
  1333. if (myParameter4.Value == DBNull.Value)
  1334. sreturnValue = "DBNull.Value";
  1335. else
  1336. sreturnValue = myParameter4.Value.ToString();
  1337. Console.WriteLine(" 3Out Value should be: DBNull.Value");
  1338. Console.WriteLine(" 3Out Value: {0}", soutValue);
  1339. Console.WriteLine(" 3InOut Value should be: DBNull.Value");
  1340. Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
  1341. Console.WriteLine(" 3Return Value should be: DBNull.Value");
  1342. Console.WriteLine(" 3Return Value: {0}", sreturnValue);
  1343. Console.WriteLine();
  1344. myParameter1.Value = 111;
  1345. myParameter3.Value = DBNull.Value;
  1346. cmd3.ExecuteNonQuery();
  1347. outValue = (decimal)myParameter2.Value;
  1348. inOutValue = (decimal)myParameter3.Value;
  1349. returnValue = (decimal)myParameter4.Value;
  1350. Console.WriteLine(" 4Out Value should be: 0 (as in digit zero)");
  1351. Console.WriteLine(" 4Out Value: {0}", outValue);
  1352. Console.WriteLine(" 4InOut Value should be: 1234567890123.12345678");
  1353. Console.WriteLine(" 4InOut Value: {0}", inOutValue);
  1354. Console.WriteLine(" 4Return Value should be: 123.45");
  1355. Console.WriteLine(" 4Return Value: {0}", returnValue);
  1356. Console.WriteLine();
  1357. }
  1358. static void OutParmTest3 (OracleConnection con)
  1359. {
  1360. // test stored function with 4 parameters
  1361. // 1. input date
  1362. // 2. output date
  1363. // 3. input output date
  1364. // 4. return dae
  1365. // a DATE type in Oracle has Date and Time
  1366. Console.WriteLine(" Create stored function SF_TESTOUTPARM3 to test Date parameters...");
  1367. OracleCommand cmd2 = con.CreateCommand();
  1368. cmd2.CommandText =
  1369. "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM3(parm1 IN DATE, parm2 OUT DATE, parm3 IN OUT DATE) RETURN DATE " +
  1370. "IS " +
  1371. " returnValue DATE := TO_DATE('2001-07-01 15:32:52', 'YYYY-MM-DD HH24:MI:SS');" +
  1372. "BEGIN " +
  1373. " IF parm1 IS NULL THEN " +
  1374. " parm2 := TO_DATE('1900-12-31', 'YYYY-MM-DD'); " +
  1375. " parm3 := TO_DATE('1900-12-31', 'YYYY-MM-DD'); " +
  1376. " ELSIF parm1 = TO_DATE('1979-11-25','YYYY-MM-DD') THEN " +
  1377. " parm2 := NULL;" +
  1378. " parm3 := NULL;" +
  1379. " returnValue := NULL;"+
  1380. " ELSIF parm3 IS NULL THEN " +
  1381. " parm2 := TO_DATE('2008-08-08', 'YYYY-MM-DD');" +
  1382. " parm3 := TO_DATE('2000-01-01', 'YYYY-MM-DD');" +
  1383. " ELSE " +
  1384. " -- add 3 days to date\n " +
  1385. " parm2 := parm1 + 3; " +
  1386. " parm3 := parm3 + 5; " +
  1387. " END IF; " +
  1388. " RETURN returnValue;" +
  1389. "END;";
  1390. cmd2.ExecuteNonQuery();
  1391. Console.WriteLine(" COMMIT...");
  1392. cmd2.CommandText = "COMMIT";
  1393. cmd2.ExecuteNonQuery();
  1394. Console.WriteLine(" Call stored function SF_TESTOUTPARM3 with 4 parameters...");
  1395. OracleCommand cmd3 = con.CreateCommand();
  1396. cmd3.CommandType = CommandType.Text;
  1397. cmd3.CommandText =
  1398. "BEGIN " +
  1399. " :returnValue := SF_TESTOUTPARM3(:p1, :p2, :p3);" +
  1400. "END;";
  1401. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.DateTime);
  1402. myParameter1.Value = new DateTime(2004, 12, 15);
  1403. myParameter1.Direction = ParameterDirection.Input;
  1404. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.DateTime);
  1405. myParameter2.Direction = ParameterDirection.Output;
  1406. OracleParameter myParameter3 = new OracleParameter("p3", OracleType.DateTime);
  1407. myParameter3.Value = new DateTime(2008, 10, 14, 20, 21, 22);
  1408. myParameter3.Direction = ParameterDirection.InputOutput;
  1409. OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.DateTime);
  1410. myParameter4.Direction = ParameterDirection.ReturnValue;
  1411. cmd3.Parameters.Add(myParameter1);
  1412. cmd3.Parameters.Add(myParameter2);
  1413. cmd3.Parameters.Add(myParameter3);
  1414. cmd3.Parameters.Add(myParameter4);
  1415. cmd3.ExecuteNonQuery();
  1416. DateTime outValue = (DateTime)myParameter2.Value;
  1417. DateTime inOutValue = (DateTime)myParameter3.Value;
  1418. DateTime returnValue = (DateTime)myParameter4.Value;
  1419. Console.WriteLine(" 1Out Value should be: 2004-12-18 00:00:00");
  1420. Console.WriteLine(" 1Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1421. Console.WriteLine(" 1InOut Value should be: 2008-10-19 20:21:22");
  1422. Console.WriteLine(" 1InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1423. Console.WriteLine(" 1Return Value should be: 2001-07-01 15:32:52");
  1424. Console.WriteLine(" 1Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1425. Console.WriteLine();
  1426. myParameter1.Value = DBNull.Value;
  1427. myParameter3.Value = new DateTime(1980, 11, 22);
  1428. cmd3.ExecuteNonQuery();
  1429. outValue = (DateTime)myParameter2.Value;
  1430. inOutValue = (DateTime)myParameter3.Value;
  1431. returnValue = (DateTime)myParameter4.Value;
  1432. Console.WriteLine(" 2Out Value should be: 1900-12-31 00:00:00");
  1433. Console.WriteLine(" 2Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1434. Console.WriteLine(" 2InOut Value should be: 1900-12-31 00:00:00");
  1435. Console.WriteLine(" 2InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1436. Console.WriteLine(" 2Return Value should be: 2001-07-01 15:32:52");
  1437. Console.WriteLine(" 2Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1438. Console.WriteLine();
  1439. myParameter1.Value = new DateTime(1979, 11, 25);
  1440. myParameter3.Value = new DateTime(1981, 12, 14);
  1441. cmd3.ExecuteNonQuery();
  1442. string soutValue = "";
  1443. string sinOutValue = "";
  1444. string sreturnValue = "";
  1445. if (myParameter2.Value == DBNull.Value)
  1446. soutValue = "DBNull.Value";
  1447. else {
  1448. outValue = (DateTime)myParameter2.Value;
  1449. soutValue = outValue.ToString("yyyy-MM-dd HH:mm:ss");
  1450. }
  1451. if (myParameter3.Value == DBNull.Value)
  1452. sinOutValue = "DBNull.Value";
  1453. else {
  1454. inOutValue = (DateTime)myParameter3.Value;
  1455. sinOutValue = inOutValue.ToString("yyyy-MM-dd HH:mm:ss");
  1456. }
  1457. if (myParameter4.Value == DBNull.Value)
  1458. sreturnValue = "DBNull.Value";
  1459. else {
  1460. returnValue = (DateTime)myParameter4.Value;
  1461. sreturnValue = returnValue.ToString("yyyy-MM-dd HH:mm:ss");
  1462. }
  1463. Console.WriteLine(" 3Out Value should be: DBNull.Value");
  1464. Console.WriteLine(" 3Out Value: {0}", soutValue);
  1465. Console.WriteLine(" 3InOut Value should be: DBNull.Value");
  1466. Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
  1467. Console.WriteLine(" 3Return Value should be: DBNull.Value");
  1468. Console.WriteLine(" 3Return Value: {0}", sreturnValue);
  1469. Console.WriteLine();
  1470. myParameter1.Value = new DateTime(1976, 7, 4);
  1471. myParameter3.Value = DBNull.Value;
  1472. cmd3.ExecuteNonQuery();
  1473. outValue = (DateTime)myParameter2.Value;
  1474. inOutValue = (DateTime)myParameter3.Value;
  1475. returnValue = (DateTime)myParameter4.Value;
  1476. Console.WriteLine(" 4Out Value should be: 2008-08-08 00:00:00");
  1477. Console.WriteLine(" 4Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1478. Console.WriteLine(" 4InOut Value should be: 2000-01-01 00:00:00");
  1479. Console.WriteLine(" 4InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1480. Console.WriteLine(" 4Return Value should be: 2001-07-01 15:32:52");
  1481. Console.WriteLine(" 4Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1482. Console.WriteLine();
  1483. }
  1484. static void OutParmTest4(OracleConnection con)
  1485. {
  1486. // test stored fuctions with 4 parameters
  1487. // 1. input long
  1488. // 2. output long
  1489. // 3. input output long
  1490. // 4. return long
  1491. Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST4 for testing LONG VARCHAR Input, Output, InputOutput, Return parameters...");
  1492. OracleCommand cmd2 = con.CreateCommand();
  1493. cmd2.CommandText =
  1494. "CREATE OR REPLACE FUNCTION SP_OUTPUTPARMTEST4(parm1 IN LONG, parm2 OUT LONG, parm3 IN OUT LONG) RETURN LONG " +
  1495. "IS " +
  1496. " returnValue LONG := 'A very, very, very long value in a far away memory space.'; " +
  1497. "BEGIN " +
  1498. " IF parm1 IS NULL THEN " +
  1499. " parm2 := 'parm1 is null'; " +
  1500. " returnValue := 'Another one bytes the dust.'; " +
  1501. " ELSE " +
  1502. " parm2 := 'One' || parm1 || 'Three'; " +
  1503. " END IF; " +
  1504. " IF parm3 IS NOT NULL THEN " +
  1505. " parm3 := parm2 || parm3 || 'Five'; " +
  1506. " ELSE " +
  1507. " parm3 := 'parm3 in was NULL'; " +
  1508. " END IF; " +
  1509. " IF parm1 IS NOT NULL THEN " +
  1510. " IF parm1 = '999' THEN " +
  1511. " parm2 := NULL; " +
  1512. " parm3 := NULL; " +
  1513. " returnValue := NULL; " +
  1514. " END IF; " +
  1515. " END IF; " +
  1516. " RETURN returnValue; " +
  1517. "END;";
  1518. cmd2.ExecuteNonQuery();
  1519. Console.WriteLine(" COMMIT...");
  1520. cmd2.CommandText = "COMMIT";
  1521. cmd2.ExecuteNonQuery();
  1522. Console.WriteLine(" Call stored procedure SP_OUTPUTPARMTEST4 with 4 parameters...");
  1523. OracleCommand cmd3 = con.CreateCommand();
  1524. cmd3.CommandType = CommandType.Text;
  1525. cmd3.CommandText =
  1526. "BEGIN " +
  1527. " :ReturnValue := SP_OUTPUTPARMTEST4(:p1, :p2, :p3); " +
  1528. "END;";
  1529. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.LongVarChar);
  1530. myParameter1.Size = 1000;
  1531. myParameter1.Direction = ParameterDirection.Input;
  1532. myParameter1.Value = "Two";
  1533. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.LongVarChar);
  1534. myParameter2.Size = 1000;
  1535. myParameter2.Direction = ParameterDirection.Output;
  1536. OracleParameter myParameter3 = new OracleParameter("p3", OracleType.LongVarChar);
  1537. myParameter3.Value = "Four";
  1538. myParameter3.Size = 1000;
  1539. myParameter3.Direction = ParameterDirection.InputOutput;
  1540. OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.LongVarChar);
  1541. myParameter4.Size = 1000;
  1542. myParameter4.Direction = ParameterDirection.ReturnValue;
  1543. cmd3.Parameters.Add(myParameter1);
  1544. cmd3.Parameters.Add(myParameter2);
  1545. cmd3.Parameters.Add(myParameter3);
  1546. cmd3.Parameters.Add(myParameter4);
  1547. cmd3.ExecuteNonQuery();
  1548. string outValue = (string)myParameter2.Value;
  1549. string inOutValue = (string)myParameter3.Value;
  1550. string returnValue = (string)myParameter4.Value;
  1551. Console.WriteLine(" 1Out Value should be: OneTwoThree");
  1552. Console.WriteLine(" 1Out Value: " + outValue);
  1553. Console.WriteLine(" 1InOut Value should be: OneTwoThreeFourFive");
  1554. Console.WriteLine(" 1InOut Value: " + inOutValue);
  1555. Console.WriteLine(" 1Return Value should be: A very, very, very long value in a far away memory space.");
  1556. Console.WriteLine(" 1Return Value: " + returnValue);
  1557. Console.WriteLine();
  1558. myParameter1.Value = DBNull.Value;
  1559. myParameter3.Value = "Hello";
  1560. cmd3.ExecuteNonQuery();
  1561. outValue = (string)myParameter2.Value;
  1562. inOutValue = (string)myParameter3.Value;
  1563. returnValue = (string)myParameter4.Value;
  1564. Console.WriteLine(" 2Out Value should be: parm1 is null");
  1565. Console.WriteLine(" 2Out Value: " + outValue);
  1566. Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
  1567. Console.WriteLine(" 2InOut Value: " + inOutValue);
  1568. Console.WriteLine(" 2Return Value should be: Another one bytes the dust.");
  1569. Console.WriteLine(" 2Return Value: " + returnValue);
  1570. Console.WriteLine();
  1571. myParameter1.Value = "999";
  1572. myParameter3.Value = "Bye";
  1573. cmd3.ExecuteNonQuery();
  1574. if (myParameter2.Value == DBNull.Value)
  1575. outValue = "Value is DBNull.Value";
  1576. else
  1577. outValue = (string)myParameter2.Value;
  1578. if (myParameter3.Value == DBNull.Value)
  1579. inOutValue = "Value is DBNullValue";
  1580. else
  1581. inOutValue = (string)myParameter3.Value;
  1582. if (myParameter4.Value == DBNull.Value)
  1583. returnValue = "Value is DBNull.Value";
  1584. else
  1585. returnValue = (string)myParameter4.Value;
  1586. Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
  1587. Console.WriteLine(" 3Out Value: " + outValue);
  1588. Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
  1589. Console.WriteLine(" 3InOut Value: " + inOutValue);
  1590. Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
  1591. Console.WriteLine(" 3Return Value: " + returnValue);
  1592. Console.WriteLine();
  1593. myParameter1.Value = "***";
  1594. myParameter3.Value = DBNull.Value;
  1595. cmd3.ExecuteNonQuery();
  1596. outValue = (string)myParameter2.Value;
  1597. inOutValue = (string)myParameter3.Value;
  1598. returnValue = (string)myParameter4.Value;
  1599. Console.WriteLine(" 4Out Value should be: One***Three");
  1600. Console.WriteLine(" 4Out Value: " + outValue);
  1601. Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
  1602. Console.WriteLine(" 4InOut Value: " + inOutValue);
  1603. Console.WriteLine(" 4Return Value should be: A very, very, very long value in a far away memory space.");
  1604. Console.WriteLine(" 4Return Value: " + returnValue);
  1605. Console.WriteLine();
  1606. }
  1607. static void OutParmTest5(OracleConnection con)
  1608. {
  1609. // test stored fuctions with 4 parameters
  1610. // 1. input CLOB
  1611. // 2. output CLOB
  1612. // 3. input output CLOB
  1613. // 4. return CLOB
  1614. Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST5 for testing CLOB Input, Output, InputOutput, Return parameters...");
  1615. OracleCommand cmd2 = con.CreateCommand();
  1616. cmd2.CommandText =
  1617. "CREATE OR REPLACE FUNCTION SP_OUTPUTPARMTEST5(parm1 IN CLOB, parm2 OUT CLOB, parm3 IN OUT CLOB) RETURN CLOB " +
  1618. " IS " +
  1619. " returnValue CLOB := 'Clobber'; " +
  1620. " BEGIN " +
  1621. " IF parm1 IS NULL THEN " +
  1622. " parm2 := 'parm1 is null'; " +
  1623. " ELSE " +
  1624. " parm2 := 'One' || parm1 || 'Three'; " +
  1625. " END IF; " +
  1626. " IF parm3 IS NOT NULL THEN " +
  1627. " parm3 := parm2 || parm3 || 'Five'; " +
  1628. " ELSE " +
  1629. " parm3 := 'parm3 in was NULL'; " +
  1630. " END IF; " +
  1631. " IF parm1 IS NOT NULL THEN " +
  1632. " IF parm1 = '999' THEN " +
  1633. " parm2 := NULL; " +
  1634. " parm3 := NULL; " +
  1635. " returnValue := NULL; " +
  1636. " ELSIF LENGTH(parm1) = 0 THEN " +
  1637. " parm2 := 'parm1 is zero length'; " +
  1638. " IF LENGTH(parm3) = 0 THEN " +
  1639. " parm3 := 'parm3 is zero length';" +
  1640. " ELSE " +
  1641. " parm3 := 'Uh oh, parm3 is not zero length like we thought'; " +
  1642. " END IF; " +
  1643. " returnValue := 'parm1 is zero length'; " +
  1644. " ELSIF parm1 = '888' THEN " +
  1645. " parm2 := EMPTY_CLOB(); " +
  1646. " parm3 := EMPTY_CLOB(); " +
  1647. " returnValue := EMPTY_CLOB(); " +
  1648. " END IF; " +
  1649. " END IF; " +
  1650. " RETURN returnValue; " +
  1651. "END;";
  1652. cmd2.ExecuteNonQuery();
  1653. Console.WriteLine(" COMMIT...");
  1654. cmd2.CommandText = "COMMIT";
  1655. cmd2.ExecuteNonQuery();
  1656. Console.WriteLine(" Call stored procedure SP_OUTPUTPARMTEST5 with 4 parameters...");
  1657. //OracleTransaction trans = con.BeginTransaction();
  1658. //OracleCommand cmd4 = con.CreateCommand();
  1659. //cmd4.Transaction = trans;
  1660. //OracleLob lob = CreateTemporaryLobLocator(cmd4, OracleType.Clob);
  1661. OracleCommand cmd3 = con.CreateCommand();
  1662. //cmd3.Transaction = trans;
  1663. cmd3.CommandType = CommandType.Text;
  1664. cmd3.CommandText =
  1665. "DECLARE " +
  1666. " tempP3 CLOB; " +
  1667. "BEGIN " +
  1668. " tempP3 := :inp3; " +
  1669. " :ReturnValue := SP_OUTPUTPARMTEST5(:p1, :p2, tempP3); " +
  1670. " :outp3 := tempP3;" +
  1671. "END;";
  1672. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Clob);
  1673. myParameter1.Size = 1000;
  1674. myParameter1.Direction = ParameterDirection.Input;
  1675. myParameter1.Value = "Two";
  1676. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Clob);
  1677. myParameter2.Size = 1000;
  1678. myParameter2.Direction = ParameterDirection.Output;
  1679. // impossible to use one OracleParameter for an CLOB IN OUT parameter?
  1680. // I had to create two parameters for the 3rd parameter: in3 as input and out3 as output
  1681. // and in the anonymous PL/SQL block, get and set the 3rd parameter appropriately
  1682. OracleParameter myParameterIn3 = new OracleParameter("inp3", OracleType.Clob);
  1683. myParameterIn3.Size = 1000;
  1684. myParameterIn3.Direction = ParameterDirection.Input;
  1685. string s = "Everything";
  1686. myParameterIn3.Value = s;
  1687. OracleParameter myParameterOut3 = new OracleParameter("outp3", OracleType.Clob);
  1688. myParameterOut3.Size = 1000;
  1689. myParameterOut3.Direction = ParameterDirection.Output;
  1690. OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.Clob);
  1691. myParameter4.Size = 1000;
  1692. myParameter4.Direction = ParameterDirection.ReturnValue;
  1693. cmd3.Parameters.Add(myParameter1);
  1694. cmd3.Parameters.Add(myParameter2);
  1695. cmd3.Parameters.Add(myParameterIn3);
  1696. cmd3.Parameters.Add(myParameterOut3);
  1697. cmd3.Parameters.Add(myParameter4);
  1698. cmd3.ExecuteNonQuery();
  1699. string outValue = GetOracleClobValue(myParameter2);
  1700. string inOutValue = GetOracleClobValue(myParameterOut3);
  1701. string returnValue = GetOracleClobValue(myParameter4);
  1702. Console.WriteLine(" 1Out Value should be: OneTwoThree");
  1703. Console.WriteLine(" 1Out Value: " + outValue);
  1704. Console.WriteLine(" 1InOut Value should be: OneTwoThreeEverythingFive");
  1705. Console.WriteLine(" 1InOut Value: " + inOutValue);
  1706. Console.WriteLine(" 1Return Value should be: Clobber");
  1707. Console.WriteLine(" 1Return Value: " + returnValue);
  1708. Console.WriteLine();
  1709. myParameter1.Value = DBNull.Value;
  1710. myParameterIn3.Value = "Hello";
  1711. cmd3.ExecuteNonQuery();
  1712. outValue = GetOracleClobValue(myParameter2);
  1713. inOutValue = GetOracleClobValue(myParameterOut3);
  1714. returnValue = GetOracleClobValue(myParameter4);
  1715. Console.WriteLine(" 2Out Value should be: parm1 is null");
  1716. Console.WriteLine(" 2Out Value: " + outValue);
  1717. Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
  1718. Console.WriteLine(" 2InOut Value: " + inOutValue);
  1719. Console.WriteLine(" 2Return Value should be: Clobber");
  1720. Console.WriteLine(" 2Return Value: " + returnValue);
  1721. Console.WriteLine();
  1722. myParameter1.Value = "999";
  1723. myParameterIn3.Value = "Bye";
  1724. cmd3.ExecuteNonQuery();
  1725. outValue = GetOracleClobValue(myParameter2);
  1726. inOutValue = GetOracleClobValue(myParameterOut3);
  1727. returnValue = GetOracleClobValue(myParameter4);
  1728. Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
  1729. Console.WriteLine(" 3Out Value: " + outValue);
  1730. Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
  1731. Console.WriteLine(" 3InOut Value: " + inOutValue);
  1732. Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
  1733. Console.WriteLine(" 3Return Value: " + returnValue);
  1734. Console.WriteLine();
  1735. myParameter1.Value = "***";
  1736. myParameterIn3.Value = DBNull.Value;
  1737. cmd3.ExecuteNonQuery();
  1738. outValue = GetOracleClobValue(myParameter2);
  1739. inOutValue = GetOracleClobValue(myParameterOut3);
  1740. returnValue = GetOracleClobValue(myParameter4);
  1741. Console.WriteLine(" 4Out Value should be: One***Three");
  1742. Console.WriteLine(" 4Out Value: " + outValue);
  1743. Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
  1744. Console.WriteLine(" 4InOut Value: " + inOutValue);
  1745. Console.WriteLine(" 4Return Value should be: Clobber");
  1746. Console.WriteLine(" 4Return Value: " + returnValue);
  1747. Console.WriteLine();
  1748. myParameter1.Value = OracleLob.Null;
  1749. myParameterIn3.Value = "bass";
  1750. cmd3.ExecuteNonQuery();
  1751. outValue = GetOracleClobValue(myParameter2);
  1752. inOutValue = GetOracleClobValue(myParameterOut3);
  1753. returnValue = GetOracleClobValue(myParameter4);
  1754. Console.WriteLine(" 5Out Value should be: parm1 is null");
  1755. Console.WriteLine(" 5Out Value: " + outValue);
  1756. Console.WriteLine(" 5InOut Value should be: parm1 is nullbassFive");
  1757. Console.WriteLine(" 5InOut Value: " + inOutValue);
  1758. Console.WriteLine(" 5Return Value should be: Clobber");
  1759. Console.WriteLine(" 5Return Value: " + returnValue);
  1760. Console.WriteLine();
  1761. myParameter1.Value = "888";
  1762. myParameterIn3.Value = "777";
  1763. cmd3.ExecuteNonQuery();
  1764. outValue = GetOracleClobValue(myParameter2);
  1765. inOutValue = GetOracleClobValue(myParameterOut3);
  1766. returnValue = GetOracleClobValue(myParameter4);
  1767. Console.WriteLine(" 6Out Value should be: Zero Length");
  1768. Console.WriteLine(" 6Out Value: " + outValue);
  1769. Console.WriteLine(" 6InOut Value should be: Zero Length");
  1770. Console.WriteLine(" 6InOut Value: " + inOutValue);
  1771. Console.WriteLine(" 6Return Value should be: Zero Length");
  1772. Console.WriteLine(" 6Return Value: " + returnValue);
  1773. Console.WriteLine();
  1774. }
  1775. public static string GetOracleClobValue (OracleParameter parm)
  1776. {
  1777. if (parm.Value.Equals (DBNull.Value))
  1778. return "Clob is DBNull.Value";
  1779. OracleLob lob = (OracleLob) parm.Value;
  1780. if (lob.Length == 0)
  1781. return "Zero Length";
  1782. return lob.Value.ToString ();
  1783. }
  1784. public static OracleLob CreateTemporaryLobLocator (OracleCommand cmd, OracleType lobType)
  1785. {
  1786. cmd.CommandText =
  1787. "DECLARE TEMP_LOB " + lobType.ToString () + "; " +
  1788. " BEGIN " +
  1789. " SYS.DBMS_LOB.CREATETEMPORARY (TEMP_LOB, FALSE); " +
  1790. " :TempLobLocator := TEMP_LOB; " +
  1791. " END;";
  1792. OracleParameter parm = cmd.Parameters.Add ("TempLobLocator", lobType);
  1793. parm.Direction = ParameterDirection.Output;
  1794. cmd.ExecuteNonQuery ();
  1795. return (OracleLob)parm.Value;
  1796. }
  1797. static void OutParmTest6 (OracleConnection con)
  1798. {
  1799. // test stored function with 4 parameters
  1800. // 1. input timestamp
  1801. // 2. output timestamp
  1802. // 3. input output timestamp
  1803. // 4. return timestamp
  1804. // a TIMESTAMP type in Oracle has Date and Time
  1805. Console.WriteLine(" Create stored function SF_TESTOUTPARM6 to test Date parameters...");
  1806. OracleCommand cmd2 = con.CreateCommand();
  1807. cmd2.CommandText =
  1808. "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM6(parm1 IN TIMESTAMP, parm2 OUT TIMESTAMP, parm3 IN OUT TIMESTAMP) RETURN TIMESTAMP " +
  1809. "IS " +
  1810. " returnValue TIMESTAMP := TO_TIMESTAMP('2001-07-01 15:32:52', 'YYYY-MM-DD HH24:MI:SS');" +
  1811. "BEGIN " +
  1812. " IF parm1 IS NULL THEN " +
  1813. " parm2 := TO_TIMESTAMP('1900-12-31', 'YYYY-MM-DD'); " +
  1814. " parm3 := TO_TIMESTAMP('1900-12-31', 'YYYY-MM-DD'); " +
  1815. " ELSIF parm1 = TO_TIMESTAMP('1979-11-25','YYYY-MM-DD') THEN " +
  1816. " parm2 := NULL;" +
  1817. " parm3 := NULL;" +
  1818. " returnValue := NULL;"+
  1819. " ELSIF parm3 IS NULL THEN " +
  1820. " parm2 := TO_TIMESTAMP('2008-08-08', 'YYYY-MM-DD');" +
  1821. " parm3 := TO_TIMESTAMP('2000-01-01', 'YYYY-MM-DD');" +
  1822. " ELSE " +
  1823. " -- add 3 days to date\n " +
  1824. " parm2 := parm1 + 3; " +
  1825. " parm3 := parm3 + 5; " +
  1826. " END IF; " +
  1827. " RETURN returnValue;" +
  1828. "END;";
  1829. cmd2.ExecuteNonQuery();
  1830. Console.WriteLine(" COMMIT...");
  1831. cmd2.CommandText = "COMMIT";
  1832. cmd2.ExecuteNonQuery();
  1833. Console.WriteLine(" Call stored function SF_TESTOUTPARM6 with 4 parameters...");
  1834. OracleCommand cmd3 = con.CreateCommand();
  1835. cmd3.CommandType = CommandType.Text;
  1836. cmd3.CommandText =
  1837. "BEGIN " +
  1838. " :returnValue := SF_TESTOUTPARM6(:p1, :p2, :p3);" +
  1839. "END;";
  1840. OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Timestamp);
  1841. myParameter1.Value = new DateTime(2004, 12, 15);
  1842. myParameter1.Direction = ParameterDirection.Input;
  1843. OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Timestamp);
  1844. myParameter2.Direction = ParameterDirection.Output;
  1845. OracleParameter myParameter3 = new OracleParameter("p3", OracleType.Timestamp);
  1846. myParameter3.Value = new DateTime(2008, 10, 14, 20, 21, 22);
  1847. myParameter3.Direction = ParameterDirection.InputOutput;
  1848. OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.Timestamp);
  1849. myParameter4.Direction = ParameterDirection.ReturnValue;
  1850. cmd3.Parameters.Add(myParameter1);
  1851. cmd3.Parameters.Add(myParameter2);
  1852. cmd3.Parameters.Add(myParameter3);
  1853. cmd3.Parameters.Add(myParameter4);
  1854. cmd3.ExecuteNonQuery();
  1855. DateTime outValue = (DateTime)myParameter2.Value;
  1856. DateTime inOutValue = (DateTime)myParameter3.Value;
  1857. DateTime returnValue = (DateTime)myParameter4.Value;
  1858. Console.WriteLine(" 1Out Value should be: 2004-12-18 00:00:00");
  1859. Console.WriteLine(" 1Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1860. Console.WriteLine(" 1InOut Value should be: 2008-10-19 20:21:22");
  1861. Console.WriteLine(" 1InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1862. Console.WriteLine(" 1Return Value should be: 2001-07-01 15:32:52");
  1863. Console.WriteLine(" 1Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1864. Console.WriteLine();
  1865. myParameter1.Value = DBNull.Value;
  1866. myParameter3.Value = new DateTime(1980, 11, 22);
  1867. cmd3.ExecuteNonQuery();
  1868. outValue = (DateTime)myParameter2.Value;
  1869. inOutValue = (DateTime)myParameter3.Value;
  1870. returnValue = (DateTime)myParameter4.Value;
  1871. Console.WriteLine(" 2Out Value should be: 1900-12-31 00:00:00");
  1872. Console.WriteLine(" 2Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1873. Console.WriteLine(" 2InOut Value should be: 1900-12-31 00:00:00");
  1874. Console.WriteLine(" 2InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1875. Console.WriteLine(" 2Return Value should be: 2001-07-01 15:32:52");
  1876. Console.WriteLine(" 2Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1877. Console.WriteLine();
  1878. myParameter1.Value = new DateTime(1979, 11, 25);
  1879. myParameter3.Value = new DateTime(1981, 12, 14);
  1880. cmd3.ExecuteNonQuery();
  1881. string soutValue = "";
  1882. string sinOutValue = "";
  1883. string sreturnValue = "";
  1884. if (myParameter2.Value == DBNull.Value)
  1885. soutValue = "DBNull.Value";
  1886. else {
  1887. outValue = (DateTime)myParameter2.Value;
  1888. soutValue = outValue.ToString("yyyy-MM-dd HH:mm:ss");
  1889. }
  1890. if (myParameter3.Value == DBNull.Value)
  1891. sinOutValue = "DBNull.Value";
  1892. else {
  1893. inOutValue = (DateTime)myParameter3.Value;
  1894. sinOutValue = inOutValue.ToString("yyyy-MM-dd HH:mm:ss");
  1895. }
  1896. if (myParameter4.Value == DBNull.Value)
  1897. sreturnValue = "DBNull.Value";
  1898. else {
  1899. returnValue = (DateTime)myParameter4.Value;
  1900. sreturnValue = returnValue.ToString("yyyy-MM-dd HH:mm:ss");
  1901. }
  1902. Console.WriteLine(" 3Out Value should be: DBNull.Value");
  1903. Console.WriteLine(" 3Out Value: {0}", soutValue);
  1904. Console.WriteLine(" 3InOut Value should be: DBNull.Value");
  1905. Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
  1906. Console.WriteLine(" 3Return Value should be: DBNull.Value");
  1907. Console.WriteLine(" 3Return Value: {0}", sreturnValue);
  1908. Console.WriteLine();
  1909. myParameter1.Value = new DateTime(1976, 7, 4);
  1910. myParameter3.Value = DBNull.Value;
  1911. cmd3.ExecuteNonQuery();
  1912. outValue = (DateTime)myParameter2.Value;
  1913. inOutValue = (DateTime)myParameter3.Value;
  1914. returnValue = (DateTime)myParameter4.Value;
  1915. Console.WriteLine(" 4Out Value should be: 2008-08-08 00:00:00");
  1916. Console.WriteLine(" 4Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1917. Console.WriteLine(" 4InOut Value should be: 2000-01-01 00:00:00");
  1918. Console.WriteLine(" 4InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1919. Console.WriteLine(" 4Return Value should be: 2001-07-01 15:32:52");
  1920. Console.WriteLine(" 4Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
  1921. Console.WriteLine();
  1922. }
  1923. static void ShowConnectionProperties (OracleConnection con)
  1924. {
  1925. try {
  1926. Console.WriteLine ("ServerVersion: " + con.ServerVersion);
  1927. } catch (System.InvalidOperationException ioe) {
  1928. Console.WriteLine ("InvalidOperationException caught.");
  1929. Console.WriteLine ("Message: " + ioe.Message);
  1930. }
  1931. Console.WriteLine ("DataSource: " + con.DataSource);
  1932. }
  1933. static void NullAggregateTest (OracleConnection con)
  1934. {
  1935. Console.WriteLine(" Drop table MONO_TEST_TABLE3...");
  1936. OracleCommand cmd2 = con.CreateCommand ();
  1937. try {
  1938. cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE3";
  1939. cmd2.ExecuteNonQuery ();
  1940. }
  1941. catch(OracleException) {
  1942. // ignore if table already exists
  1943. }
  1944. Console.WriteLine(" Create table MONO_TEST_TABLE3...");
  1945. cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE3 (" +
  1946. " COL1 VARCHAR2(8), "+
  1947. " COL2 VARCHAR2(32))";
  1948. cmd2.ExecuteNonQuery ();
  1949. Console.WriteLine(" Insert some rows into table MONO_TEST_TABLE3...");
  1950. cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('1','one')";
  1951. cmd2.ExecuteNonQuery ();
  1952. cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('1','uno')";
  1953. cmd2.ExecuteNonQuery ();
  1954. cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3','three')";
  1955. cmd2.ExecuteNonQuery ();
  1956. cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3', null)";
  1957. cmd2.ExecuteNonQuery ();
  1958. cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3','few')";
  1959. cmd2.ExecuteNonQuery ();
  1960. Console.WriteLine(" ExecuteScalar...");
  1961. cmd2.CommandText = "SELECT COL1, COUNT(COL2) AS MAX_COL1 FROM MONO_TEST_TABLE3 GROUP BY COL1";
  1962. OracleDataReader reader = cmd2.ExecuteReader ();
  1963. Console.WriteLine (" Read...");
  1964. while (reader.Read ()) {
  1965. object obj0 = reader.GetValue (0);
  1966. Console.WriteLine("Value 0: " + obj0.ToString ());
  1967. object obj1 = reader.GetValue (1);
  1968. Console.WriteLine("Value 1: " + obj1.ToString ());
  1969. Console.WriteLine (" Read...");
  1970. }
  1971. Console.WriteLine (" No more records.");
  1972. }
  1973. static void OnInfoMessage (object sender, OracleInfoMessageEventArgs e)
  1974. {
  1975. Console.WriteLine("InfoMessage Message: " + e.Message.ToString());
  1976. Console.WriteLine("InfoMessage Code: " + e.Code.ToString());
  1977. Console.WriteLine("InfoMessage Source: " + e.Source.ToString());
  1978. }
  1979. static void OnStateChange (object sender, StateChangeEventArgs e)
  1980. {
  1981. Console.WriteLine("StateChange CurrentSate:" + e.CurrentState.ToString ());
  1982. Console.WriteLine("StateChange OriginalState:" + e.OriginalState.ToString ());
  1983. }
  1984. static void RefCursorTests(OracleConnection con)
  1985. {
  1986. SetupRefCursorTests(con); // for ref cursor tests 1 thru 3
  1987. RefCursorTest1(con); // using BEGIN/END
  1988. RefCursorTest2(con); // using call
  1989. RefCursorTest3(con); // using CommandType.StoredProcedure
  1990. RefCursorTest4(con);
  1991. }
  1992. static void SetupRefCursorTests(OracleConnection con)
  1993. {
  1994. Console.WriteLine("Setup Oracle package curspkg_join...");
  1995. OracleCommand cmd = con.CreateCommand();
  1996. Console.Error.WriteLine(" create or replace package curspkg_join...");
  1997. cmd.CommandText =
  1998. "CREATE OR REPLACE PACKAGE curspkg_join AS\n" +
  1999. "TYPE t_cursor IS REF CURSOR;\n" +
  2000. "Procedure open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor);\n" +
  2001. "END curspkg_join;";
  2002. cmd.ExecuteNonQuery();
  2003. Console.Error.WriteLine(" create or replace package body curspkg_join...");
  2004. cmd.CommandText =
  2005. "CREATE OR REPLACE PACKAGE BODY curspkg_join AS\n" +
  2006. " Procedure open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)\n" +
  2007. " IS\n" +
  2008. " v_cursor t_cursor;\n" +
  2009. " BEGIN\n" +
  2010. " IF n_EMPNO <> 0 THEN\n" +
  2011. " OPEN v_cursor FOR\n" +
  2012. " SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME\n" +
  2013. " FROM SCOTT.EMP, SCOTT.DEPT\n" +
  2014. " WHERE EMP.DEPTNO = DEPT.DEPTNO\n" +
  2015. " AND EMP.EMPNO = n_EMPNO;\n" +
  2016. "\n" +
  2017. " ELSE\n" +
  2018. " OPEN v_cursor FOR\n" +
  2019. " SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME\n" +
  2020. " FROM SCOTT.EMP, SCOTT.DEPT\n" +
  2021. " WHERE EMP.DEPTNO = DEPT.DEPTNO;\n" +
  2022. "\n" +
  2023. " END IF;\n" +
  2024. " io_cursor := v_cursor;\n" +
  2025. " END open_join_cursor1;\n" +
  2026. "END curspkg_join;";
  2027. cmd.ExecuteNonQuery();
  2028. cmd.CommandText = "commit";
  2029. cmd.ExecuteNonQuery();
  2030. }
  2031. public static void RefCursorTest4(OracleConnection connection)
  2032. {
  2033. Console.WriteLine("Setup test package and data for RefCursorTest4...");
  2034. OracleCommand cmddrop = connection.CreateCommand();
  2035. cmddrop.CommandText = "DROP TABLE TESTTABLE";
  2036. try {
  2037. cmddrop.ExecuteNonQuery();
  2038. }
  2039. catch(OracleException e) {
  2040. Console.WriteLine("Ignore this error: " + e.Message);
  2041. }
  2042. cmddrop.Dispose();
  2043. cmddrop = null;
  2044. OracleCommand cmd = connection.CreateCommand();
  2045. // create table TESTTABLE
  2046. cmd.CommandText =
  2047. "create table TESTTABLE (\n" +
  2048. " col1 numeric(18,0),\n" +
  2049. " col2 char(32),\n" +
  2050. " col3 date)";
  2051. cmd.ExecuteNonQuery();
  2052. // insert some rows into TESTTABLE
  2053. cmd.CommandText =
  2054. "insert into TESTTABLE\n" +
  2055. "(col1, col2, col3)\n" +
  2056. "values(45, 'Mono', sysdate)";
  2057. cmd.ExecuteNonQuery();
  2058. cmd.CommandText =
  2059. "insert into TESTTABLE\n" +
  2060. "(col1, col2, col3)\n" +
  2061. "values(136, 'Fun', sysdate)";
  2062. cmd.ExecuteNonQuery();
  2063. cmd.CommandText =
  2064. "insert into TESTTABLE\n" +
  2065. "(col1, col2, col3)\n" +
  2066. "values(526, 'System.Data.OracleClient', sysdate)";
  2067. cmd.ExecuteNonQuery();
  2068. cmd.CommandText = "commit";
  2069. cmd.ExecuteNonQuery();
  2070. // create Oracle package TestTablePkg
  2071. cmd.CommandText =
  2072. "CREATE OR REPLACE PACKAGE TestTablePkg\n" +
  2073. "AS\n" +
  2074. " TYPE T_CURSOR IS REF CURSOR;\n" +
  2075. "\n" +
  2076. " PROCEDURE GetData(tableCursor OUT T_CURSOR);\n" +
  2077. "END TestTablePkg;";
  2078. cmd.ExecuteNonQuery();
  2079. // create Oracle package body for package TestTablePkg
  2080. cmd.CommandText =
  2081. "CREATE OR REPLACE PACKAGE BODY TestTablePkg AS\n" +
  2082. " PROCEDURE GetData(tableCursor OUT T_CURSOR)\n" +
  2083. " IS\n" +
  2084. " BEGIN\n" +
  2085. " OPEN tableCursor FOR\n" +
  2086. " SELECT *\n" +
  2087. " FROM TestTable;\n" +
  2088. " END GetData;\n" +
  2089. "END TestTablePkg;";
  2090. cmd.ExecuteNonQuery();
  2091. cmd.Dispose();
  2092. cmd = null;
  2093. Console.WriteLine("Set up command and parameters to call stored proc...");
  2094. OracleCommand command = new OracleCommand("TestTablePkg.GetData", connection);
  2095. command.CommandType = CommandType.StoredProcedure;
  2096. OracleParameter parameter = new OracleParameter("tableCursor", OracleType.Cursor);
  2097. parameter.Direction = ParameterDirection.Output;
  2098. command.Parameters.Add(parameter);
  2099. Console.WriteLine("Execute...");
  2100. command.ExecuteNonQuery();
  2101. Console.WriteLine("Get OracleDataReader for cursor output parameter...");
  2102. OracleDataReader reader = (OracleDataReader) parameter.Value;
  2103. Console.WriteLine("Read data...");
  2104. int r = 0;
  2105. while (reader.Read()) {
  2106. Console.WriteLine("Row {0}", r);
  2107. for (int f = 0; f < reader.FieldCount; f ++) {
  2108. object val = reader.GetValue(f);
  2109. Console.WriteLine(" Field {0} Value: {1}", f, val.ToString());
  2110. }
  2111. r ++;
  2112. }
  2113. Console.WriteLine("Rows retrieved: {0}", r);
  2114. Console.WriteLine("Clean up...");
  2115. reader.Close();
  2116. reader = null;
  2117. command.Dispose();
  2118. command = null;
  2119. }
  2120. static void RefCursorTest1(OracleConnection con)
  2121. {
  2122. Console.WriteLine("Ref Cursor Test 1 - using BEGIN/END for proc - Begin...");
  2123. Console.WriteLine("Create command...");
  2124. OracleCommand cmd = new OracleCommand();
  2125. cmd.Connection = con;
  2126. cmd.CommandText =
  2127. "BEGIN\n" +
  2128. " curspkg_join.open_join_cursor1(:n_Empno,:io_cursor);\n" +
  2129. "END;";
  2130. // PL/SQL definition of stored procedure in package curspkg_join
  2131. // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
  2132. Console.WriteLine("Create parameters...");
  2133. OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
  2134. parm1.Direction = ParameterDirection.Input;
  2135. parm1.Value = 7902;
  2136. OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
  2137. parm2.Direction = ParameterDirection.Output;
  2138. cmd.Parameters.Add(parm1);
  2139. cmd.Parameters.Add(parm2);
  2140. // positional parm
  2141. //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
  2142. // named parm
  2143. //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
  2144. OracleDataReader reader;
  2145. Console.WriteLine("Execute Non Query...");
  2146. cmd.ExecuteNonQuery();
  2147. Console.WriteLine("Get data reader (ref cursor) from out parameter...");
  2148. reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
  2149. int x, count;
  2150. count = 0;
  2151. Console.WriteLine("Get data from ref cursor...");
  2152. while (reader.Read()) {
  2153. for (x = 0; x < reader.FieldCount; x++)
  2154. Console.Write(reader[x] + " ");
  2155. Console.WriteLine();
  2156. count += 1;
  2157. }
  2158. Console.WriteLine(count.ToString() + " Rows Returned.");
  2159. reader.Close();
  2160. }
  2161. static void RefCursorTest2(OracleConnection con)
  2162. {
  2163. Console.WriteLine("Ref Cursor Test 2 - using call - Begin...");
  2164. Console.WriteLine("Create command...");
  2165. OracleCommand cmd = new OracleCommand();
  2166. cmd.Connection = con;
  2167. cmd.CommandText = "call curspkg_join.open_join_cursor1(:n_Empno,:io_cursor)";
  2168. // PL/SQL definition of stored procedure in package curspkg_join
  2169. // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
  2170. Console.WriteLine("Create parameters...");
  2171. OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
  2172. parm1.Direction = ParameterDirection.Input;
  2173. parm1.Value = 7902;
  2174. OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
  2175. parm2.Direction = ParameterDirection.Output;
  2176. cmd.Parameters.Add(parm1);
  2177. cmd.Parameters.Add(parm2);
  2178. // positional parm
  2179. //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
  2180. // named parm
  2181. //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
  2182. OracleDataReader reader;
  2183. Console.WriteLine("Execute Non Query...");
  2184. cmd.ExecuteNonQuery();
  2185. Console.WriteLine("Get data reader (ref cursor) from out parameter...");
  2186. reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
  2187. int x, count;
  2188. count = 0;
  2189. Console.WriteLine("Get data from ref cursor...");
  2190. while (reader.Read()) {
  2191. for (x = 0; x < reader.FieldCount; x++)
  2192. Console.Write(reader[x] + " ");
  2193. Console.WriteLine();
  2194. count += 1;
  2195. }
  2196. Console.WriteLine(count.ToString() + " Rows Returned.");
  2197. reader.Close();
  2198. }
  2199. static void RefCursorTest3(OracleConnection con)
  2200. {
  2201. Console.WriteLine("Ref Cursor Test 3 - CommandType.StoredProcedure - Begin...");
  2202. Console.WriteLine("Create command...");
  2203. OracleCommand cmd = new OracleCommand();
  2204. cmd.Connection = con;
  2205. cmd.CommandText = "curspkg_join.open_join_cursor1";
  2206. cmd.CommandType = CommandType.StoredProcedure;
  2207. // PL/SQL definition of stored procedure in package curspkg_join
  2208. // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
  2209. Console.WriteLine("Create parameters...");
  2210. OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
  2211. parm1.Direction = ParameterDirection.Input;
  2212. parm1.Value = 7902;
  2213. OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
  2214. parm2.Direction = ParameterDirection.Output;
  2215. cmd.Parameters.Add(parm1);
  2216. cmd.Parameters.Add(parm2);
  2217. // positional parm
  2218. //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
  2219. // named parm
  2220. //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
  2221. OracleDataReader reader;
  2222. Console.WriteLine("Execute Non Query...");
  2223. cmd.ExecuteNonQuery();
  2224. Console.WriteLine("Get data reader (ref cursor) from out parameter...");
  2225. reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
  2226. int x, count;
  2227. count = 0;
  2228. Console.WriteLine("Get data from ref cursor...");
  2229. while (reader.Read()) {
  2230. for (x = 0; x < reader.FieldCount; x++)
  2231. Console.Write(reader[x] + " ");
  2232. Console.WriteLine();
  2233. count += 1;
  2234. }
  2235. Console.WriteLine(count.ToString() + " Rows Returned.");
  2236. reader.Close();
  2237. }
  2238. static void ExternalAuthenticationTest ()
  2239. {
  2240. string user = Environment.UserName;
  2241. if (!Environment.UserDomainName.Equals(String.Empty))
  2242. user = Environment.UserDomainName + "\\" + Environment.UserName;
  2243. Console.WriteLine("Environment UserDomainName and UserName: " + user);
  2244. Console.WriteLine("Open connection using external authentication...");
  2245. OracleConnection con = new OracleConnection("Data Source=palis;Integrated Security=true");
  2246. try {
  2247. con.Open();
  2248. OracleCommand cmd = con.CreateCommand();
  2249. cmd.CommandText = "SELECT USER FROM DUAL";
  2250. OracleDataReader reader = cmd.ExecuteReader();
  2251. if (reader.Read())
  2252. Console.WriteLine("User: " + reader.GetString(reader.GetOrdinal("USER")));
  2253. con.Close();
  2254. }
  2255. catch (Exception e) {
  2256. Console.WriteLine("Exception caught: " + e.Message);
  2257. Console.WriteLine("Probably not setup for external authentication.");
  2258. }
  2259. con.Dispose();
  2260. con = null;
  2261. }
  2262. public static void TestPersistSucurityInfo1()
  2263. {
  2264. Console.WriteLine("\nTestPersistSucurityInfo1 - persist security info=false");
  2265. OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=false");
  2266. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2267. con.Open();
  2268. Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
  2269. con.Close();
  2270. Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
  2271. con = null;
  2272. }
  2273. public static void TestPersistSucurityInfo2()
  2274. {
  2275. Console.WriteLine("\nTestPersistSucurityInfo2 - persist security info=true");
  2276. OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=true");
  2277. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2278. con.Open();
  2279. Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
  2280. con.Close();
  2281. Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
  2282. con = null;
  2283. }
  2284. public static void TestPersistSucurityInfo3()
  2285. {
  2286. Console.WriteLine("\nTestPersistSucurityInfo3 - use default for persist security info which is false");
  2287. OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger");
  2288. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2289. con.Open();
  2290. Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
  2291. con.Close();
  2292. Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
  2293. con = null;
  2294. }
  2295. public static void TestPersistSucurityInfo4()
  2296. {
  2297. Console.WriteLine("\nTestPersistSucurityInfo4 - persist security info=false with password at front");
  2298. OracleConnection con = new OracleConnection(";password=tiger;data source=palis;user id=scott;persist security info=false");
  2299. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2300. con.Open();
  2301. Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
  2302. con.Close();
  2303. Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
  2304. con = null;
  2305. }
  2306. public static void TestPersistSucurityInfo5()
  2307. {
  2308. Console.WriteLine("\nTestPersistSucurityInfo5 - persist security info=false");
  2309. OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=false");
  2310. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2311. con.Open();
  2312. Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
  2313. Console.WriteLine("ConnectionState for con: " + con.State.ToString() + "\n");
  2314. Console.WriteLine("Clone OracleConnection...");
  2315. OracleConnection con2 = (OracleConnection) ((ICloneable) con).Clone();
  2316. Console.WriteLine("ConnectionState for con2: " + con2.State.ToString());
  2317. Console.WriteLine("con2 ConnectionString before open: " + con2.ConnectionString);
  2318. con2.Open();
  2319. Console.WriteLine("con2 ConnectionString after open: " + con2.ConnectionString);
  2320. con2.Close();
  2321. Console.WriteLine("con2 ConnectionString after close: " + con2.ConnectionString);
  2322. con.Close();
  2323. }
  2324. public static void TestPersistSucurityInfo6()
  2325. {
  2326. Console.WriteLine("\nTestPersistSucurityInfo6 - external auth using persist security info");
  2327. string user = Environment.UserName;
  2328. if (!Environment.UserDomainName.Equals(String.Empty))
  2329. user = Environment.UserDomainName + "\\" + Environment.UserName;
  2330. Console.WriteLine("Environment UserDomainName and UserName: " + user);
  2331. Console.WriteLine("Open connection using external authentication...");
  2332. OracleConnection con = new OracleConnection("Data Source=palis;Integrated Security=true");
  2333. Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
  2334. try {
  2335. con.Open();
  2336. OracleCommand cmd = con.CreateCommand();
  2337. cmd.CommandText = "SELECT USER FROM DUAL";
  2338. OracleDataReader reader = cmd.ExecuteReader();
  2339. if (reader.Read())
  2340. Console.WriteLine("User: " + reader.GetString(reader.GetOrdinal("USER")));
  2341. con.Close();
  2342. Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
  2343. }
  2344. catch (Exception e) {
  2345. Console.WriteLine("Exception caught: " + e.Message);
  2346. Console.WriteLine("Probably not setup for external authentication. This is fine.");
  2347. }
  2348. con.Dispose();
  2349. Console.WriteLine("ConnectionString after dispose: " + con.ConnectionString);
  2350. con = null;
  2351. Console.WriteLine("\n\n");
  2352. }
  2353. public static void ConnectionPoolingTest1 ()
  2354. {
  2355. Console.WriteLine("Start Connection Pooling Test 1...");
  2356. OracleConnection[] connections = null;
  2357. int maxCon = MAX_CONNECTIONS + 1; // add 1 more over the max connections to cause it to wait for the next available connection
  2358. int i = 0;
  2359. try {
  2360. connections = new OracleConnection[maxCon];
  2361. for (i = 0; i < maxCon; i++) {
  2362. Console.WriteLine(" Open connection: {0}", i);
  2363. connections[i] = new OracleConnection(conStr);
  2364. connections[i].Open ();
  2365. }
  2366. } catch (InvalidOperationException e) {
  2367. Console.WriteLine("Expected exception InvalidOperationException caught.");
  2368. Console.WriteLine(e);
  2369. }
  2370. for (i = 0; i < maxCon; i++) {
  2371. if (connections[i] != null) {
  2372. Console.WriteLine(" Close connection: {0}", i);
  2373. if (connections[i].State == ConnectionState.Open)
  2374. connections[i].Close ();
  2375. connections[i].Dispose ();
  2376. connections[i] = null;
  2377. }
  2378. }
  2379. connections = null;
  2380. Console.WriteLine("Done Connection Pooling Test 1.");
  2381. }
  2382. public static void ConnectionPoolingTest2 ()
  2383. {
  2384. Console.WriteLine("Start Connection Pooling Test 2...");
  2385. OracleConnection[] connections = null;
  2386. int maxCon = MAX_CONNECTIONS;
  2387. int i = 0;
  2388. connections = new OracleConnection[maxCon];
  2389. for (i = 0; i < maxCon; i++) {
  2390. Console.WriteLine(" Open connection: {0}", i);
  2391. connections[i] = new OracleConnection(conStr);
  2392. connections[i].Open ();
  2393. }
  2394. Console.WriteLine("Start another thread...");
  2395. t = new Thread(new ThreadStart(AnotherThreadProc));
  2396. t.Start ();
  2397. Console.WriteLine("Sleep...");
  2398. Thread.Sleep(100);
  2399. Console.WriteLine("Closing...");
  2400. for (i = 0; i < maxCon; i++) {
  2401. if (connections[i] != null) {
  2402. Console.WriteLine(" Close connection: {0}", i);
  2403. if (connections[i].State == ConnectionState.Open)
  2404. connections[i].Close ();
  2405. connections[i].Dispose ();
  2406. connections[i] = null;
  2407. }
  2408. }
  2409. connections = null;
  2410. }
  2411. private static void AnotherThreadProc ()
  2412. {
  2413. Console.WriteLine("Open connection via another thread...");
  2414. OracleConnection[] connections = null;
  2415. int maxCon = MAX_CONNECTIONS;
  2416. int i = 0;
  2417. connections = new OracleConnection[maxCon];
  2418. for (i = 0; i < maxCon; i++) {
  2419. Console.WriteLine(" Open connection: {0}", i);
  2420. connections[i] = new OracleConnection(conStr);
  2421. connections[i].Open ();
  2422. }
  2423. Console.WriteLine("Done Connection Pooling Test 2.");
  2424. System.Environment.Exit (0);
  2425. }
  2426. private static void SetParameterOracleType (OracleConnection con)
  2427. {
  2428. Console.WriteLine();
  2429. OracleParameter p = con.CreateCommand().CreateParameter();
  2430. Console.WriteLine("p.OracleType [VarChar]: " + p.OracleType.ToString());
  2431. p.OracleType = OracleType.Clob;
  2432. Console.WriteLine("p.OracleType [Clob]: " + p.OracleType.ToString());
  2433. p.Value = "SomeString";
  2434. Console.WriteLine("p.OracleType [Clob]: " + p.OracleType.ToString());
  2435. Console.WriteLine();
  2436. OracleParameter p2 = con.CreateCommand().CreateParameter();
  2437. Console.WriteLine("p2.OracleType [VarChar]: " + p2.OracleType.ToString());
  2438. p2.Value = new byte[] { 0x01, 0x02, 0x03, 0x04 };
  2439. Console.WriteLine("p2.OracleType [VarChar]: " + p2.OracleType.ToString());
  2440. p2.OracleType = OracleType.Blob;
  2441. Console.WriteLine("p2.OracleType [Blob]: " + p2.OracleType.ToString());
  2442. Console.WriteLine();
  2443. OracleParameter p3 = new OracleParameter("test", OracleType.Clob);
  2444. Console.WriteLine("p3.OracleType [Clob]: " + p3.OracleType.ToString());
  2445. p3.Value = "blah";
  2446. Console.WriteLine("p3.OracleType [Clob]: " + p3.OracleType.ToString());
  2447. Console.WriteLine();
  2448. OracleParameter p4 = new OracleParameter("test", "blah");
  2449. Console.WriteLine("p4.OracleType [VarChar]: " + p4.OracleType.ToString());
  2450. p4.OracleType = OracleType.Clob;
  2451. Console.WriteLine("p4.OracleType [Clob]: " + p4.OracleType.ToString());
  2452. Console.WriteLine();
  2453. OracleParameter p5 = new OracleParameter ((string) null, new DateTime (2005, 3, 8));
  2454. Console.WriteLine("p5.OracleType [DateTime]: " + p5.OracleType.ToString());
  2455. }
  2456. public static void InsertBlobTest(OracleConnection con)
  2457. {
  2458. checkTNS();
  2459. SetupMyPackage(con);
  2460. InsertBlob(con);
  2461. }
  2462. public static void checkTNS()
  2463. {
  2464. //string tnsAdmin = System.Environment.GetEnvironmentVariable("TNS_ADMIN");
  2465. //if ( (tnsAdmin == null)|| (string.Empty.Equals(tnsAdmin)) )
  2466. //{
  2467. // System.Environment.SetEnvironmentVariable("TNS_ADMIN", "~/instantclient");
  2468. //}
  2469. }
  2470. public static decimal InsertBlob(OracleConnection con)
  2471. {
  2472. byte[] ByteArray = new byte[2000]; // test Blob data
  2473. byte j = 0;
  2474. for (int i = 0; i < ByteArray.Length; i++) {
  2475. ByteArray[i] = j;
  2476. if (j > 255)
  2477. j = 0;
  2478. j++;
  2479. }
  2480. Console.WriteLine("Test Blob Data beginning: " + GetHexString (ByteArray));
  2481. decimal retVal = -1;
  2482. string sproc = "MyPackage" + ".InsertBlob";
  2483. OracleCommand cmd = new OracleCommand();
  2484. cmd.CommandText = sproc;
  2485. cmd.CommandType = CommandType.StoredProcedure;
  2486. cmd.Connection = con;
  2487. //cmd.Connection.Open();
  2488. cmd.Transaction = cmd.Connection.BeginTransaction();
  2489. try {
  2490. OracleParameter p1 = new OracleParameter("i_Sig_File", OracleType.Blob);
  2491. p1.Direction = ParameterDirection.Input;
  2492. //EXCEPTION thrown here
  2493. //p1.Value = GetOracleLob(cmd.Transaction, ByteArray);
  2494. OracleLob lob2 = GetOracleLob(cmd.Transaction, ByteArray);
  2495. byte[] b2 = (byte[]) lob2.Value;
  2496. Console.WriteLine("Test Blob Data here: " + GetHexString (b2));
  2497. p1.Value = lob2.Value;
  2498. //p1.Value = ByteArray;
  2499. cmd.Parameters.Add(p1);
  2500. cmd.ExecuteNonQuery();
  2501. cmd.Transaction.Commit();
  2502. OracleCommand select = con.CreateCommand ();
  2503. //select.Transaction = transaction;
  2504. select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST2";
  2505. Console.WriteLine (" SELECTING A BLOB (Binary) VALUE FROM BLOBTEST2");
  2506. OracleDataReader reader = select.ExecuteReader ();
  2507. if (!reader.Read ())
  2508. Console.WriteLine ("ERROR: RECORD NOT FOUND");
  2509. Console.WriteLine (" TESTING OracleLob OBJECT ...");
  2510. if (reader.IsDBNull(0))
  2511. Console.WriteLine("Lob IsNull");
  2512. else {
  2513. OracleLob lob = reader.GetOracleLob (0);
  2514. if (lob == OracleLob.Null)
  2515. Console.WriteLine("Lob is OracleLob.Null");
  2516. else {
  2517. byte[] blob = (byte[]) lob.Value;
  2518. string result = GetHexString(blob);
  2519. Console.WriteLine("Blob result: " + result);
  2520. if (ByteArrayCompare (ByteArray, blob))
  2521. Console.WriteLine("ByteArray and blob are the same: good");
  2522. else
  2523. Console.WriteLine("ByteArray and blob are not the same: bad");
  2524. }
  2525. }
  2526. }
  2527. catch(Exception ex) {
  2528. Console.WriteLine("I exploded:" + ex.ToString());
  2529. cmd.Transaction.Rollback();
  2530. }
  2531. return retVal;
  2532. }
  2533. private static OracleLob GetOracleLob(OracleTransaction transaction, byte[] blob)
  2534. {
  2535. string BLOB_CREATE = "DECLARE dpBlob BLOB; "
  2536. + "BEGIN "
  2537. + " DBMS_LOB.CREATETEMPORARY(dpBlob , False, 0); "
  2538. + " :tempBlob := dpBlob; "
  2539. + "END;";
  2540. OracleLob tempLob = OracleLob.Null;
  2541. if (blob != null)
  2542. {
  2543. // Create a new command using the same connection
  2544. OracleCommand command = transaction.Connection.CreateCommand();
  2545. // Assign the transaction to the command
  2546. command.Transaction = transaction;
  2547. // Create blob storage on the Oracle server
  2548. command.CommandText = BLOB_CREATE;
  2549. // Add a new output paramter to accept the blob storage reference
  2550. OracleParameter parm = new OracleParameter("tempBlob", OracleType.Blob);
  2551. parm.Direction = ParameterDirection.Output;
  2552. command.Parameters.Add(parm);
  2553. // command.Parameters.Add(
  2554. // new OracleParameter("tempBlob", OracleType.Blob)).Direction =
  2555. // ParameterDirection.Output;
  2556. // Fire as your guns bear...
  2557. command.ExecuteNonQuery();
  2558. // Retrieve the blob stream from the OracleLob parameter
  2559. //tempLob = (OracleLob)command.Parameters[0].Value;
  2560. tempLob = (OracleLob) parm.Value;
  2561. // Prevent server side events from firing while we write to the stream
  2562. tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
  2563. // Write bytes to the stream
  2564. tempLob.Write(blob, 0, blob.Length);
  2565. // Resume firing server events
  2566. tempLob.EndBatch();
  2567. }
  2568. return tempLob;
  2569. }
  2570. static void SetupMyPackage(OracleConnection con)
  2571. {
  2572. Console.WriteLine("Setup Oracle package curspkg_join...");
  2573. Console.WriteLine (" Drop table BLOBTEST2 ...");
  2574. try {
  2575. OracleCommand cmd2 = con.CreateCommand ();
  2576. //cmd2.Transaction = transaction;
  2577. cmd2.CommandText = "DROP TABLE BLOBTEST2";
  2578. cmd2.ExecuteNonQuery ();
  2579. }
  2580. catch (OracleException) {
  2581. // ignore if table already exists
  2582. }
  2583. Console.WriteLine (" CREATE TABLE ...");
  2584. OracleCommand create = con.CreateCommand ();
  2585. //create.Transaction = transaction;
  2586. create.CommandText = "CREATE TABLE BLOBTEST2 (BLOB_COLUMN BLOB)";
  2587. create.ExecuteNonQuery ();
  2588. create.CommandText = "commit";
  2589. create.ExecuteNonQuery();
  2590. Console.Error.WriteLine(" create or replace package MyPackage...");
  2591. OracleCommand cmd = con.CreateCommand();
  2592. cmd.CommandText =
  2593. "CREATE OR REPLACE PACKAGE MyPackage AS\n" +
  2594. " Procedure InsertBlob (i_Sig_File blob);\n" +
  2595. "END MyPackage;";
  2596. cmd.ExecuteNonQuery();
  2597. Console.Error.WriteLine(" create or replace package body MyPackage...");
  2598. cmd.CommandText =
  2599. "CREATE OR REPLACE PACKAGE BODY MyPackage AS\n" +
  2600. " Procedure InsertBlob (i_Sig_File blob)\n" +
  2601. " IS\n" +
  2602. " BEGIN\n" +
  2603. " INSERT INTO BLOBTEST2 (BLOB_COLUMN) VALUES(i_Sig_File); " +
  2604. " END InsertBlob; " +
  2605. "END MyPackage;";
  2606. cmd.ExecuteNonQuery();
  2607. cmd.CommandText = "commit";
  2608. cmd.ExecuteNonQuery();
  2609. }
  2610. static byte[] ByteArrayCombine (byte[] b1, byte[] b2)
  2611. {
  2612. if (b1 == null)
  2613. b1 = new byte[0];
  2614. if (b2 == null)
  2615. b2 = new byte[0];
  2616. byte[] bytes = new byte[b1.Length + b2.Length];
  2617. int i = 0;
  2618. for (int j = 0; j < b1.Length; j++) {
  2619. bytes[i] = b1[j];
  2620. i++;
  2621. }
  2622. for (int k = 0; k < b2.Length; k++) {
  2623. bytes[i] = b2[k];
  2624. i++;
  2625. }
  2626. return bytes;
  2627. }
  2628. static bool ByteArrayCompare(byte[] ba1, byte[] ba2)
  2629. {
  2630. if (ba1 == null && ba2 == null)
  2631. return true;
  2632. if (ba1 == null)
  2633. return false;
  2634. if (ba2 == null)
  2635. return false;
  2636. if (ba1.Length != ba2.Length)
  2637. return false;
  2638. // for (int i = 0; i < ba1.Length; i++)
  2639. // {
  2640. //Console.WriteLine("i: " + i.ToString() + " ba1: " + ba1[i].ToString() + " ba2: " + ba2[i].ToString());
  2641. //}
  2642. for (int i = 0; i < ba1.Length; i++)
  2643. {
  2644. if (ba1[i] != ba2[i])
  2645. return false;
  2646. }
  2647. return true;
  2648. }
  2649. [STAThread]
  2650. static void Main(string[] args)
  2651. {
  2652. if(args.Length != 3) {
  2653. Console.WriteLine("Usage: mono TestOracleClient database userid password");
  2654. return;
  2655. }
  2656. string connectionString = String.Format(
  2657. "Data Source={0};" +
  2658. "User ID={1};" +
  2659. "Password={2}",
  2660. args[0], args[1], args[2]);
  2661. conStr = connectionString;
  2662. OracleConnection con1 = new OracleConnection();
  2663. ShowConnectionProperties (con1);
  2664. con1.ConnectionString = connectionString;
  2665. con1.InfoMessage += new OracleInfoMessageEventHandler (OnInfoMessage);
  2666. con1.StateChange += new StateChangeEventHandler (OnStateChange);
  2667. Console.WriteLine("Opening...");
  2668. con1.Open ();
  2669. Console.WriteLine("Opened.");
  2670. ShowConnectionProperties (con1);
  2671. InsertBlobTest (con1);
  2672. Console.WriteLine ("Mono Oracle Test BEGIN ...");
  2673. MonoTest (con1);
  2674. Console.WriteLine ("Mono Oracle Test END ...");
  2675. Wait ("");
  2676. Console.WriteLine ("LOB Test BEGIN...");
  2677. CLOBTest (con1);
  2678. BLOBTest (con1);
  2679. Console.WriteLine ("LOB Test END.");
  2680. Wait ("");
  2681. Console.WriteLine ("Read Simple Test BEGIN - scott.emp...");
  2682. ReadSimpleTest(con1, "SELECT e.*, e.rowid FROM scott.emp e");
  2683. Console.WriteLine ("Read Simple Test END - scott.emp");
  2684. Wait ("");
  2685. Console.WriteLine ("DataAdapter Test BEGIN...");
  2686. DataAdapterTest(con1);
  2687. Console.WriteLine ("DataAdapter Test END.");
  2688. Wait ("");
  2689. Console.WriteLine ("DataAdapter Test 2 BEGIN...");
  2690. DataAdapterTest2(con1);
  2691. Console.WriteLine ("DataAdapter Test 2 END.");
  2692. Wait ("");
  2693. Console.WriteLine ("Rollback Test BEGIN...");
  2694. RollbackTest(con1);
  2695. Console.WriteLine ("Rollback Test END.");
  2696. Wait ("");
  2697. Console.WriteLine ("Commit Test BEGIN...");
  2698. CommitTest(con1);
  2699. Console.WriteLine ("Commit Test END.");
  2700. Wait ("");
  2701. Console.WriteLine ("Parameter Test BEGIN...");
  2702. ParameterTest(con1);
  2703. ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE7");
  2704. Console.WriteLine ("Parameter Test END.");
  2705. Wait ("");
  2706. Console.WriteLine ("Stored Proc Test 1 BEGIN...");
  2707. StoredProcedureTest1 (con1);
  2708. ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE1");
  2709. Console.WriteLine ("Stored Proc Test 1 END...");
  2710. Wait ("");
  2711. Console.WriteLine ("Stored Proc Test 2 BEGIN...");
  2712. StoredProcedureTest2 (con1);
  2713. ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE2");
  2714. Console.WriteLine ("Stored Proc Test 2 END...");
  2715. SetParameterOracleType (con1);
  2716. Console.WriteLine ("Out Parameter and PL/SQL Block Test 1 BEGIN...");
  2717. OutParmTest1 (con1);
  2718. Console.WriteLine ("Out Parameter and PL/SQL Block Test 1 END...");
  2719. Console.WriteLine ("Out Parameter and PL/SQL Block Test 2 BEGIN...");
  2720. OutParmTest2 (con1);
  2721. Console.WriteLine ("Out Parameter and PL/SQL Block Test 2 END...");
  2722. Console.WriteLine ("Out Parameter and PL/SQL Block Test 3 BEGIN...");
  2723. OutParmTest3 (con1);
  2724. Console.WriteLine ("Out Parameter and PL/SQL Block Test 3 END...");
  2725. Console.WriteLine ("Out Parameter and PL/SQL Block Test 4 BEGIN...");
  2726. OutParmTest4 (con1);
  2727. Console.WriteLine ("Out Parameter and PL/SQL Block Test 4 END...");
  2728. Console.WriteLine ("Out Parameter and PL/SQL Block Test 5 BEGIN...");
  2729. OutParmTest5 (con1);
  2730. Console.WriteLine ("Out Parameter and PL/SQL Block Test 5 END...");
  2731. Console.WriteLine ("Out Parameter and PL/SQL Block Test 6 BEGIN...");
  2732. OutParmTest6 (con1);
  2733. Console.WriteLine ("Out Parameter and PL/SQL Block Test 6 END...");
  2734. Wait ("");
  2735. Console.WriteLine ("Test a Non Query using Execute Reader BEGIN...");
  2736. TestNonQueryUsingExecuteReader (con1);
  2737. Console.WriteLine ("Test a Non Query using Execute Reader END...");
  2738. Wait ("");
  2739. Console.WriteLine ("Null Aggregate Warning BEGIN test...");
  2740. NullAggregateTest (con1);
  2741. Console.WriteLine ("Null Aggregate Warning END test...");
  2742. Console.WriteLine ("Ref Cursor BEGIN tests...");
  2743. RefCursorTests (con1);
  2744. Console.WriteLine ("Ref Cursor END tests...");
  2745. Console.WriteLine("Closing...");
  2746. con1.Close ();
  2747. Console.WriteLine("Closed.");
  2748. conStr = conStr + ";pooling=true;min pool size=4;max pool size=" + MAX_CONNECTIONS.ToString ();
  2749. ConnectionPoolingTest1 (); ConnectionPoolingTest2 ();
  2750. // Need to have an external authentication user setup in Linux and oracle
  2751. // before running this test
  2752. //ExternalAuthenticationTest();
  2753. TestPersistSucurityInfo1();
  2754. TestPersistSucurityInfo2();
  2755. TestPersistSucurityInfo3();
  2756. TestPersistSucurityInfo4();
  2757. TestPersistSucurityInfo5();
  2758. TestPersistSucurityInfo6();
  2759. Console.WriteLine("Done.");
  2760. }
  2761. }
  2762. }