| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345 |
- //
- // TestOracleClient.cs - Tests Sytem.Data.OracleClient
- // data provider in Mono.
- //
- // Part of managed C#/.NET library System.Data.OracleClient.dll
- //
- // Part of the Mono class libraries at
- // mcs/class/System.Data.OracleClient/System.Data.OracleClient.OCI
- //
- // Tests:
- // Assembly: System.Data.OracleClient.dll
- // Namespace: System.Data.OracleClient
- //
- // To Compile:
- // mcs TestOracleClient.cs /r:System.Data.dll /r:System.Data.OracleClient.dll /nowarn:0168
- //
- // Author:
- // Daniel Morgan <[email protected]>
- //
- // Copyright (C) Daniel Morgan, 2002, 2004-2005, 2008
- //
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Data;
- using System.Data.OracleClient;
- using System.Text;
- using System.Threading;
- namespace Test.OracleClient
- {
- public class OracleTest
- {
- private static Thread t = null;
- private static string conStr;
- public static readonly int MAX_CONNECTIONS = 30; // max connections default to 100, but I will set to 30.
- public OracleTest()
- {
- }
- static void MonoTest(OracleConnection con)
- {
- Console.WriteLine (" Drop table MONO_ORACLE_TEST ...");
- try {
- OracleCommand cmd2 = con.CreateCommand ();
- cmd2.CommandText = "DROP TABLE MONO_ORACLE_TEST";
- cmd2.ExecuteNonQuery ();
- }
- catch (OracleException) {
- // ignore if table already exists
- }
- OracleCommand cmd = null;
- Console.WriteLine(" Creating table MONO_ORACLE_TEST...");
- cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.CommandText = "CREATE TABLE MONO_ORACLE_TEST ( " +
- " varchar2_value VarChar2(32), " +
- " long_value long, " +
- " number_whole_value Number(18), " +
- " number_scaled_value Number(18,2), " +
- " number_integer_value Integer, " +
- " float_value Float, " +
- " date_value Date, " +
- " char_value Char(32), " +
- " clob_value Clob, " +
- " blob_value Blob, " +
- " clob_empty_value Clob, " +
- " blob_empty_value Blob, " +
- " varchar2_null_value VarChar2(32), " +
- " number_whole_null_value Number(18), " +
- " number_scaled_null_value Number(18,2), " +
- " number_integer_null_value Integer, " +
- " float_null_value Float, " +
- " date_null_value Date, " +
- " char_null_value Char(32), " +
- " clob_null_value Clob, " +
- " blob_null_value Blob " +
- ")";
- cmd.ExecuteNonQuery();
- Console.WriteLine(" Begin Trans for table MONO_ORACLE_TEST...");
- OracleTransaction trans = con.BeginTransaction ();
- Console.WriteLine(" Inserting value into MONO_ORACLE_TEST...");
- cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.Transaction = trans;
- cmd.CommandText = "INSERT INTO mono_oracle_test " +
- " ( varchar2_value, " +
- " long_value, " +
- " number_whole_value, " +
- " number_scaled_value, " +
- " number_integer_value, " +
- " float_value, " +
- " date_value, " +
- " char_value, " +
- " clob_value, " +
- " blob_value, " +
- " clob_empty_value, " +
- " blob_empty_value " +
- ") " +
- " VALUES( " +
- " 'Mono', " +
- " 'This is a LONG column', " +
- " 123, " +
- " 456.78, " +
- " 8765, " +
- " 235.2, " +
- " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
- " 'US', " +
- " EMPTY_CLOB(), " +
- " EMPTY_BLOB()," +
- " EMPTY_CLOB(), " +
- " EMPTY_BLOB()" +
- ")";
- cmd.ExecuteNonQuery();
- Console.WriteLine(" Select/Update CLOB columns on table MONO_ORACLE_TEST...");
- // update BLOB and CLOB columns
- OracleCommand select = con.CreateCommand ();
- select.Transaction = trans;
- select.CommandText = "SELECT CLOB_VALUE, BLOB_VALUE FROM MONO_ORACLE_TEST FOR UPDATE";
- OracleDataReader reader = select.ExecuteReader ();
- if (!reader.Read ())
- Console.WriteLine ("ERROR: RECORD NOT FOUND");
- // update clob_value
- Console.WriteLine(" Update CLOB column on table MONO_ORACLE_TEST...");
- OracleLob clob = reader.GetOracleLob (0);
- byte[] bytes = null;
- UnicodeEncoding encoding = new UnicodeEncoding ();
- bytes = encoding.GetBytes ("Mono is fun!");
- clob.Write (bytes, 0, bytes.Length);
- clob.Close ();
- // update blob_value
- Console.WriteLine(" Update BLOB column on table MONO_ORACLE_TEST...");
- OracleLob blob = reader.GetOracleLob (1);
- bytes = new byte[6] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x036 };
- blob.Write (bytes, 0, bytes.Length);
- blob.Close ();
-
- Console.WriteLine(" Commit trans for table MONO_ORACLE_TEST...");
- trans.Commit ();
- // OracleCommand.ExecuteReader of MONO_ORACLE_TEST table
- Console.WriteLine(" Read simple test for table MONO_ORACLE_TEST...");
- ReadSimpleTest(con, "SELECT * FROM MONO_ORACLE_TEST");
- // OracleCommand.ExecuteScalar
- Console.WriteLine(" -ExecuteScalar tests...");
- string varchar2_value = (string) ReadScalar (con,"SELECT MAX(varchar2_value) FROM MONO_ORACLE_TEST");
- Console.WriteLine(" String Value: " + varchar2_value);
- Console.WriteLine(" Read Scalar: number_whole_value");
- decimal number_whole_value = (decimal)
- ReadScalar (con,"SELECT MAX(number_whole_value) FROM MONO_ORACLE_TEST");
- Console.WriteLine(" Int32 Value: " + number_whole_value.ToString());
- Console.WriteLine(" Read Scalar: number_scaled_value");
- decimal number_scaled_value = (decimal)
- ReadScalar (con,"SELECT number_scaled_value FROM MONO_ORACLE_TEST");
- Console.WriteLine(" Decimal Value: " + number_scaled_value.ToString());
-
- Console.WriteLine(" Read Scalar: date_value");
- DateTime date_value = (DateTime)
- ReadScalar (con,"SELECT date_value FROM MONO_ORACLE_TEST");
- Console.WriteLine(" DateTime Value: " + date_value.ToString());
-
- Console.WriteLine(" Read Scalar: clob_value");
- string clob_value = (string)
- ReadScalar (con,"SELECT clob_value FROM MONO_ORACLE_TEST");
- Console.WriteLine(" CLOB Value: " + clob_value);
- Console.WriteLine(" Read Scalar: blob_value");
- byte[] blob_value = (byte[])
- ReadScalar (con,"SELECT blob_value FROM MONO_ORACLE_TEST");
- string sblob_value = GetHexString (blob_value);
- Console.WriteLine(" BLOB Value: " + sblob_value);
-
- // OracleCommand.ExecuteOracleScalar
- Console.WriteLine(" -ExecuteOracleScalar tests...");
- Console.WriteLine(" Read Oracle Scalar: varchar2_value");
- ReadOracleScalar (con,"SELECT MAX(varchar2_value) FROM MONO_ORACLE_TEST");
- Console.WriteLine(" Read Oracle Scalar: number_whole_value");
- ReadOracleScalar (con,"SELECT MAX(number_whole_value) FROM MONO_ORACLE_TEST");
- Console.WriteLine(" Read Oracle Scalar: number_scaled_value");
- ReadOracleScalar (con,"SELECT number_scaled_value FROM MONO_ORACLE_TEST");
-
- Console.WriteLine(" Read Oracle Scalar: date_value");
- ReadOracleScalar (con,"SELECT date_value FROM MONO_ORACLE_TEST");
-
- Console.WriteLine(" Read Oracle Scalar: clob_value");
- ReadOracleScalar (con,"SELECT clob_value FROM MONO_ORACLE_TEST");
- Console.WriteLine(" Read Oracle Scalar: blob_value");
- ReadOracleScalar (con,"SELECT blob_value FROM MONO_ORACLE_TEST");
- }
- static object ReadScalar (OracleConnection con, string selectSql)
- {
- OracleCommand cmd = null;
- cmd = con.CreateCommand();
- cmd.CommandText = selectSql;
- object o = cmd.ExecuteScalar ();
- string dataType = o.GetType ().ToString ();
- Console.WriteLine (" DataType: " + dataType);
- return o;
- }
- static void ReadOracleScalar (OracleConnection con, string selectSql)
- {
- OracleCommand cmd = null;
- cmd = con.CreateCommand();
- cmd.CommandText = selectSql;
- object o = cmd.ExecuteOracleScalar ();
- string dataType = o.GetType ().ToString ();
- Console.WriteLine (" DataType: " + dataType);
- if (dataType.Equals("System.Data.OracleClient.OracleLob"))
- o = ((OracleLob) o).Value;
- if (o.GetType ().ToString ().Equals ("System.Byte[]"))
- o = GetHexString ((byte[])o);
-
- Console.WriteLine (" Value: " + o.ToString ());
- }
- static void ReadSimpleTest(OracleConnection con, string selectSql)
- {
- OracleCommand cmd = null;
- OracleDataReader reader = null;
-
- cmd = con.CreateCommand();
- cmd.CommandText = selectSql;
- reader = cmd.ExecuteReader();
-
- Console.WriteLine(" Results...");
- Console.WriteLine(" Schema");
- DataTable table;
- table = reader.GetSchemaTable();
- for(int c = 0; c < reader.FieldCount; c++) {
- Console.WriteLine(" Column " + c.ToString());
- DataRow row = table.Rows[c];
-
- string strColumnName = row["ColumnName"].ToString();
- string strBaseColumnName = row["BaseColumnName"].ToString();
- string strColumnSize = row["ColumnSize"].ToString();
- string strNumericScale = row["NumericScale"].ToString();
- string strNumericPrecision = row["NumericPrecision"].ToString();
- string strDataType = row["DataType"].ToString();
- Console.WriteLine(" ColumnName: " + strColumnName);
- Console.WriteLine(" BaseColumnName: " + strBaseColumnName);
- Console.WriteLine(" ColumnSize: " + strColumnSize);
- Console.WriteLine(" NumericScale: " + strNumericScale);
- Console.WriteLine(" NumericPrecision: " + strNumericPrecision);
- Console.WriteLine(" DataType: " + strDataType);
- }
- int r = 0;
- Console.WriteLine (" Data");
- while (reader.Read ()) {
- r++;
- Console.WriteLine (" Row: " + r.ToString ());
- for (int f = 0; f < reader.FieldCount; f++) {
- string sname = "";
- object ovalue = "";
- string svalue = "";
- string sDataType = "";
- string sFieldType = "";
- string sDataTypeName = "";
- string sOraDataType = "";
- sname = reader.GetName (f);
- if (reader.IsDBNull (f)) {
- ovalue = DBNull.Value;
- svalue = "";
- sDataType = "DBNull.Value";
- sOraDataType = "DBNull.Value";
- }
- else {
- //ovalue = reader.GetValue (f);
- ovalue = reader.GetOracleValue (f);
- object oravalue = null;
-
- sDataType = ovalue.GetType ().ToString ();
- switch (sDataType) {
- case "System.Data.OracleClient.OracleString":
- oravalue = ((OracleString) ovalue).Value;
- break;
- case "System.Data.OracleClient.OracleNumber":
- oravalue = ((OracleNumber) ovalue).Value;
- break;
- case "System.Data.OracleClient.OracleLob":
- OracleLob lob = (OracleLob) ovalue;
- oravalue = lob.Value;
- lob.Close ();
- break;
- case "System.Data.OracleClient.OracleDateTime":
- oravalue = ((OracleDateTime) ovalue).Value;
- break;
- case "System.Byte[]":
- oravalue = GetHexString((byte[])ovalue);
- break;
- case "System.Decimal":
- //Console.WriteLine(" *** Get Decimal, Int16, Int32, Int64, Float, Double, ...");
- decimal dec = reader.GetDecimal (f);
- //Console.WriteLine(" GetDecimal: " + dec.ToString ());
- oravalue = (object) dec;
- /*
- try {
- reader.GetInt16 (f);
- } catch (NotSupportedException e) {
- Console.WriteLine (" ** Expected exception caught for GetInt16: NotSupportedException: " + e.Message);
- }
- try {
- long lng = reader.GetInt64 (f);
- Console.WriteLine(" GetInt64: " + lng.ToString ());
- int n = reader.GetInt32 (f);
- Console.WriteLine(" GetInt32: " + n.ToString ());
- float flt = reader.GetFloat (f);
- Console.WriteLine(" GetFloat: " + flt.ToString ());
- double dbl = reader.GetDouble (f);
- Console.WriteLine(" GetDouble: " + dbl.ToString ());
- } catch (OverflowException oe1) {
- Console.WriteLine (" ** Overflow exception for numbers to big or too small: " + oe1.Message);
- }
- */
- break;
- default:
- oravalue = ovalue.ToString ();
- break;
- }
-
- sOraDataType = oravalue.GetType ().ToString ();
- if (sOraDataType.Equals ("System.Byte[]"))
- svalue = GetHexString ((byte[]) oravalue);
- else
- svalue = oravalue.ToString();
-
- }
- sFieldType = reader.GetFieldType(f).ToString();
- sDataTypeName = reader.GetDataTypeName(f);
- Console.WriteLine(" Field: " + f.ToString());
- Console.WriteLine(" Name: " + sname);
- Console.WriteLine(" Value: " + svalue);
- Console.WriteLine(" Oracle Data Type: " + sOraDataType);
- Console.WriteLine(" Data Type: " + sDataType);
- Console.WriteLine(" Field Type: " + sFieldType);
- Console.WriteLine(" Data Type Name: " + sDataTypeName);
- }
- }
- if(r == 0)
- Console.WriteLine(" No data returned.");
- }
-
- static void DataAdapterTest (OracleConnection connection)
- {
- Console.WriteLine(" Create select command...");
- OracleCommand command = connection.CreateCommand ();
- command.CommandText = "SELECT * FROM SCOTT.EMP";
- Console.WriteLine(" Create data adapter...");
- OracleDataAdapter adapter = new OracleDataAdapter (command);
- Console.WriteLine(" Create DataSet...");
- DataSet dataSet = new DataSet ("EMP");
- Console.WriteLine(" Fill DataSet via data adapter...");
- adapter.Fill (dataSet);
- Console.WriteLine(" Get DataTable...");
- DataTable table = dataSet.Tables [0];
- Console.WriteLine(" Display each row...");
- int rowCount = 0;
- foreach (DataRow row in table.Rows) {
- Console.WriteLine (" row {0}", rowCount + 1);
- for (int i = 0; i < table.Columns.Count; i += 1) {
- Console.WriteLine (" {0}: {1}", table.Columns [i].ColumnName, row [i]);
- }
- Console.WriteLine ();
- rowCount += 1;
- }
- }
- public static void DataAdapterTest2 (OracleConnection con)
- {
- DataAdapterTest2_Setup (con);
- ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
-
- GetMetaData (con, "SELECT * FROM mono_adapter_test");
- DataAdapterTest2_Insert (con);
- ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
-
- DataAdapterTest2_Update (con);
- ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
- DataAdapterTest2_Delete (con);
- ReadSimpleTest (con, "SELECT * FROM mono_adapter_test");
- }
- public static void GetMetaData (OracleConnection con, string sql)
- {
- OracleCommand cmd = null;
- OracleDataReader rdr = null;
-
- cmd = con.CreateCommand();
- cmd.CommandText = sql;
- Console.WriteLine("Read Schema With KeyInfo");
- rdr = cmd.ExecuteReader(CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly);
-
- DataTable dt;
- dt = rdr.GetSchemaTable();
- foreach (DataRow schemaRow in dt.Rows) {
- foreach (DataColumn schemaCol in dt.Columns) {
- Console.WriteLine(schemaCol.ColumnName +
- " = " +
- schemaRow[schemaCol]);
- Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
- }
- Console.WriteLine("");
- }
- Console.WriteLine("Read Schema with No KeyInfo");
- rdr = cmd.ExecuteReader();
- dt = rdr.GetSchemaTable();
- foreach (DataRow schemaRow in dt.Rows) {
- foreach (DataColumn schemaCol in dt.Columns) {
- Console.WriteLine(schemaCol.ColumnName +
- " = " +
- schemaRow[schemaCol]);
- Console.WriteLine("---Type: " + schemaRow[schemaCol].GetType ().ToString());
- Console.WriteLine();
- }
- }
- }
- public static void DataAdapterTest2_Setup (OracleConnection con)
- {
- Console.WriteLine (" Drop table mono_adapter_test ...");
- try {
- OracleCommand cmd2 = con.CreateCommand ();
- cmd2.CommandText = "DROP TABLE mono_adapter_test";
- cmd2.ExecuteNonQuery ();
- }
- catch (OracleException) {
- // ignore if table already exists
- }
- OracleCommand cmd = null;
- Console.WriteLine(" Creating table mono_adapter_test...");
- cmd = new OracleCommand ();
- cmd.Connection = con;
- cmd.CommandText = "CREATE TABLE mono_adapter_test ( " +
- " varchar2_value VarChar2(32), " +
- " number_whole_value Number(18) PRIMARY KEY, " +
- " number_scaled_value Number(18,2), " +
- " number_integer_value Integer, " +
- " float_value Float, " +
- " date_value Date, " +
- " clob_value Clob, " +
- " blob_value Blob ) ";
-
- // FIXME: char_value does not work
- /*
- cmd.CommandText = "CREATE TABLE mono_adapter_test ( " +
- " varchar2_value VarChar2(32), " +
- " number_whole_value Number(18) PRIMARY KEY, " +
- " number_scaled_value Number(18,2), " +
- " number_integer_value Integer, " +
- " float_value Float, " +
- " date_value Date, " +
- " char_value Char(32), " +
- " clob_value Clob, " +
- " blob_value Blob ) ";
- */
- cmd.ExecuteNonQuery();
- Console.WriteLine(" Begin Trans for table mono_adapter_test...");
- OracleTransaction trans = con.BeginTransaction ();
- Console.WriteLine(" Inserting value into mono_adapter_test...");
- cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.Transaction = trans;
-
- cmd.CommandText = "INSERT INTO mono_adapter_test " +
- " ( varchar2_value, " +
- " number_whole_value, " +
- " number_scaled_value, " +
- " number_integer_value, " +
- " float_value, " +
- " date_value, " +
- " clob_value, " +
- " blob_value " +
- ") " +
- " VALUES( " +
- " 'Mono', " +
- " 11, " +
- " 456.78, " +
- " 8765, " +
- " 235.2, " +
- " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
- " EMPTY_CLOB(), " +
- " EMPTY_BLOB() " +
- ")";
- /*
- cmd.CommandText = "INSERT INTO mono_adapter_test " +
- " ( varchar2_value, " +
- " number_whole_value, " +
- " number_scaled_value, " +
- " number_integer_value, " +
- " float_value, " +
- " date_value, " +
- " char_value, " +
- " clob_value, " +
- " blob_value " +
- ") " +
- " VALUES( " +
- " 'Mono', " +
- " 11, " +
- " 456.78, " +
- " 8765, " +
- " 235.2, " +
- " TO_DATE( '2004-12-31', 'YYYY-MM-DD' ), " +
- " 'US', " +
- " EMPTY_CLOB(), " +
- " EMPTY_BLOB() " +
- ")";
- */
- cmd.ExecuteNonQuery();
- Console.WriteLine(" Select/Update CLOB columns on table mono_adapter_test...");
-
- // update BLOB and CLOB columns
- OracleCommand select = con.CreateCommand ();
- select.Transaction = trans;
- select.CommandText = "SELECT CLOB_VALUE, BLOB_VALUE FROM mono_adapter_test FOR UPDATE";
- OracleDataReader reader = select.ExecuteReader ();
- if (!reader.Read ())
- Console.WriteLine ("ERROR: RECORD NOT FOUND");
-
- // update clob_value
- Console.WriteLine(" Update CLOB column on table mono_adapter_test...");
- OracleLob clob = reader.GetOracleLob (0);
- byte[] bytes = null;
- UnicodeEncoding encoding = new UnicodeEncoding ();
- bytes = encoding.GetBytes ("Mono is fun!");
- clob.Write (bytes, 0, bytes.Length);
- clob.Close ();
-
- // update blob_value
- Console.WriteLine(" Update BLOB column on table mono_adapter_test...");
- OracleLob blob = reader.GetOracleLob (1);
- bytes = new byte[6] { 0x31, 0x32, 0x33, 0x34, 0x35, 0x036 };
- blob.Write (bytes, 0, bytes.Length);
- blob.Close ();
-
- Console.WriteLine(" Commit trans for table mono_adapter_test...");
- trans.Commit ();
- CommitCursor (con);
- }
- public static void DataAdapterTest2_Insert (OracleConnection con)
- {
- Console.WriteLine("================================");
- Console.WriteLine("=== Adapter Insert =============");
- Console.WriteLine("================================");
- OracleTransaction transaction = con.BeginTransaction ();
-
- Console.WriteLine(" Create adapter...");
- OracleDataAdapter da = new OracleDataAdapter("select * from mono_adapter_test", con);
- da.SelectCommand.Transaction = transaction;
-
- Console.WriteLine(" Create command builder...");
- OracleCommandBuilder mycb = new OracleCommandBuilder(da);
- Console.WriteLine(" Create data set ...");
- DataSet ds = new DataSet();
-
- Console.WriteLine(" Fill data set via adapter...");
- da.Fill(ds, "mono_adapter_test");
- Console.WriteLine(" New Row...");
- DataRow myRow;
- myRow = ds.Tables["mono_adapter_test"].NewRow();
- byte[] bytes = new byte[] { 0x45,0x46,0x47,0x48,0x49,0x50 };
- Console.WriteLine(" Set values in the new DataRow...");
- myRow["varchar2_value"] = "OracleClient";
- myRow["number_whole_value"] = 22;
- myRow["number_scaled_value"] = 12.34;
- myRow["number_integer_value"] = 456;
- myRow["float_value"] = 98.76;
- myRow["date_value"] = new DateTime(2001,07,09);
- Console.WriteLine(" *** FIXME; char value not working");
- //myRow["char_value"] = "Romeo";
- myRow["clob_value"] = "clobtest";
- myRow["blob_value"] = bytes;
-
- Console.WriteLine(" Add DataRow to DataTable...");
- ds.Tables["mono_adapter_test"].Rows.Add(myRow);
- Console.WriteLine("da.Update(ds...");
- da.Update(ds, "mono_adapter_test");
- transaction.Commit();
- mycb.Dispose();
- mycb = null;
- }
- public static void DataAdapterTest2_Update (OracleConnection con)
- {
- Console.WriteLine("================================");
- Console.WriteLine("=== Adapter Update =============");
- Console.WriteLine("================================");
- OracleTransaction transaction = con.BeginTransaction ();
- Console.WriteLine(" Create adapter...");
- OracleCommand selectCmd = con.CreateCommand ();
- selectCmd.Transaction = transaction;
- selectCmd.CommandText = "SELECT * FROM mono_adapter_test";
- OracleDataAdapter da = new OracleDataAdapter(selectCmd);
- Console.WriteLine(" Create command builder...");
- OracleCommandBuilder mycb = new OracleCommandBuilder(da);
- Console.WriteLine(" Create data set ...");
- DataSet ds = new DataSet();
- Console.WriteLine(" Set missing schema action...");
-
- Console.WriteLine(" Fill data set via adapter...");
- da.Fill(ds, "mono_adapter_test");
- DataRow myRow;
- Console.WriteLine(" New Row...");
- myRow = ds.Tables["mono_adapter_test"].Rows[0];
- Console.WriteLine("Tables Count: " + ds.Tables.Count.ToString());
- DataTable table = ds.Tables["mono_adapter_test"];
- DataRowCollection rows;
- rows = table.Rows;
- Console.WriteLine(" Row Count: " + rows.Count.ToString());
- myRow = rows[0];
- byte[] bytes = new byte[] { 0x62,0x63,0x64,0x65,0x66,0x67 };
- Console.WriteLine(" Set values in the new DataRow...");
- myRow["varchar2_value"] = "Super Power!";
-
- myRow["number_scaled_value"] = 12.35;
- myRow["number_integer_value"] = 457;
- myRow["float_value"] = 198.76;
- myRow["date_value"] = new DateTime(2002,08,09);
- //myRow["char_value"] = "Juliet";
- myRow["clob_value"] = "this is a clob";
- myRow["blob_value"] = bytes;
- Console.WriteLine("da.Update(ds...");
- da.Update(ds, "mono_adapter_test");
- transaction.Commit();
- mycb.Dispose();
- mycb = null;
- }
- public static void DataAdapterTest2_Delete (OracleConnection con)
- {
- Console.WriteLine("================================");
- Console.WriteLine("=== Adapter Delete =============");
- Console.WriteLine("================================");
- OracleTransaction transaction = con.BeginTransaction ();
-
- Console.WriteLine(" Create adapter...");
- OracleDataAdapter da = new OracleDataAdapter("SELECT * FROM mono_adapter_test", con);
- Console.WriteLine(" Create command builder...");
- OracleCommandBuilder mycb = new OracleCommandBuilder(da);
- Console.WriteLine(" set transr...");
- da.SelectCommand.Transaction = transaction;
- Console.WriteLine(" Create data set ...");
- DataSet ds = new DataSet();
-
- Console.WriteLine("Fill data set via adapter...");
- da.Fill(ds, "mono_adapter_test");
- Console.WriteLine("delete row...");
- ds.Tables["mono_adapter_test"].Rows[0].Delete();
- Console.WriteLine("da.Update(table...");
- da.Update(ds, "mono_adapter_test");
- Console.WriteLine("Commit...");
- transaction.Commit();
- mycb.Dispose();
- mycb = null;
- }
- static void TestNonQueryUsingExecuteReader(OracleConnection con)
- {
- OracleDataReader reader = null;
- OracleTransaction trans = null;
- Console.WriteLine(" drop table mono_adapter_test...");
- OracleCommand cmd = con.CreateCommand();
- cmd.CommandText = "DROP TABLE MONO_ADAPTER_TEST";
- trans = con.BeginTransaction();
- cmd.Transaction = trans;
- try {
- reader = cmd.ExecuteReader();
- Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
- reader.Read();
- Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
- reader.Close();
- Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
- trans.Commit();
- }
- catch(OracleException e) {
- Console.WriteLine(" OracleException caught: " + e.Message);
- trans.Commit();
- }
- Console.WriteLine(" Create table mono_adapter_test...");
- cmd.CommandText = "CREATE TABLE MONO_ADAPTER_TEST ( " +
- " varchar2_value VarChar2(32), " +
- " number_whole_value Number(18,0) PRIMARY KEY ) ";
- trans = con.BeginTransaction();
- cmd.Transaction = trans;
- reader = cmd.ExecuteReader();
- Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
- reader.Read();
- Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
- reader.Close();
- Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
- trans.Commit();
- Console.WriteLine("Insert into table mono_adapter_test...");
-
- string sql =
- "INSERT INTO MONO_ADAPTER_TEST " +
- "(VARCHAR2_VALUE,NUMBER_WHOLE_VALUE) " +
- "VALUES(:p1,:p2)";
- OracleCommand cmd2 = con.CreateCommand();
- trans = con.BeginTransaction();
- cmd2.Transaction = trans;
- cmd2.CommandText = sql;
-
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.VarChar, 32);
- myParameter1.Direction = ParameterDirection.Input;
-
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Number);
- myParameter2.Direction = ParameterDirection.Input;
- myParameter2.Value = 182;
- myParameter1.Value = "Mono";
- cmd2.Parameters.Add (myParameter1);
- cmd2.Parameters.Add (myParameter2);
-
- // insert 1 record
- reader = cmd2.ExecuteReader();
- Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
- reader.Read();
- Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
- reader.Close();
- Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
- // insert another record
- Console.WriteLine(" Insert another record...");
- myParameter2.Value = 183;
- myParameter1.Value = "Oracle";
- reader = cmd2.ExecuteReader();
- Console.WriteLine(" RowsAffected before read: " + reader.RecordsAffected.ToString());
- reader.Read();
- Console.WriteLine(" RowsAffected after read: " + reader.RecordsAffected.ToString());
- reader.Close();
- Console.WriteLine(" RowsAffected after close: " + reader.RecordsAffected.ToString());
- trans.Commit();
- trans = null;
-
- ReadSimpleTest(con, "SELECT * FROM MONO_ADAPTER_TEST");
- }
- static void CommitCursor (OracleConnection con)
- {
- OracleCommand cmd = con.CreateCommand ();
- cmd.CommandText = "COMMIT";
- cmd.ExecuteNonQuery ();
- cmd.Dispose ();
- cmd = null;
- }
- static void RollbackTest (OracleConnection connection)
- {
- OracleTransaction transaction = connection.BeginTransaction ();
- OracleCommand insert = connection.CreateCommand ();
- insert.Transaction = transaction;
- insert.CommandText = "INSERT INTO SCOTT.EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
- Console.WriteLine (" Inserting record ...");
- insert.ExecuteNonQuery ();
- OracleCommand select = connection.CreateCommand ();
- select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
- select.Transaction = transaction;
- OracleDataReader reader = select.ExecuteReader ();
- reader.Read ();
- Console.WriteLine (" Row count SHOULD BE 1, VALUE IS {0}", reader.GetValue (0));
- reader.Close ();
- Console.WriteLine (" Rolling back transaction ...");
- transaction.Rollback ();
- select = connection.CreateCommand ();
- select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
- reader = select.ExecuteReader ();
- reader.Read ();
- Console.WriteLine (" Row count SHOULD BE 0, VALUE IS {0}", reader.GetValue (0));
- reader.Close ();
- }
-
- static void CommitTest (OracleConnection connection)
- {
- OracleTransaction transaction = connection.BeginTransaction ();
- OracleCommand insert = connection.CreateCommand ();
- insert.Transaction = transaction;
- insert.CommandText = "INSERT INTO SCOTT.EMP (EMPNO, ENAME, JOB) VALUES (8787, 'T Coleman', 'Monoist')";
- Console.WriteLine (" Inserting record ...");
- insert.ExecuteNonQuery ();
- OracleCommand select = connection.CreateCommand ();
- select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
- select.Transaction = transaction;
- Console.WriteLine (" Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
- Console.WriteLine (" Committing transaction ...");
- transaction.Commit ();
- select = connection.CreateCommand ();
- select.CommandText = "SELECT COUNT(*) FROM SCOTT.EMP WHERE EMPNO = 8787";
- Console.WriteLine ("Row count SHOULD BE 1, VALUE IS {0}", select.ExecuteScalar ());
- transaction = connection.BeginTransaction ();
- OracleCommand delete = connection.CreateCommand ();
- delete.Transaction = transaction;
- delete.CommandText = "DELETE FROM SCOTT.EMP WHERE EMPNO = 8787";
- delete.ExecuteNonQuery ();
- transaction.Commit ();
- }
- public static void ParameterTest2 (OracleConnection connection)
- {
- Console.WriteLine(" Setting NLS_DATE_FORMAT...");
- OracleCommand cmd2 = connection.CreateCommand();
- cmd2.CommandText = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
-
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
- try {
- cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE7";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine(" Create table MONO_TEST_TABLE7...");
- cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE7(" +
- " COL1 VARCHAR2(8) NOT NULL, " +
- " COL2 VARCHAR2(32), " +
- " COL3 NUMBER(18,2), " +
- " COL4 NUMBER(18,2), " +
- " COL5 DATE NOT NULL, " +
- " COL6 DATE, " +
- " COL7 BLOB NOT NULL, " +
- " COL8 BLOB, " +
- " COL9 CLOB NOT NULL, " +
- " COL10 CLOB " +
- ")";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" create insert command...");
- OracleTransaction trans = connection.BeginTransaction ();
- OracleCommand cmd = connection.CreateCommand ();
- cmd.Transaction = trans;
- cmd.CommandText = "INSERT INTO MONO_TEST_TABLE7 " +
- "(COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10) " +
- "VALUES(:P1,:P2,:P3,:P4,:P5,:P6,:P7,:P8,:P9,:P10)";
-
- Console.WriteLine(" Add parameters...");
- OracleParameter parm1 = cmd.Parameters.Add (":P1", OracleType.VarChar, 8);
- OracleParameter parm2 = cmd.Parameters.Add (":P2", OracleType.VarChar, 32);
-
- OracleParameter parm3 = cmd.Parameters.Add (":P3", OracleType.Number);
- OracleParameter parm4 = cmd.Parameters.Add (":P4", OracleType.Number);
-
- OracleParameter parm5 = cmd.Parameters.Add (":P5", OracleType.DateTime);
- OracleParameter parm6 = cmd.Parameters.Add (":P6", OracleType.DateTime);
- // FIXME: fix BLOBs and CLOBs in OracleParameter
- OracleParameter parm7 = cmd.Parameters.Add (":P7", OracleType.Blob);
- OracleParameter parm8 = cmd.Parameters.Add (":P8", OracleType.Blob);
- OracleParameter parm9 = cmd.Parameters.Add (":P9", OracleType.Clob);
- OracleParameter parm10 = cmd.Parameters.Add (":P10", OracleType.Clob);
- // TODO: implement out, return, and ref parameters
- string s = "Mono";
- decimal d = 123456789012345.678M;
- DateTime dt = DateTime.Now;
- string clob = "Clob";
- byte[] blob = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
-
- Console.WriteLine(" Set Values...");
- parm1.Value = s;
- parm2.Value = DBNull.Value;
-
- parm3.Value = d;
- parm4.Value = DBNull.Value;
-
- parm5.Value = dt;
- parm6.Value = DBNull.Value;
-
- parm7.Value = blob;
- parm8.Value = DBNull.Value;
- parm9.Value = clob;
- parm10.Value = DBNull.Value;
-
- Console.WriteLine(" ExecuteNonQuery...");
- cmd.ExecuteNonQuery ();
- trans.Commit();
- }
- public static void ParameterTest (OracleConnection connection)
- {
- Console.WriteLine(" Setting NLS_DATE_FORMAT...");
- OracleCommand cmd2 = connection.CreateCommand();
- cmd2.CommandText = "ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'";
-
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
- try {
- cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE7";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine(" Create table MONO_TEST_TABLE7...");
- cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE7(" +
- " COL1 VARCHAR2(8) NOT NULL, " +
- " COL2 VARCHAR2(32), " +
- " COL3 NUMBER(18,2) NOT NULL, " +
- " COL4 NUMBER(18,2), " +
- " COL5 DATE NOT NULL, " +
- " COL6 DATE, " +
- " COL7 BLOB NOT NULL, " +
- " COL8 BLOB, " +
- " COL9 CLOB NOT NULL, " +
- " COL10 CLOB " +
- ")";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" create insert command...");
- OracleTransaction trans = connection.BeginTransaction ();
- OracleCommand cmd = connection.CreateCommand ();
- cmd.Transaction = trans;
- cmd.CommandText = "INSERT INTO MONO_TEST_TABLE7 " +
- "(COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10) " +
- "VALUES(:P1,:P2,:P3,:P4,:P5,:P6,:P7,:P8,:P9,:P10)";
-
- Console.WriteLine(" Add parameters...");
- OracleParameter parm1 = cmd.Parameters.Add (":P1", OracleType.VarChar, 8);
- OracleParameter parm2 = cmd.Parameters.Add (":P2", OracleType.VarChar, 32);
-
- OracleParameter parm3 = cmd.Parameters.Add (":P3", OracleType.Number);
- OracleParameter parm4 = cmd.Parameters.Add (":P4", OracleType.Number);
-
- OracleParameter parm5 = cmd.Parameters.Add (":P5", OracleType.DateTime);
- OracleParameter parm6 = cmd.Parameters.Add (":P6", OracleType.DateTime);
- // FIXME: fix BLOBs and CLOBs in OracleParameter
- OracleParameter parm7 = cmd.Parameters.Add (":P7", OracleType.Blob);
- OracleParameter parm8 = cmd.Parameters.Add (":P8", OracleType.Blob);
- OracleParameter parm9 = cmd.Parameters.Add (":P9", OracleType.Clob);
- OracleParameter parm10 = cmd.Parameters.Add (":P10", OracleType.Clob);
- // TODO: implement out, return, and ref parameters
- string s = "Mono";
- decimal d = 123456789012345.678M;
- DateTime dt = DateTime.Now;
- string clob = "Clob";
- byte[] blob = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
-
- Console.WriteLine(" Set Values...");
- parm1.Value = s;
- parm2.Value = DBNull.Value;
-
- parm3.Value = d;
- parm4.Value = DBNull.Value;
-
- parm5.Value = dt;
- parm6.Value = DBNull.Value;
-
- parm7.Value = blob;
- parm8.Value = DBNull.Value;
- parm9.Value = clob;
- parm10.Value = DBNull.Value;
-
- Console.WriteLine(" ExecuteNonQuery...");
- cmd.ExecuteNonQuery ();
- trans.Commit();
- }
- public static void CLOBTest (OracleConnection connection)
- {
- Console.WriteLine (" BEGIN TRANSACTION ...");
- OracleTransaction transaction = connection.BeginTransaction ();
- Console.WriteLine (" Drop table CLOBTEST ...");
- try {
- OracleCommand cmd2 = connection.CreateCommand ();
- cmd2.Transaction = transaction;
- cmd2.CommandText = "DROP TABLE CLOBTEST";
- cmd2.ExecuteNonQuery ();
- }
- catch (OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine (" CREATE TABLE ...");
- OracleCommand create = connection.CreateCommand ();
- create.Transaction = transaction;
- create.CommandText = "CREATE TABLE CLOBTEST (CLOB_COLUMN CLOB)";
- create.ExecuteNonQuery ();
- Console.WriteLine (" INSERT RECORD ...");
- OracleCommand insert = connection.CreateCommand ();
- insert.Transaction = transaction;
- insert.CommandText = "INSERT INTO CLOBTEST VALUES (EMPTY_CLOB())";
- insert.ExecuteNonQuery ();
- OracleCommand select = connection.CreateCommand ();
- select.Transaction = transaction;
- select.CommandText = "SELECT CLOB_COLUMN FROM CLOBTEST FOR UPDATE";
- Console.WriteLine (" SELECTING A CLOB (CHARACTER) VALUE FROM CLOBTEST");
- OracleDataReader reader = select.ExecuteReader ();
- if (!reader.Read ())
- Console.WriteLine ("ERROR: RECORD NOT FOUND");
- Console.WriteLine (" TESTING OracleLob OBJECT ...");
- OracleLob lob = reader.GetOracleLob (0);
- Console.WriteLine (" LENGTH: {0}", lob.Length);
- Console.WriteLine (" CHUNK SIZE: {0}", lob.ChunkSize);
- UnicodeEncoding encoding = new UnicodeEncoding ();
- byte[] value = new byte [lob.Length * 2];
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- Console.WriteLine (" UPDATING VALUE TO 'TEST ME!'");
- value = encoding.GetBytes ("TEST ME!");
- lob.Write (value, 0, value.Length);
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- Console.WriteLine (" RE-READ VALUE...");
- lob.Seek (1, SeekOrigin.Begin);
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- value = new byte [lob.Length * 2];
- lob.Read (value, 0, value.Length);
- Console.WriteLine (" VALUE: {0}", encoding.GetString (value));
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- Console.WriteLine (" CLOSE OracleLob...");
- lob.Close ();
- Console.WriteLine (" CLOSING READER...");
-
- reader.Close ();
- transaction.Commit ();
- }
- public static void BLOBTest (OracleConnection connection)
- {
- Console.WriteLine (" BEGIN TRANSACTION ...");
- OracleTransaction transaction = connection.BeginTransaction ();
- Console.WriteLine (" Drop table BLOBTEST ...");
- try {
- OracleCommand cmd2 = connection.CreateCommand ();
- cmd2.Transaction = transaction;
- cmd2.CommandText = "DROP TABLE BLOBTEST";
- cmd2.ExecuteNonQuery ();
- }
- catch (OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine (" CREATE TABLE ...");
- OracleCommand create = connection.CreateCommand ();
- create.Transaction = transaction;
- create.CommandText = "CREATE TABLE BLOBTEST (BLOB_COLUMN BLOB)";
- create.ExecuteNonQuery ();
- Console.WriteLine (" INSERT RECORD ...");
- OracleCommand insert = connection.CreateCommand ();
- insert.Transaction = transaction;
- insert.CommandText = "INSERT INTO BLOBTEST VALUES (EMPTY_BLOB())";
- insert.ExecuteNonQuery ();
- OracleCommand select = connection.CreateCommand ();
- select.Transaction = transaction;
- select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST FOR UPDATE";
- Console.WriteLine (" SELECTING A BLOB (Binary) VALUE FROM BLOBTEST");
- OracleDataReader reader = select.ExecuteReader ();
- if (!reader.Read ())
- Console.WriteLine ("ERROR: RECORD NOT FOUND");
- Console.WriteLine (" TESTING OracleLob OBJECT ...");
- OracleLob lob = reader.GetOracleLob (0);
-
- byte[] value = null;
- string bvalue = "";
- Console.WriteLine (" UPDATING VALUE");
- byte[] bytes = new byte[6];
- bytes[0] = 0x31;
- bytes[1] = 0x32;
- bytes[2] = 0x33;
- bytes[3] = 0x34;
- bytes[4] = 0x35;
- bytes[5] = 0x36;
- lob.Write (bytes, 0, bytes.Length);
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- Console.WriteLine (" RE-READ VALUE...");
- lob.Seek (1, SeekOrigin.Begin);
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- value = new byte [lob.Length];
- lob.Read (value, 0, value.Length);
-
- bvalue = "";
- if (value.GetType ().ToString ().Equals ("System.Byte[]"))
- bvalue = GetHexString (value);
- Console.WriteLine (" Bytes: " + bvalue);
- Console.WriteLine (" CURRENT POSITION: {0}", lob.Position);
- Console.WriteLine (" CLOSE OracleLob...");
- lob.Close ();
- Console.WriteLine (" CLOSING READER...");
-
- reader.Close ();
- transaction.Commit ();
- }
- static void Wait(string msg)
- {
- Console.WriteLine(msg);
- if (msg.Equals(""))
- Console.WriteLine("Waiting... Press Enter to continue...");
- Console.ReadLine();
- }
- // use this function to read a byte array into a string
- // for easy display of binary data, such as, a BLOB value
- public static string GetHexString (byte[] bytes)
- {
- string bvalue = "";
-
- StringBuilder sb2 = new StringBuilder();
- for (int z = 0; z < bytes.Length; z++) {
- byte byt = bytes[z];
- if (byt < 0x10)
- sb2.Append ("0");
- sb2.Append (byt.ToString("x"));
- }
- if (sb2.Length > 0)
- bvalue = "0x" + sb2.ToString ();
-
- return bvalue;
- }
- static void StoredProcedureTest1 (OracleConnection con)
- {
- // test stored procedure with no parameters
-
-
- OracleCommand cmd2 = con.CreateCommand ();
- Console.WriteLine(" Drop table MONO_TEST_TABLE1...");
- try {
- cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE1";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if table did not exist
- }
- Console.WriteLine(" Drop procedure SP_TEST1...");
- try {
- cmd2.CommandText = "DROP PROCEDURE SP_TEST1";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if procedure did not exist
- }
- Console.WriteLine(" Create table MONO_TEST_TABLE1...");
- cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE1 (" +
- " COL1 VARCHAR2(8), "+
- " COL2 VARCHAR2(32))";
- cmd2.ExecuteNonQuery ();
-
- Console.WriteLine(" Create stored procedure SP_TEST1...");
- cmd2.CommandText = "CREATE PROCEDURE SP_TEST1 " +
- " IS " +
- " BEGIN " +
- " INSERT INTO MONO_TEST_TABLE1 (COL1,COL2) VALUES ('aaa','bbbb');" +
- " COMMIT;" +
- " END;";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine("COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" Call stored procedure sp_test1...");
- OracleCommand cmd3 = con.CreateCommand ();
- cmd3.CommandType = CommandType.StoredProcedure;
- cmd3.CommandText = "sp_test1";
- cmd3.ExecuteNonQuery ();
- }
- static void StoredProcedureTest2 (OracleConnection con)
- {
- // test stored procedure with 2 parameters
- Console.WriteLine(" Drop table MONO_TEST_TABLE2...");
- OracleCommand cmd2 = con.CreateCommand ();
- try {
- cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE2";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine(" Drop procedure SP_TEST2...");
- try {
- cmd2.CommandText = "DROP PROCEDURE SP_TEST2";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if procedure does not exists
- }
- Console.WriteLine(" Create table MONO_TEST_TABLE2...");
-
- cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE2 (" +
- " COL1 VARCHAR2(8), "+
- " COL2 VARCHAR2(32))";
- cmd2.ExecuteNonQuery ();
-
- Console.WriteLine(" Create stored procedure SP_TEST2...");
- cmd2.CommandText = "CREATE PROCEDURE SP_TEST2(parm1 VARCHAR2,parm2 VARCHAR2) " +
- " IS " +
- " BEGIN " +
- " INSERT INTO MONO_TEST_TABLE2 (COL1,COL2) VALUES (parm1,parm2);" +
- " COMMIT;" +
- " END;";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" Call stored procedure SP_TEST2 with two parameters...");
- OracleCommand cmd3 = con.CreateCommand ();
- cmd3.CommandType = CommandType.StoredProcedure;
- cmd3.CommandText = "sp_test2";
- OracleParameter myParameter1 = new OracleParameter("parm1", OracleType.VarChar);
- myParameter1.Value = "yyy13";
- myParameter1.Size = 8;
- myParameter1.Direction = ParameterDirection.Input;
-
- OracleParameter myParameter2 = new OracleParameter("parm2", OracleType.VarChar);
- myParameter2.Value = "iii13";
- myParameter2.Size = 32;
- myParameter2.Direction = ParameterDirection.Input;
- cmd3.Parameters.Add (myParameter1);
- cmd3.Parameters.Add (myParameter2);
- cmd3.ExecuteNonQuery ();
- }
- static void OutParmTest1(OracleConnection con)
- {
- // test stored fuctions with 4 parameters
- // 1. input varchar2
- // 2. output varchar2
- // 3. input output varchar2
- // 4. return varchar2
- Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST1 for testing VARCHAR2 Input, Output, InputOutput, Return parameters...");
-
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM1(parm1 IN VARCHAR2, parm2 OUT VARCHAR2, parm3 IN OUT VARCHAR2) RETURN VARCHAR2 " +
- "IS " +
- " returnValue VARCHAR2(32) := 'Anywhere';" +
- "BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := 'parm1 is null'; " +
- " ELSE " +
- " parm2 := 'One' || parm1 || 'Three'; " +
- " END IF; " +
- " IF parm3 IS NOT NULL THEN " +
- " parm3 := parm2 || parm3 || 'Five'; " +
- " ELSE " +
- " parm3 := 'parm3 in was NULL'; " +
- " END IF; " +
- " IF parm1 IS NOT NULL THEN " +
- " IF parm1 = '999' THEN " +
- " parm2 := NULL; " +
- " parm3 := NULL; " +
- " returnValue := NULL; " +
- " END IF; " +
- " END IF; " +
- " RETURN returnValue; " +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored function SF_TESTOUTPARM1 with 4 parameters...");
- OracleCommand cmd3 = con.CreateCommand();
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "BEGIN " +
- " :ReturnValue := SF_TESTOUTPARM1(:p1, :p2, :p3); " +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.VarChar);
- myParameter1.Value = "Two";
- myParameter1.Size = 32;
- myParameter1.Direction = ParameterDirection.Input;
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.VarChar);
- myParameter2.Size = 32;
- myParameter2.Direction = ParameterDirection.Output;
- OracleParameter myParameter3 = new OracleParameter("p3", OracleType.VarChar);
- myParameter3.Value = "Four";
- myParameter3.Size = 32;
- myParameter3.Direction = ParameterDirection.InputOutput;
- OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.VarChar);
- myParameter4.Size = 32;
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameter3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- string outValue = (string)myParameter2.Value;
- string inOutValue = (string)myParameter3.Value;
- string returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 1Out Value should be: OneTwoThree");
- Console.WriteLine(" 1Out Value: " + outValue);
- Console.WriteLine(" 1InOut Value should be: OneTwoThreeFourFive");
- Console.WriteLine(" 1InOut Value: " + inOutValue);
- Console.WriteLine(" 1Return Value should be: Anywhere");
- Console.WriteLine(" 1Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameter3.Value = "Hello";
- cmd3.ExecuteNonQuery();
- outValue = (string)myParameter2.Value;
- inOutValue = (string)myParameter3.Value;
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 2Out Value should be: parm1 is null");
- Console.WriteLine(" 2Out Value: " + outValue);
- Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
- Console.WriteLine(" 2InOut Value: " + inOutValue);
- Console.WriteLine(" 2Return Value should be: Anywhere");
- Console.WriteLine(" 2Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "999";
- myParameter3.Value = "Bye";
- cmd3.ExecuteNonQuery();
- if (myParameter2.Value == DBNull.Value)
- outValue = "Value is DBNull.Value";
- else
- outValue = (string)myParameter2.Value;
- if( myParameter3.Value == DBNull.Value)
- inOutValue = "Value is DBNull.Value";
- else
- inOutValue = (string)myParameter3.Value;
- if (myParameter4.Value == DBNull.Value)
- returnValue = "Value is DBNull.Value";
- else
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Out Value: " + outValue);
- Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3InOut Value: " + inOutValue);
- Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "***";
- myParameter3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = (string)myParameter2.Value;
- inOutValue = (string)myParameter3.Value;
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 4Out Value should be: One***Three");
- Console.WriteLine(" 4Out Value: " + outValue);
- Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
- Console.WriteLine(" 4InOut Value: " + inOutValue);
- Console.WriteLine(" 4Return Value should be: Anywhere");
- Console.WriteLine(" 4Return Value: " + returnValue);
- Console.WriteLine();
- }
- static void OutParmTest2 (OracleConnection con)
- {
- // test stored function with 4 parameters
- // 1. input number(18,2)
- // 2. output number(18,2)
- // 3. input output number (18,2)
- // 4. return number (18,2)
- Console.WriteLine(" Create stored function SF_TESTOUTPARM2 to test NUMBER parameters...");
- // stored procedure addes two numbers
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM2(parm1 IN NUMBER, parm2 OUT NUMBER, parm3 IN OUT NUMBER) RETURN NUMBER " +
- "IS " +
- " returnValue NUMBER := 123.45; " +
- "BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := 18; " +
- " parm3 := parm3 + 8000; " +
- " returnValue := 78; " +
- " ELSIF parm1 = 999 THEN " +
- " parm2 := NULL;" +
- " parm3 := NULL;" +
- " returnValue := NULL;" +
- " ELSIF parm3 IS NULL THEN " +
- " parm2 := 0; " +
- " parm3 := 1234567890123.12345678; " +
- " ELSE " +
- " parm2 := parm1 + 3; " +
- " parm3 := parm3 + 70; " +
- " END IF;" +
- " RETURN returnValue;" +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored function SP_TESTOUTPARM2 with 4 parameters...");
- OracleCommand cmd3 = con.CreateCommand();
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "BEGIN " +
- " :returnValue := SF_TESTOUTPARM2(:p1, :p2, :p3);" +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Number);
- myParameter1.Value = 2.2;
- myParameter1.Direction = ParameterDirection.Input;
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Number);
- myParameter2.Direction = ParameterDirection.Output;
- OracleParameter myParameter3 = new OracleParameter("p3", OracleType.Number);
- myParameter3.Value = 33.4;
- myParameter3.Direction = ParameterDirection.InputOutput;
- OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.Number);
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameter3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- decimal outValue = (decimal)myParameter2.Value;
- decimal inOutValue = (decimal)myParameter3.Value;
- decimal returnValue = (decimal)myParameter4.Value;
- Console.WriteLine(" 1Out Value should be: 5.20");
- Console.WriteLine(" 1Out Value: {0}", outValue);
- Console.WriteLine(" 1InOut Value should be: 103.40");
- Console.WriteLine(" 1InOut Value: {0}", inOutValue);
- Console.WriteLine(" 1Return Value should be: 123.45");
- Console.WriteLine(" 1Return Value: {0}", returnValue);
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameter3.Value = 23;
- cmd3.ExecuteNonQuery();
- outValue = (decimal)myParameter2.Value;
- inOutValue = (decimal)myParameter3.Value;
- returnValue = (decimal)myParameter4.Value;
- Console.WriteLine(" 2Out Value should be: 18");
- Console.WriteLine(" 2Out Value: {0}", outValue);
- Console.WriteLine(" 2InOut Value should be: 8023");
- Console.WriteLine(" 2InOut Value: {0}", inOutValue);
- Console.WriteLine(" 2Return Value should be: 78");
- Console.WriteLine(" 2Return Value: {0}", returnValue);
- Console.WriteLine();
- string soutValue = "";
- string sinOutValue = "";
- string sreturnValue = "";
- myParameter1.Value = 999;
- myParameter3.Value = 66;
- cmd3.ExecuteNonQuery();
- if (myParameter2.Value == DBNull.Value)
- soutValue = "DBNull.Value";
- else
- soutValue = myParameter2.Value.ToString();
- if (myParameter3.Value == DBNull.Value)
- sinOutValue = "DBNull.Value";
- else
- sinOutValue = myParameter3.Value.ToString();
- if (myParameter4.Value == DBNull.Value)
- sreturnValue = "DBNull.Value";
- else
- sreturnValue = myParameter4.Value.ToString();
- Console.WriteLine(" 3Out Value should be: DBNull.Value");
- Console.WriteLine(" 3Out Value: {0}", soutValue);
- Console.WriteLine(" 3InOut Value should be: DBNull.Value");
- Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
- Console.WriteLine(" 3Return Value should be: DBNull.Value");
- Console.WriteLine(" 3Return Value: {0}", sreturnValue);
- Console.WriteLine();
- myParameter1.Value = 111;
- myParameter3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = (decimal)myParameter2.Value;
- inOutValue = (decimal)myParameter3.Value;
- returnValue = (decimal)myParameter4.Value;
- Console.WriteLine(" 4Out Value should be: 0 (as in digit zero)");
- Console.WriteLine(" 4Out Value: {0}", outValue);
- Console.WriteLine(" 4InOut Value should be: 1234567890123.12345678");
- Console.WriteLine(" 4InOut Value: {0}", inOutValue);
- Console.WriteLine(" 4Return Value should be: 123.45");
- Console.WriteLine(" 4Return Value: {0}", returnValue);
- Console.WriteLine();
- }
- static void OutParmTest3 (OracleConnection con)
- {
- // test stored function with 4 parameters
- // 1. input date
- // 2. output date
- // 3. input output date
- // 4. return dae
- // a DATE type in Oracle has Date and Time
- Console.WriteLine(" Create stored function SF_TESTOUTPARM3 to test Date parameters...");
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM3(parm1 IN DATE, parm2 OUT DATE, parm3 IN OUT DATE) RETURN DATE " +
- "IS " +
- " returnValue DATE := TO_DATE('2001-07-01 15:32:52', 'YYYY-MM-DD HH24:MI:SS');" +
- "BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := TO_DATE('1900-12-31', 'YYYY-MM-DD'); " +
- " parm3 := TO_DATE('1900-12-31', 'YYYY-MM-DD'); " +
- " ELSIF parm1 = TO_DATE('1979-11-25','YYYY-MM-DD') THEN " +
- " parm2 := NULL;" +
- " parm3 := NULL;" +
- " returnValue := NULL;"+
- " ELSIF parm3 IS NULL THEN " +
- " parm2 := TO_DATE('2008-08-08', 'YYYY-MM-DD');" +
- " parm3 := TO_DATE('2000-01-01', 'YYYY-MM-DD');" +
- " ELSE " +
- " -- add 3 days to date\n " +
- " parm2 := parm1 + 3; " +
- " parm3 := parm3 + 5; " +
- " END IF; " +
- " RETURN returnValue;" +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored function SF_TESTOUTPARM3 with 4 parameters...");
- OracleCommand cmd3 = con.CreateCommand();
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "BEGIN " +
- " :returnValue := SF_TESTOUTPARM3(:p1, :p2, :p3);" +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.DateTime);
- myParameter1.Value = new DateTime(2004, 12, 15);
- myParameter1.Direction = ParameterDirection.Input;
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.DateTime);
- myParameter2.Direction = ParameterDirection.Output;
- OracleParameter myParameter3 = new OracleParameter("p3", OracleType.DateTime);
- myParameter3.Value = new DateTime(2008, 10, 14, 20, 21, 22);
- myParameter3.Direction = ParameterDirection.InputOutput;
- OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.DateTime);
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameter3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- DateTime outValue = (DateTime)myParameter2.Value;
- DateTime inOutValue = (DateTime)myParameter3.Value;
- DateTime returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 1Out Value should be: 2004-12-18 00:00:00");
- Console.WriteLine(" 1Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 1InOut Value should be: 2008-10-19 20:21:22");
- Console.WriteLine(" 1InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 1Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 1Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameter3.Value = new DateTime(1980, 11, 22);
- cmd3.ExecuteNonQuery();
- outValue = (DateTime)myParameter2.Value;
- inOutValue = (DateTime)myParameter3.Value;
- returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 2Out Value should be: 1900-12-31 00:00:00");
- Console.WriteLine(" 2Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 2InOut Value should be: 1900-12-31 00:00:00");
- Console.WriteLine(" 2InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 2Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 2Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- myParameter1.Value = new DateTime(1979, 11, 25);
- myParameter3.Value = new DateTime(1981, 12, 14);
- cmd3.ExecuteNonQuery();
- string soutValue = "";
- string sinOutValue = "";
- string sreturnValue = "";
- if (myParameter2.Value == DBNull.Value)
- soutValue = "DBNull.Value";
- else {
- outValue = (DateTime)myParameter2.Value;
- soutValue = outValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- if (myParameter3.Value == DBNull.Value)
- sinOutValue = "DBNull.Value";
- else {
- inOutValue = (DateTime)myParameter3.Value;
- sinOutValue = inOutValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- if (myParameter4.Value == DBNull.Value)
- sreturnValue = "DBNull.Value";
- else {
- returnValue = (DateTime)myParameter4.Value;
- sreturnValue = returnValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- Console.WriteLine(" 3Out Value should be: DBNull.Value");
- Console.WriteLine(" 3Out Value: {0}", soutValue);
- Console.WriteLine(" 3InOut Value should be: DBNull.Value");
- Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
- Console.WriteLine(" 3Return Value should be: DBNull.Value");
- Console.WriteLine(" 3Return Value: {0}", sreturnValue);
- Console.WriteLine();
- myParameter1.Value = new DateTime(1976, 7, 4);
- myParameter3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = (DateTime)myParameter2.Value;
- inOutValue = (DateTime)myParameter3.Value;
- returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 4Out Value should be: 2008-08-08 00:00:00");
- Console.WriteLine(" 4Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 4InOut Value should be: 2000-01-01 00:00:00");
- Console.WriteLine(" 4InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 4Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 4Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- }
- static void OutParmTest4(OracleConnection con)
- {
- // test stored fuctions with 4 parameters
- // 1. input long
- // 2. output long
- // 3. input output long
- // 4. return long
- Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST4 for testing LONG VARCHAR Input, Output, InputOutput, Return parameters...");
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SP_OUTPUTPARMTEST4(parm1 IN LONG, parm2 OUT LONG, parm3 IN OUT LONG) RETURN LONG " +
- "IS " +
- " returnValue LONG := 'A very, very, very long value in a far away memory space.'; " +
- "BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := 'parm1 is null'; " +
- " returnValue := 'Another one bytes the dust.'; " +
- " ELSE " +
- " parm2 := 'One' || parm1 || 'Three'; " +
- " END IF; " +
- " IF parm3 IS NOT NULL THEN " +
- " parm3 := parm2 || parm3 || 'Five'; " +
- " ELSE " +
- " parm3 := 'parm3 in was NULL'; " +
- " END IF; " +
- " IF parm1 IS NOT NULL THEN " +
- " IF parm1 = '999' THEN " +
- " parm2 := NULL; " +
- " parm3 := NULL; " +
- " returnValue := NULL; " +
- " END IF; " +
- " END IF; " +
- " RETURN returnValue; " +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored procedure SP_OUTPUTPARMTEST4 with 4 parameters...");
- OracleCommand cmd3 = con.CreateCommand();
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "BEGIN " +
- " :ReturnValue := SP_OUTPUTPARMTEST4(:p1, :p2, :p3); " +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.LongVarChar);
- myParameter1.Size = 1000;
- myParameter1.Direction = ParameterDirection.Input;
- myParameter1.Value = "Two";
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.LongVarChar);
- myParameter2.Size = 1000;
- myParameter2.Direction = ParameterDirection.Output;
- OracleParameter myParameter3 = new OracleParameter("p3", OracleType.LongVarChar);
- myParameter3.Value = "Four";
- myParameter3.Size = 1000;
- myParameter3.Direction = ParameterDirection.InputOutput;
- OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.LongVarChar);
- myParameter4.Size = 1000;
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameter3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- string outValue = (string)myParameter2.Value;
- string inOutValue = (string)myParameter3.Value;
- string returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 1Out Value should be: OneTwoThree");
- Console.WriteLine(" 1Out Value: " + outValue);
- Console.WriteLine(" 1InOut Value should be: OneTwoThreeFourFive");
- Console.WriteLine(" 1InOut Value: " + inOutValue);
- Console.WriteLine(" 1Return Value should be: A very, very, very long value in a far away memory space.");
- Console.WriteLine(" 1Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameter3.Value = "Hello";
- cmd3.ExecuteNonQuery();
- outValue = (string)myParameter2.Value;
- inOutValue = (string)myParameter3.Value;
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 2Out Value should be: parm1 is null");
- Console.WriteLine(" 2Out Value: " + outValue);
- Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
- Console.WriteLine(" 2InOut Value: " + inOutValue);
- Console.WriteLine(" 2Return Value should be: Another one bytes the dust.");
- Console.WriteLine(" 2Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "999";
- myParameter3.Value = "Bye";
- cmd3.ExecuteNonQuery();
- if (myParameter2.Value == DBNull.Value)
- outValue = "Value is DBNull.Value";
- else
- outValue = (string)myParameter2.Value;
- if (myParameter3.Value == DBNull.Value)
- inOutValue = "Value is DBNullValue";
- else
- inOutValue = (string)myParameter3.Value;
- if (myParameter4.Value == DBNull.Value)
- returnValue = "Value is DBNull.Value";
- else
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Out Value: " + outValue);
- Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3InOut Value: " + inOutValue);
- Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "***";
- myParameter3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = (string)myParameter2.Value;
- inOutValue = (string)myParameter3.Value;
- returnValue = (string)myParameter4.Value;
- Console.WriteLine(" 4Out Value should be: One***Three");
- Console.WriteLine(" 4Out Value: " + outValue);
- Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
- Console.WriteLine(" 4InOut Value: " + inOutValue);
- Console.WriteLine(" 4Return Value should be: A very, very, very long value in a far away memory space.");
- Console.WriteLine(" 4Return Value: " + returnValue);
- Console.WriteLine();
- }
- static void OutParmTest5(OracleConnection con)
- {
- // test stored fuctions with 4 parameters
- // 1. input CLOB
- // 2. output CLOB
- // 3. input output CLOB
- // 4. return CLOB
- Console.WriteLine(" Create stored function SP_OUTPUTPARMTEST5 for testing CLOB Input, Output, InputOutput, Return parameters...");
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SP_OUTPUTPARMTEST5(parm1 IN CLOB, parm2 OUT CLOB, parm3 IN OUT CLOB) RETURN CLOB " +
- " IS " +
- " returnValue CLOB := 'Clobber'; " +
- " BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := 'parm1 is null'; " +
- " ELSE " +
- " parm2 := 'One' || parm1 || 'Three'; " +
- " END IF; " +
- " IF parm3 IS NOT NULL THEN " +
- " parm3 := parm2 || parm3 || 'Five'; " +
- " ELSE " +
- " parm3 := 'parm3 in was NULL'; " +
- " END IF; " +
- " IF parm1 IS NOT NULL THEN " +
- " IF parm1 = '999' THEN " +
- " parm2 := NULL; " +
- " parm3 := NULL; " +
- " returnValue := NULL; " +
- " ELSIF LENGTH(parm1) = 0 THEN " +
- " parm2 := 'parm1 is zero length'; " +
- " IF LENGTH(parm3) = 0 THEN " +
- " parm3 := 'parm3 is zero length';" +
- " ELSE " +
- " parm3 := 'Uh oh, parm3 is not zero length like we thought'; " +
- " END IF; " +
- " returnValue := 'parm1 is zero length'; " +
- " ELSIF parm1 = '888' THEN " +
- " parm2 := EMPTY_CLOB(); " +
- " parm3 := EMPTY_CLOB(); " +
- " returnValue := EMPTY_CLOB(); " +
- " END IF; " +
- " END IF; " +
- " RETURN returnValue; " +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored procedure SP_OUTPUTPARMTEST5 with 4 parameters...");
- //OracleTransaction trans = con.BeginTransaction();
- //OracleCommand cmd4 = con.CreateCommand();
- //cmd4.Transaction = trans;
- //OracleLob lob = CreateTemporaryLobLocator(cmd4, OracleType.Clob);
- OracleCommand cmd3 = con.CreateCommand();
- //cmd3.Transaction = trans;
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "DECLARE " +
- " tempP3 CLOB; " +
- "BEGIN " +
- " tempP3 := :inp3; " +
- " :ReturnValue := SP_OUTPUTPARMTEST5(:p1, :p2, tempP3); " +
- " :outp3 := tempP3;" +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Clob);
- myParameter1.Size = 1000;
- myParameter1.Direction = ParameterDirection.Input;
- myParameter1.Value = "Two";
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Clob);
- myParameter2.Size = 1000;
- myParameter2.Direction = ParameterDirection.Output;
- // impossible to use one OracleParameter for an CLOB IN OUT parameter?
- // I had to create two parameters for the 3rd parameter: in3 as input and out3 as output
- // and in the anonymous PL/SQL block, get and set the 3rd parameter appropriately
- OracleParameter myParameterIn3 = new OracleParameter("inp3", OracleType.Clob);
- myParameterIn3.Size = 1000;
- myParameterIn3.Direction = ParameterDirection.Input;
- string s = "Everything";
- myParameterIn3.Value = s;
- OracleParameter myParameterOut3 = new OracleParameter("outp3", OracleType.Clob);
- myParameterOut3.Size = 1000;
- myParameterOut3.Direction = ParameterDirection.Output;
- OracleParameter myParameter4 = new OracleParameter("ReturnValue", OracleType.Clob);
- myParameter4.Size = 1000;
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameterIn3);
- cmd3.Parameters.Add(myParameterOut3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- string outValue = GetOracleClobValue(myParameter2);
- string inOutValue = GetOracleClobValue(myParameterOut3);
- string returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 1Out Value should be: OneTwoThree");
- Console.WriteLine(" 1Out Value: " + outValue);
- Console.WriteLine(" 1InOut Value should be: OneTwoThreeEverythingFive");
- Console.WriteLine(" 1InOut Value: " + inOutValue);
- Console.WriteLine(" 1Return Value should be: Clobber");
- Console.WriteLine(" 1Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameterIn3.Value = "Hello";
- cmd3.ExecuteNonQuery();
- outValue = GetOracleClobValue(myParameter2);
- inOutValue = GetOracleClobValue(myParameterOut3);
- returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 2Out Value should be: parm1 is null");
- Console.WriteLine(" 2Out Value: " + outValue);
- Console.WriteLine(" 2InOut Value should be: parm1 is nullHelloFive");
- Console.WriteLine(" 2InOut Value: " + inOutValue);
- Console.WriteLine(" 2Return Value should be: Clobber");
- Console.WriteLine(" 2Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "999";
- myParameterIn3.Value = "Bye";
- cmd3.ExecuteNonQuery();
- outValue = GetOracleClobValue(myParameter2);
- inOutValue = GetOracleClobValue(myParameterOut3);
- returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 3Out Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Out Value: " + outValue);
- Console.WriteLine(" 3InOut Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3InOut Value: " + inOutValue);
- Console.WriteLine(" 3Return Value should be: Value is DBNull.Value");
- Console.WriteLine(" 3Return Value: " + returnValue);
- Console.WriteLine();
- myParameter1.Value = "***";
- myParameterIn3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = GetOracleClobValue(myParameter2);
- inOutValue = GetOracleClobValue(myParameterOut3);
- returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 4Out Value should be: One***Three");
- Console.WriteLine(" 4Out Value: " + outValue);
- Console.WriteLine(" 4InOut Value should be: parm3 in was NULL");
- Console.WriteLine(" 4InOut Value: " + inOutValue);
- Console.WriteLine(" 4Return Value should be: Clobber");
- Console.WriteLine(" 4Return Value: " + returnValue);
- Console.WriteLine();
-
- myParameter1.Value = OracleLob.Null;
- myParameterIn3.Value = "bass";
- cmd3.ExecuteNonQuery();
- outValue = GetOracleClobValue(myParameter2);
- inOutValue = GetOracleClobValue(myParameterOut3);
- returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 5Out Value should be: parm1 is null");
- Console.WriteLine(" 5Out Value: " + outValue);
- Console.WriteLine(" 5InOut Value should be: parm1 is nullbassFive");
- Console.WriteLine(" 5InOut Value: " + inOutValue);
- Console.WriteLine(" 5Return Value should be: Clobber");
- Console.WriteLine(" 5Return Value: " + returnValue);
- Console.WriteLine();
-
- myParameter1.Value = "888";
- myParameterIn3.Value = "777";
- cmd3.ExecuteNonQuery();
- outValue = GetOracleClobValue(myParameter2);
- inOutValue = GetOracleClobValue(myParameterOut3);
- returnValue = GetOracleClobValue(myParameter4);
- Console.WriteLine(" 6Out Value should be: Zero Length");
- Console.WriteLine(" 6Out Value: " + outValue);
- Console.WriteLine(" 6InOut Value should be: Zero Length");
- Console.WriteLine(" 6InOut Value: " + inOutValue);
- Console.WriteLine(" 6Return Value should be: Zero Length");
- Console.WriteLine(" 6Return Value: " + returnValue);
- Console.WriteLine();
- }
- public static string GetOracleClobValue (OracleParameter parm)
- {
- if (parm.Value.Equals (DBNull.Value))
- return "Clob is DBNull.Value";
- OracleLob lob = (OracleLob) parm.Value;
- if (lob.Length == 0)
- return "Zero Length";
- return lob.Value.ToString ();
- }
- public static OracleLob CreateTemporaryLobLocator (OracleCommand cmd, OracleType lobType)
- {
- cmd.CommandText =
- "DECLARE TEMP_LOB " + lobType.ToString () + "; " +
- " BEGIN " +
- " SYS.DBMS_LOB.CREATETEMPORARY (TEMP_LOB, FALSE); " +
- " :TempLobLocator := TEMP_LOB; " +
- " END;";
- OracleParameter parm = cmd.Parameters.Add ("TempLobLocator", lobType);
- parm.Direction = ParameterDirection.Output;
- cmd.ExecuteNonQuery ();
- return (OracleLob)parm.Value;
- }
- static void OutParmTest6 (OracleConnection con)
- {
- // test stored function with 4 parameters
- // 1. input timestamp
- // 2. output timestamp
- // 3. input output timestamp
- // 4. return timestamp
- // a TIMESTAMP type in Oracle has Date and Time
- Console.WriteLine(" Create stored function SF_TESTOUTPARM6 to test Date parameters...");
- OracleCommand cmd2 = con.CreateCommand();
- cmd2.CommandText =
- "CREATE OR REPLACE FUNCTION SF_TESTOUTPARM6(parm1 IN TIMESTAMP, parm2 OUT TIMESTAMP, parm3 IN OUT TIMESTAMP) RETURN TIMESTAMP " +
- "IS " +
- " returnValue TIMESTAMP := TO_TIMESTAMP('2001-07-01 15:32:52', 'YYYY-MM-DD HH24:MI:SS');" +
- "BEGIN " +
- " IF parm1 IS NULL THEN " +
- " parm2 := TO_TIMESTAMP('1900-12-31', 'YYYY-MM-DD'); " +
- " parm3 := TO_TIMESTAMP('1900-12-31', 'YYYY-MM-DD'); " +
- " ELSIF parm1 = TO_TIMESTAMP('1979-11-25','YYYY-MM-DD') THEN " +
- " parm2 := NULL;" +
- " parm3 := NULL;" +
- " returnValue := NULL;"+
- " ELSIF parm3 IS NULL THEN " +
- " parm2 := TO_TIMESTAMP('2008-08-08', 'YYYY-MM-DD');" +
- " parm3 := TO_TIMESTAMP('2000-01-01', 'YYYY-MM-DD');" +
- " ELSE " +
- " -- add 3 days to date\n " +
- " parm2 := parm1 + 3; " +
- " parm3 := parm3 + 5; " +
- " END IF; " +
- " RETURN returnValue;" +
- "END;";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" COMMIT...");
- cmd2.CommandText = "COMMIT";
- cmd2.ExecuteNonQuery();
- Console.WriteLine(" Call stored function SF_TESTOUTPARM6 with 4 parameters...");
- OracleCommand cmd3 = con.CreateCommand();
- cmd3.CommandType = CommandType.Text;
- cmd3.CommandText =
- "BEGIN " +
- " :returnValue := SF_TESTOUTPARM6(:p1, :p2, :p3);" +
- "END;";
- OracleParameter myParameter1 = new OracleParameter("p1", OracleType.Timestamp);
- myParameter1.Value = new DateTime(2004, 12, 15);
- myParameter1.Direction = ParameterDirection.Input;
- OracleParameter myParameter2 = new OracleParameter("p2", OracleType.Timestamp);
- myParameter2.Direction = ParameterDirection.Output;
- OracleParameter myParameter3 = new OracleParameter("p3", OracleType.Timestamp);
- myParameter3.Value = new DateTime(2008, 10, 14, 20, 21, 22);
- myParameter3.Direction = ParameterDirection.InputOutput;
- OracleParameter myParameter4 = new OracleParameter("returnValue", OracleType.Timestamp);
- myParameter4.Direction = ParameterDirection.ReturnValue;
- cmd3.Parameters.Add(myParameter1);
- cmd3.Parameters.Add(myParameter2);
- cmd3.Parameters.Add(myParameter3);
- cmd3.Parameters.Add(myParameter4);
- cmd3.ExecuteNonQuery();
- DateTime outValue = (DateTime)myParameter2.Value;
- DateTime inOutValue = (DateTime)myParameter3.Value;
- DateTime returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 1Out Value should be: 2004-12-18 00:00:00");
- Console.WriteLine(" 1Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 1InOut Value should be: 2008-10-19 20:21:22");
- Console.WriteLine(" 1InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 1Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 1Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- myParameter1.Value = DBNull.Value;
- myParameter3.Value = new DateTime(1980, 11, 22);
- cmd3.ExecuteNonQuery();
- outValue = (DateTime)myParameter2.Value;
- inOutValue = (DateTime)myParameter3.Value;
- returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 2Out Value should be: 1900-12-31 00:00:00");
- Console.WriteLine(" 2Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 2InOut Value should be: 1900-12-31 00:00:00");
- Console.WriteLine(" 2InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 2Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 2Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- myParameter1.Value = new DateTime(1979, 11, 25);
- myParameter3.Value = new DateTime(1981, 12, 14);
- cmd3.ExecuteNonQuery();
- string soutValue = "";
- string sinOutValue = "";
- string sreturnValue = "";
- if (myParameter2.Value == DBNull.Value)
- soutValue = "DBNull.Value";
- else {
- outValue = (DateTime)myParameter2.Value;
- soutValue = outValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- if (myParameter3.Value == DBNull.Value)
- sinOutValue = "DBNull.Value";
- else {
- inOutValue = (DateTime)myParameter3.Value;
- sinOutValue = inOutValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- if (myParameter4.Value == DBNull.Value)
- sreturnValue = "DBNull.Value";
- else {
- returnValue = (DateTime)myParameter4.Value;
- sreturnValue = returnValue.ToString("yyyy-MM-dd HH:mm:ss");
- }
- Console.WriteLine(" 3Out Value should be: DBNull.Value");
- Console.WriteLine(" 3Out Value: {0}", soutValue);
- Console.WriteLine(" 3InOut Value should be: DBNull.Value");
- Console.WriteLine(" 3InOut Value: {0}", sinOutValue);
- Console.WriteLine(" 3Return Value should be: DBNull.Value");
- Console.WriteLine(" 3Return Value: {0}", sreturnValue);
- Console.WriteLine();
- myParameter1.Value = new DateTime(1976, 7, 4);
- myParameter3.Value = DBNull.Value;
- cmd3.ExecuteNonQuery();
- outValue = (DateTime)myParameter2.Value;
- inOutValue = (DateTime)myParameter3.Value;
- returnValue = (DateTime)myParameter4.Value;
- Console.WriteLine(" 4Out Value should be: 2008-08-08 00:00:00");
- Console.WriteLine(" 4Out Value: {0}", outValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 4InOut Value should be: 2000-01-01 00:00:00");
- Console.WriteLine(" 4InOut Value: {0}", inOutValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine(" 4Return Value should be: 2001-07-01 15:32:52");
- Console.WriteLine(" 4Return Value: {0}", returnValue.ToString("yyyy-MM-dd HH:mm:ss"));
- Console.WriteLine();
- }
- static void ShowConnectionProperties (OracleConnection con)
- {
- try {
- Console.WriteLine ("ServerVersion: " + con.ServerVersion);
- } catch (System.InvalidOperationException ioe) {
- Console.WriteLine ("InvalidOperationException caught.");
- Console.WriteLine ("Message: " + ioe.Message);
- }
- Console.WriteLine ("DataSource: " + con.DataSource);
- }
- static void NullAggregateTest (OracleConnection con)
- {
- Console.WriteLine(" Drop table MONO_TEST_TABLE3...");
- OracleCommand cmd2 = con.CreateCommand ();
- try {
- cmd2.CommandText = "DROP TABLE MONO_TEST_TABLE3";
- cmd2.ExecuteNonQuery ();
- }
- catch(OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine(" Create table MONO_TEST_TABLE3...");
-
- cmd2.CommandText = "CREATE TABLE MONO_TEST_TABLE3 (" +
- " COL1 VARCHAR2(8), "+
- " COL2 VARCHAR2(32))";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" Insert some rows into table MONO_TEST_TABLE3...");
- cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('1','one')";
- cmd2.ExecuteNonQuery ();
- cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('1','uno')";
- cmd2.ExecuteNonQuery ();
-
- cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3','three')";
- cmd2.ExecuteNonQuery ();
-
- cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3', null)";
- cmd2.ExecuteNonQuery ();
- cmd2.CommandText = "INSERT INTO MONO_TEST_TABLE3 (COL1, COL2) VALUES ('3','few')";
- cmd2.ExecuteNonQuery ();
- Console.WriteLine(" ExecuteScalar...");
- cmd2.CommandText = "SELECT COL1, COUNT(COL2) AS MAX_COL1 FROM MONO_TEST_TABLE3 GROUP BY COL1";
- OracleDataReader reader = cmd2.ExecuteReader ();
- Console.WriteLine (" Read...");
- while (reader.Read ()) {
- object obj0 = reader.GetValue (0);
- Console.WriteLine("Value 0: " + obj0.ToString ());
- object obj1 = reader.GetValue (1);
- Console.WriteLine("Value 1: " + obj1.ToString ());
-
- Console.WriteLine (" Read...");
- }
- Console.WriteLine (" No more records.");
- }
- static void OnInfoMessage (object sender, OracleInfoMessageEventArgs e)
- {
- Console.WriteLine("InfoMessage Message: " + e.Message.ToString());
- Console.WriteLine("InfoMessage Code: " + e.Code.ToString());
- Console.WriteLine("InfoMessage Source: " + e.Source.ToString());
- }
- static void OnStateChange (object sender, StateChangeEventArgs e)
- {
- Console.WriteLine("StateChange CurrentSate:" + e.CurrentState.ToString ());
- Console.WriteLine("StateChange OriginalState:" + e.OriginalState.ToString ());
- }
- static void RefCursorTests(OracleConnection con)
- {
- SetupRefCursorTests(con); // for ref cursor tests 1 thru 3
- RefCursorTest1(con); // using BEGIN/END
- RefCursorTest2(con); // using call
- RefCursorTest3(con); // using CommandType.StoredProcedure
-
- RefCursorTest4(con);
- }
- static void SetupRefCursorTests(OracleConnection con)
- {
- Console.WriteLine("Setup Oracle package curspkg_join...");
-
- OracleCommand cmd = con.CreateCommand();
- Console.Error.WriteLine(" create or replace package curspkg_join...");
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE curspkg_join AS\n" +
- "TYPE t_cursor IS REF CURSOR;\n" +
- "Procedure open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor);\n" +
- "END curspkg_join;";
- cmd.ExecuteNonQuery();
- Console.Error.WriteLine(" create or replace package body curspkg_join...");
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE BODY curspkg_join AS\n" +
- " Procedure open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)\n" +
- " IS\n" +
- " v_cursor t_cursor;\n" +
- " BEGIN\n" +
- " IF n_EMPNO <> 0 THEN\n" +
- " OPEN v_cursor FOR\n" +
- " SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME\n" +
- " FROM SCOTT.EMP, SCOTT.DEPT\n" +
- " WHERE EMP.DEPTNO = DEPT.DEPTNO\n" +
- " AND EMP.EMPNO = n_EMPNO;\n" +
- "\n" +
- " ELSE\n" +
- " OPEN v_cursor FOR\n" +
- " SELECT EMP.EMPNO, EMP.ENAME, DEPT.DEPTNO, DEPT.DNAME\n" +
- " FROM SCOTT.EMP, SCOTT.DEPT\n" +
- " WHERE EMP.DEPTNO = DEPT.DEPTNO;\n" +
- "\n" +
- " END IF;\n" +
- " io_cursor := v_cursor;\n" +
- " END open_join_cursor1;\n" +
- "END curspkg_join;";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "commit";
- cmd.ExecuteNonQuery();
- }
- public static void RefCursorTest4(OracleConnection connection)
- {
- Console.WriteLine("Setup test package and data for RefCursorTest4...");
- OracleCommand cmddrop = connection.CreateCommand();
- cmddrop.CommandText = "DROP TABLE TESTTABLE";
- try {
- cmddrop.ExecuteNonQuery();
- }
- catch(OracleException e) {
- Console.WriteLine("Ignore this error: " + e.Message);
- }
- cmddrop.Dispose();
- cmddrop = null;
- OracleCommand cmd = connection.CreateCommand();
- // create table TESTTABLE
- cmd.CommandText =
- "create table TESTTABLE (\n" +
- " col1 numeric(18,0),\n" +
- " col2 char(32),\n" +
- " col3 date)";
- cmd.ExecuteNonQuery();
- // insert some rows into TESTTABLE
- cmd.CommandText =
- "insert into TESTTABLE\n" +
- "(col1, col2, col3)\n" +
- "values(45, 'Mono', sysdate)";
- cmd.ExecuteNonQuery();
- cmd.CommandText =
- "insert into TESTTABLE\n" +
- "(col1, col2, col3)\n" +
- "values(136, 'Fun', sysdate)";
- cmd.ExecuteNonQuery();
- cmd.CommandText =
- "insert into TESTTABLE\n" +
- "(col1, col2, col3)\n" +
- "values(526, 'System.Data.OracleClient', sysdate)";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "commit";
- cmd.ExecuteNonQuery();
- // create Oracle package TestTablePkg
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE TestTablePkg\n" +
- "AS\n" +
- " TYPE T_CURSOR IS REF CURSOR;\n" +
- "\n" +
- " PROCEDURE GetData(tableCursor OUT T_CURSOR);\n" +
- "END TestTablePkg;";
- cmd.ExecuteNonQuery();
- // create Oracle package body for package TestTablePkg
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE BODY TestTablePkg AS\n" +
- " PROCEDURE GetData(tableCursor OUT T_CURSOR)\n" +
- " IS\n" +
- " BEGIN\n" +
- " OPEN tableCursor FOR\n" +
- " SELECT *\n" +
- " FROM TestTable;\n" +
- " END GetData;\n" +
- "END TestTablePkg;";
- cmd.ExecuteNonQuery();
- cmd.Dispose();
- cmd = null;
- Console.WriteLine("Set up command and parameters to call stored proc...");
- OracleCommand command = new OracleCommand("TestTablePkg.GetData", connection);
- command.CommandType = CommandType.StoredProcedure;
- OracleParameter parameter = new OracleParameter("tableCursor", OracleType.Cursor);
- parameter.Direction = ParameterDirection.Output;
- command.Parameters.Add(parameter);
- Console.WriteLine("Execute...");
- command.ExecuteNonQuery();
- Console.WriteLine("Get OracleDataReader for cursor output parameter...");
- OracleDataReader reader = (OracleDataReader) parameter.Value;
-
- Console.WriteLine("Read data...");
- int r = 0;
- while (reader.Read()) {
- Console.WriteLine("Row {0}", r);
- for (int f = 0; f < reader.FieldCount; f ++) {
- object val = reader.GetValue(f);
- Console.WriteLine(" Field {0} Value: {1}", f, val.ToString());
- }
- r ++;
- }
- Console.WriteLine("Rows retrieved: {0}", r);
- Console.WriteLine("Clean up...");
- reader.Close();
- reader = null;
- command.Dispose();
- command = null;
- }
- static void RefCursorTest1(OracleConnection con)
- {
- Console.WriteLine("Ref Cursor Test 1 - using BEGIN/END for proc - Begin...");
- Console.WriteLine("Create command...");
- OracleCommand cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.CommandText =
- "BEGIN\n" +
- " curspkg_join.open_join_cursor1(:n_Empno,:io_cursor);\n" +
- "END;";
-
- // PL/SQL definition of stored procedure in package curspkg_join
- // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
- Console.WriteLine("Create parameters...");
- OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
- parm1.Direction = ParameterDirection.Input;
- parm1.Value = 7902;
- OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
- parm2.Direction = ParameterDirection.Output;
- cmd.Parameters.Add(parm1);
- cmd.Parameters.Add(parm2);
- // positional parm
- //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
- // named parm
- //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
- OracleDataReader reader;
- Console.WriteLine("Execute Non Query...");
- cmd.ExecuteNonQuery();
- Console.WriteLine("Get data reader (ref cursor) from out parameter...");
- reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
- int x, count;
- count = 0;
- Console.WriteLine("Get data from ref cursor...");
- while (reader.Read()) {
- for (x = 0; x < reader.FieldCount; x++)
- Console.Write(reader[x] + " ");
-
- Console.WriteLine();
- count += 1;
- }
- Console.WriteLine(count.ToString() + " Rows Returned.");
- reader.Close();
- }
- static void RefCursorTest2(OracleConnection con)
- {
- Console.WriteLine("Ref Cursor Test 2 - using call - Begin...");
- Console.WriteLine("Create command...");
- OracleCommand cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.CommandText = "call curspkg_join.open_join_cursor1(:n_Empno,:io_cursor)";
-
- // PL/SQL definition of stored procedure in package curspkg_join
- // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
- Console.WriteLine("Create parameters...");
- OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
- parm1.Direction = ParameterDirection.Input;
- parm1.Value = 7902;
- OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
- parm2.Direction = ParameterDirection.Output;
- cmd.Parameters.Add(parm1);
- cmd.Parameters.Add(parm2);
- // positional parm
- //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
- // named parm
- //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
- OracleDataReader reader;
- Console.WriteLine("Execute Non Query...");
- cmd.ExecuteNonQuery();
- Console.WriteLine("Get data reader (ref cursor) from out parameter...");
- reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
- int x, count;
- count = 0;
- Console.WriteLine("Get data from ref cursor...");
- while (reader.Read()) {
- for (x = 0; x < reader.FieldCount; x++)
- Console.Write(reader[x] + " ");
-
- Console.WriteLine();
- count += 1;
- }
- Console.WriteLine(count.ToString() + " Rows Returned.");
- reader.Close();
- }
- static void RefCursorTest3(OracleConnection con)
- {
- Console.WriteLine("Ref Cursor Test 3 - CommandType.StoredProcedure - Begin...");
- Console.WriteLine("Create command...");
- OracleCommand cmd = new OracleCommand();
- cmd.Connection = con;
- cmd.CommandText = "curspkg_join.open_join_cursor1";
- cmd.CommandType = CommandType.StoredProcedure;
-
- // PL/SQL definition of stored procedure in package curspkg_join
- // open_join_cursor1 (n_EMPNO IN NUMBER, io_cursor IN OUT t_cursor)
- Console.WriteLine("Create parameters...");
- OracleParameter parm1 = new OracleParameter("n_Empno", OracleType.Number);
- parm1.Direction = ParameterDirection.Input;
- parm1.Value = 7902;
- OracleParameter parm2 = new OracleParameter("io_cursor", OracleType.Cursor);
- parm2.Direction = ParameterDirection.Output;
- cmd.Parameters.Add(parm1);
- cmd.Parameters.Add(parm2);
- // positional parm
- //cmd.Parameters.Add(new OracleParameter("io_cursor", OracleType.Cursor)).Direction = ParameterDirection.Output;
- // named parm
- //cmd.Parameters.Add("n_Empno", OracleType.Number, 4).Value = 7902;
- OracleDataReader reader;
- Console.WriteLine("Execute Non Query...");
- cmd.ExecuteNonQuery();
- Console.WriteLine("Get data reader (ref cursor) from out parameter...");
- reader = (OracleDataReader) cmd.Parameters["io_cursor"].Value;
- int x, count;
- count = 0;
- Console.WriteLine("Get data from ref cursor...");
- while (reader.Read()) {
- for (x = 0; x < reader.FieldCount; x++)
- Console.Write(reader[x] + " ");
-
- Console.WriteLine();
- count += 1;
- }
- Console.WriteLine(count.ToString() + " Rows Returned.");
- reader.Close();
- }
- static void ExternalAuthenticationTest ()
- {
- string user = Environment.UserName;
- if (!Environment.UserDomainName.Equals(String.Empty))
- user = Environment.UserDomainName + "\\" + Environment.UserName;
- Console.WriteLine("Environment UserDomainName and UserName: " + user);
- Console.WriteLine("Open connection using external authentication...");
- OracleConnection con = new OracleConnection("Data Source=palis;Integrated Security=true");
- try {
- con.Open();
- OracleCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT USER FROM DUAL";
- OracleDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- Console.WriteLine("User: " + reader.GetString(reader.GetOrdinal("USER")));
- con.Close();
- }
- catch (Exception e) {
- Console.WriteLine("Exception caught: " + e.Message);
- Console.WriteLine("Probably not setup for external authentication.");
- }
- con.Dispose();
- con = null;
- }
- public static void TestPersistSucurityInfo1()
- {
- Console.WriteLine("\nTestPersistSucurityInfo1 - persist security info=false");
- OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=false");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- con.Open();
- Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
- con.Close();
- Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
- con = null;
- }
- public static void TestPersistSucurityInfo2()
- {
- Console.WriteLine("\nTestPersistSucurityInfo2 - persist security info=true");
- OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=true");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- con.Open();
- Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
- con.Close();
- Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
- con = null;
- }
- public static void TestPersistSucurityInfo3()
- {
- Console.WriteLine("\nTestPersistSucurityInfo3 - use default for persist security info which is false");
- OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- con.Open();
- Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
- con.Close();
- Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
- con = null;
- }
- public static void TestPersistSucurityInfo4()
- {
- Console.WriteLine("\nTestPersistSucurityInfo4 - persist security info=false with password at front");
- OracleConnection con = new OracleConnection(";password=tiger;data source=palis;user id=scott;persist security info=false");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- con.Open();
- Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
- con.Close();
- Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
- con = null;
- }
- public static void TestPersistSucurityInfo5()
- {
- Console.WriteLine("\nTestPersistSucurityInfo5 - persist security info=false");
- OracleConnection con = new OracleConnection("data source=palis;user id=scott;password=tiger;persist security info=false");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- con.Open();
- Console.WriteLine("ConnectionString after open: " + con.ConnectionString);
- Console.WriteLine("ConnectionState for con: " + con.State.ToString() + "\n");
-
- Console.WriteLine("Clone OracleConnection...");
- OracleConnection con2 = (OracleConnection) ((ICloneable) con).Clone();
-
- Console.WriteLine("ConnectionState for con2: " + con2.State.ToString());
- Console.WriteLine("con2 ConnectionString before open: " + con2.ConnectionString);
- con2.Open();
- Console.WriteLine("con2 ConnectionString after open: " + con2.ConnectionString);
- con2.Close();
- Console.WriteLine("con2 ConnectionString after close: " + con2.ConnectionString);
-
- con.Close();
- }
- public static void TestPersistSucurityInfo6()
- {
- Console.WriteLine("\nTestPersistSucurityInfo6 - external auth using persist security info");
- string user = Environment.UserName;
- if (!Environment.UserDomainName.Equals(String.Empty))
- user = Environment.UserDomainName + "\\" + Environment.UserName;
- Console.WriteLine("Environment UserDomainName and UserName: " + user);
- Console.WriteLine("Open connection using external authentication...");
- OracleConnection con = new OracleConnection("Data Source=palis;Integrated Security=true");
- Console.WriteLine("ConnectionString before open: " + con.ConnectionString);
- try {
- con.Open();
- OracleCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT USER FROM DUAL";
- OracleDataReader reader = cmd.ExecuteReader();
- if (reader.Read())
- Console.WriteLine("User: " + reader.GetString(reader.GetOrdinal("USER")));
- con.Close();
- Console.WriteLine("ConnectionString after close: " + con.ConnectionString);
- }
- catch (Exception e) {
- Console.WriteLine("Exception caught: " + e.Message);
- Console.WriteLine("Probably not setup for external authentication. This is fine.");
- }
- con.Dispose();
- Console.WriteLine("ConnectionString after dispose: " + con.ConnectionString);
- con = null;
- Console.WriteLine("\n\n");
- }
- public static void ConnectionPoolingTest1 ()
- {
- Console.WriteLine("Start Connection Pooling Test 1...");
- OracleConnection[] connections = null;
- int maxCon = MAX_CONNECTIONS + 1; // add 1 more over the max connections to cause it to wait for the next available connection
- int i = 0;
- try {
- connections = new OracleConnection[maxCon];
-
- for (i = 0; i < maxCon; i++) {
- Console.WriteLine(" Open connection: {0}", i);
- connections[i] = new OracleConnection(conStr);
- connections[i].Open ();
- }
- } catch (InvalidOperationException e) {
- Console.WriteLine("Expected exception InvalidOperationException caught.");
- Console.WriteLine(e);
- }
- for (i = 0; i < maxCon; i++) {
- if (connections[i] != null) {
- Console.WriteLine(" Close connection: {0}", i);
- if (connections[i].State == ConnectionState.Open)
- connections[i].Close ();
- connections[i].Dispose ();
- connections[i] = null;
- }
- }
- connections = null;
- Console.WriteLine("Done Connection Pooling Test 1.");
- }
- public static void ConnectionPoolingTest2 ()
- {
- Console.WriteLine("Start Connection Pooling Test 2...");
- OracleConnection[] connections = null;
- int maxCon = MAX_CONNECTIONS;
- int i = 0;
- connections = new OracleConnection[maxCon];
-
- for (i = 0; i < maxCon; i++) {
- Console.WriteLine(" Open connection: {0}", i);
- connections[i] = new OracleConnection(conStr);
- connections[i].Open ();
- }
-
- Console.WriteLine("Start another thread...");
- t = new Thread(new ThreadStart(AnotherThreadProc));
- t.Start ();
- Console.WriteLine("Sleep...");
- Thread.Sleep(100);
- Console.WriteLine("Closing...");
- for (i = 0; i < maxCon; i++) {
- if (connections[i] != null) {
- Console.WriteLine(" Close connection: {0}", i);
- if (connections[i].State == ConnectionState.Open)
- connections[i].Close ();
- connections[i].Dispose ();
- connections[i] = null;
- }
- }
- connections = null;
- }
- private static void AnotherThreadProc ()
- {
- Console.WriteLine("Open connection via another thread...");
- OracleConnection[] connections = null;
- int maxCon = MAX_CONNECTIONS;
- int i = 0;
- connections = new OracleConnection[maxCon];
-
- for (i = 0; i < maxCon; i++) {
- Console.WriteLine(" Open connection: {0}", i);
- connections[i] = new OracleConnection(conStr);
- connections[i].Open ();
- }
- Console.WriteLine("Done Connection Pooling Test 2.");
- System.Environment.Exit (0);
- }
- private static void SetParameterOracleType (OracleConnection con)
- {
- Console.WriteLine();
- OracleParameter p = con.CreateCommand().CreateParameter();
- Console.WriteLine("p.OracleType [VarChar]: " + p.OracleType.ToString());
- p.OracleType = OracleType.Clob;
- Console.WriteLine("p.OracleType [Clob]: " + p.OracleType.ToString());
- p.Value = "SomeString";
- Console.WriteLine("p.OracleType [Clob]: " + p.OracleType.ToString());
- Console.WriteLine();
- OracleParameter p2 = con.CreateCommand().CreateParameter();
- Console.WriteLine("p2.OracleType [VarChar]: " + p2.OracleType.ToString());
- p2.Value = new byte[] { 0x01, 0x02, 0x03, 0x04 };
- Console.WriteLine("p2.OracleType [VarChar]: " + p2.OracleType.ToString());
- p2.OracleType = OracleType.Blob;
- Console.WriteLine("p2.OracleType [Blob]: " + p2.OracleType.ToString());
- Console.WriteLine();
- OracleParameter p3 = new OracleParameter("test", OracleType.Clob);
- Console.WriteLine("p3.OracleType [Clob]: " + p3.OracleType.ToString());
- p3.Value = "blah";
- Console.WriteLine("p3.OracleType [Clob]: " + p3.OracleType.ToString());
- Console.WriteLine();
- OracleParameter p4 = new OracleParameter("test", "blah");
- Console.WriteLine("p4.OracleType [VarChar]: " + p4.OracleType.ToString());
- p4.OracleType = OracleType.Clob;
- Console.WriteLine("p4.OracleType [Clob]: " + p4.OracleType.ToString());
- Console.WriteLine();
- OracleParameter p5 = new OracleParameter ((string) null, new DateTime (2005, 3, 8));
- Console.WriteLine("p5.OracleType [DateTime]: " + p5.OracleType.ToString());
- }
- public static void InsertBlobTest(OracleConnection con)
- {
- checkTNS();
- SetupMyPackage(con);
- InsertBlob(con);
- }
- public static void checkTNS()
- {
- //string tnsAdmin = System.Environment.GetEnvironmentVariable("TNS_ADMIN");
- //if ( (tnsAdmin == null)|| (string.Empty.Equals(tnsAdmin)) )
- //{
- // System.Environment.SetEnvironmentVariable("TNS_ADMIN", "~/instantclient");
- //}
- }
- public static decimal InsertBlob(OracleConnection con)
- {
- byte[] ByteArray = new byte[2000]; // test Blob data
- byte j = 0;
- for (int i = 0; i < ByteArray.Length; i++) {
- ByteArray[i] = j;
- if (j > 255)
- j = 0;
- j++;
- }
- Console.WriteLine("Test Blob Data beginning: " + GetHexString (ByteArray));
- decimal retVal = -1;
- string sproc = "MyPackage" + ".InsertBlob";
- OracleCommand cmd = new OracleCommand();
- cmd.CommandText = sproc;
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Connection = con;
- //cmd.Connection.Open();
- cmd.Transaction = cmd.Connection.BeginTransaction();
- try {
- OracleParameter p1 = new OracleParameter("i_Sig_File", OracleType.Blob);
- p1.Direction = ParameterDirection.Input;
- //EXCEPTION thrown here
- //p1.Value = GetOracleLob(cmd.Transaction, ByteArray);
- OracleLob lob2 = GetOracleLob(cmd.Transaction, ByteArray);
- byte[] b2 = (byte[]) lob2.Value;
- Console.WriteLine("Test Blob Data here: " + GetHexString (b2));
- p1.Value = lob2.Value;
- //p1.Value = ByteArray;
- cmd.Parameters.Add(p1);
- cmd.ExecuteNonQuery();
- cmd.Transaction.Commit();
-
- OracleCommand select = con.CreateCommand ();
- //select.Transaction = transaction;
- select.CommandText = "SELECT BLOB_COLUMN FROM BLOBTEST2";
- Console.WriteLine (" SELECTING A BLOB (Binary) VALUE FROM BLOBTEST2");
- OracleDataReader reader = select.ExecuteReader ();
- if (!reader.Read ())
- Console.WriteLine ("ERROR: RECORD NOT FOUND");
- Console.WriteLine (" TESTING OracleLob OBJECT ...");
- if (reader.IsDBNull(0))
- Console.WriteLine("Lob IsNull");
- else {
- OracleLob lob = reader.GetOracleLob (0);
- if (lob == OracleLob.Null)
- Console.WriteLine("Lob is OracleLob.Null");
- else {
- byte[] blob = (byte[]) lob.Value;
- string result = GetHexString(blob);
- Console.WriteLine("Blob result: " + result);
- if (ByteArrayCompare (ByteArray, blob))
- Console.WriteLine("ByteArray and blob are the same: good");
- else
- Console.WriteLine("ByteArray and blob are not the same: bad");
- }
- }
- }
- catch(Exception ex) {
- Console.WriteLine("I exploded:" + ex.ToString());
- cmd.Transaction.Rollback();
- }
- return retVal;
- }
- private static OracleLob GetOracleLob(OracleTransaction transaction, byte[] blob)
- {
- string BLOB_CREATE = "DECLARE dpBlob BLOB; "
- + "BEGIN "
- + " DBMS_LOB.CREATETEMPORARY(dpBlob , False, 0); "
- + " :tempBlob := dpBlob; "
- + "END;";
- OracleLob tempLob = OracleLob.Null;
- if (blob != null)
- {
- // Create a new command using the same connection
- OracleCommand command = transaction.Connection.CreateCommand();
- // Assign the transaction to the command
- command.Transaction = transaction;
- // Create blob storage on the Oracle server
- command.CommandText = BLOB_CREATE;
- // Add a new output paramter to accept the blob storage reference
- OracleParameter parm = new OracleParameter("tempBlob", OracleType.Blob);
- parm.Direction = ParameterDirection.Output;
- command.Parameters.Add(parm);
- // command.Parameters.Add(
- // new OracleParameter("tempBlob", OracleType.Blob)).Direction =
- // ParameterDirection.Output;
- // Fire as your guns bear...
- command.ExecuteNonQuery();
- // Retrieve the blob stream from the OracleLob parameter
- //tempLob = (OracleLob)command.Parameters[0].Value;
- tempLob = (OracleLob) parm.Value;
- // Prevent server side events from firing while we write to the stream
- tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
- // Write bytes to the stream
- tempLob.Write(blob, 0, blob.Length);
-
- // Resume firing server events
- tempLob.EndBatch();
- }
- return tempLob;
- }
- static void SetupMyPackage(OracleConnection con)
- {
- Console.WriteLine("Setup Oracle package curspkg_join...");
-
- Console.WriteLine (" Drop table BLOBTEST2 ...");
- try {
- OracleCommand cmd2 = con.CreateCommand ();
- //cmd2.Transaction = transaction;
- cmd2.CommandText = "DROP TABLE BLOBTEST2";
- cmd2.ExecuteNonQuery ();
- }
- catch (OracleException) {
- // ignore if table already exists
- }
- Console.WriteLine (" CREATE TABLE ...");
- OracleCommand create = con.CreateCommand ();
- //create.Transaction = transaction;
- create.CommandText = "CREATE TABLE BLOBTEST2 (BLOB_COLUMN BLOB)";
- create.ExecuteNonQuery ();
- create.CommandText = "commit";
- create.ExecuteNonQuery();
- Console.Error.WriteLine(" create or replace package MyPackage...");
- OracleCommand cmd = con.CreateCommand();
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE MyPackage AS\n" +
- " Procedure InsertBlob (i_Sig_File blob);\n" +
- "END MyPackage;";
- cmd.ExecuteNonQuery();
- Console.Error.WriteLine(" create or replace package body MyPackage...");
- cmd.CommandText =
- "CREATE OR REPLACE PACKAGE BODY MyPackage AS\n" +
- " Procedure InsertBlob (i_Sig_File blob)\n" +
- " IS\n" +
- " BEGIN\n" +
- " INSERT INTO BLOBTEST2 (BLOB_COLUMN) VALUES(i_Sig_File); " +
- " END InsertBlob; " +
- "END MyPackage;";
- cmd.ExecuteNonQuery();
- cmd.CommandText = "commit";
- cmd.ExecuteNonQuery();
- }
- static byte[] ByteArrayCombine (byte[] b1, byte[] b2)
- {
- if (b1 == null)
- b1 = new byte[0];
- if (b2 == null)
- b2 = new byte[0];
-
- byte[] bytes = new byte[b1.Length + b2.Length];
- int i = 0;
- for (int j = 0; j < b1.Length; j++) {
- bytes[i] = b1[j];
- i++;
- }
- for (int k = 0; k < b2.Length; k++) {
- bytes[i] = b2[k];
- i++;
- }
- return bytes;
- }
- static bool ByteArrayCompare(byte[] ba1, byte[] ba2)
- {
- if (ba1 == null && ba2 == null)
- return true;
- if (ba1 == null)
- return false;
- if (ba2 == null)
- return false;
- if (ba1.Length != ba2.Length)
- return false;
- // for (int i = 0; i < ba1.Length; i++)
- // {
- //Console.WriteLine("i: " + i.ToString() + " ba1: " + ba1[i].ToString() + " ba2: " + ba2[i].ToString());
- //}
- for (int i = 0; i < ba1.Length; i++)
- {
- if (ba1[i] != ba2[i])
- return false;
- }
- return true;
- }
- [STAThread]
- static void Main(string[] args)
- {
- if(args.Length != 3) {
- Console.WriteLine("Usage: mono TestOracleClient database userid password");
- return;
- }
- string connectionString = String.Format(
- "Data Source={0};" +
- "User ID={1};" +
- "Password={2}",
- args[0], args[1], args[2]);
- conStr = connectionString;
- OracleConnection con1 = new OracleConnection();
- ShowConnectionProperties (con1);
- con1.ConnectionString = connectionString;
- con1.InfoMessage += new OracleInfoMessageEventHandler (OnInfoMessage);
- con1.StateChange += new StateChangeEventHandler (OnStateChange);
- Console.WriteLine("Opening...");
- con1.Open ();
- Console.WriteLine("Opened.");
- ShowConnectionProperties (con1);
- InsertBlobTest (con1);
- Console.WriteLine ("Mono Oracle Test BEGIN ...");
- MonoTest (con1);
- Console.WriteLine ("Mono Oracle Test END ...");
- Wait ("");
-
- Console.WriteLine ("LOB Test BEGIN...");
- CLOBTest (con1);
- BLOBTest (con1);
- Console.WriteLine ("LOB Test END.");
- Wait ("");
- Console.WriteLine ("Read Simple Test BEGIN - scott.emp...");
- ReadSimpleTest(con1, "SELECT e.*, e.rowid FROM scott.emp e");
- Console.WriteLine ("Read Simple Test END - scott.emp");
- Wait ("");
-
- Console.WriteLine ("DataAdapter Test BEGIN...");
- DataAdapterTest(con1);
- Console.WriteLine ("DataAdapter Test END.");
- Wait ("");
- Console.WriteLine ("DataAdapter Test 2 BEGIN...");
- DataAdapterTest2(con1);
- Console.WriteLine ("DataAdapter Test 2 END.");
- Wait ("");
- Console.WriteLine ("Rollback Test BEGIN...");
- RollbackTest(con1);
- Console.WriteLine ("Rollback Test END.");
- Wait ("");
- Console.WriteLine ("Commit Test BEGIN...");
- CommitTest(con1);
- Console.WriteLine ("Commit Test END.");
- Wait ("");
- Console.WriteLine ("Parameter Test BEGIN...");
- ParameterTest(con1);
- ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE7");
- Console.WriteLine ("Parameter Test END.");
- Wait ("");
-
- Console.WriteLine ("Stored Proc Test 1 BEGIN...");
- StoredProcedureTest1 (con1);
- ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE1");
- Console.WriteLine ("Stored Proc Test 1 END...");
- Wait ("");
- Console.WriteLine ("Stored Proc Test 2 BEGIN...");
- StoredProcedureTest2 (con1);
- ReadSimpleTest(con1, "SELECT * FROM MONO_TEST_TABLE2");
- Console.WriteLine ("Stored Proc Test 2 END...");
- SetParameterOracleType (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 1 BEGIN...");
- OutParmTest1 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 1 END...");
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 2 BEGIN...");
- OutParmTest2 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 2 END...");
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 3 BEGIN...");
- OutParmTest3 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 3 END...");
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 4 BEGIN...");
- OutParmTest4 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 4 END...");
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 5 BEGIN...");
- OutParmTest5 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 5 END...");
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 6 BEGIN...");
- OutParmTest6 (con1);
- Console.WriteLine ("Out Parameter and PL/SQL Block Test 6 END...");
- Wait ("");
- Console.WriteLine ("Test a Non Query using Execute Reader BEGIN...");
- TestNonQueryUsingExecuteReader (con1);
- Console.WriteLine ("Test a Non Query using Execute Reader END...");
- Wait ("");
- Console.WriteLine ("Null Aggregate Warning BEGIN test...");
- NullAggregateTest (con1);
- Console.WriteLine ("Null Aggregate Warning END test...");
- Console.WriteLine ("Ref Cursor BEGIN tests...");
- RefCursorTests (con1);
- Console.WriteLine ("Ref Cursor END tests...");
- Console.WriteLine("Closing...");
- con1.Close ();
- Console.WriteLine("Closed.");
- conStr = conStr + ";pooling=true;min pool size=4;max pool size=" + MAX_CONNECTIONS.ToString ();
- ConnectionPoolingTest1 ();
ConnectionPoolingTest2 ();
- // Need to have an external authentication user setup in Linux and oracle
- // before running this test
- //ExternalAuthenticationTest();
- TestPersistSucurityInfo1();
- TestPersistSucurityInfo2();
- TestPersistSucurityInfo3();
- TestPersistSucurityInfo4();
- TestPersistSucurityInfo5();
- TestPersistSucurityInfo6();
-
- Console.WriteLine("Done.");
- }
- }
- }
|