SqlCommandTest.cs 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796
  1. //
  2. // SqlCommandTest.cs - NUnit Test Cases for testing the
  3. // SqlCommand class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Sureshkumar T ([email protected])
  7. // Senganal T ([email protected])
  8. //
  9. // Copyright (c) 2004 Novell Inc., and the individuals listed
  10. // on the ChangeLog entries.
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Data;
  33. using System.Data.Common;
  34. using System.Data.SqlClient;
  35. #if NET_2_0
  36. using System.Data.Sql;
  37. #endif
  38. using System.Globalization;
  39. #if NET_2_0
  40. using System.Xml;
  41. #endif
  42. using NUnit.Framework;
  43. namespace MonoTests.System.Data.SqlClient
  44. {
  45. [TestFixture]
  46. [Category ("sqlserver")]
  47. public class SqlCommandTest
  48. {
  49. SqlConnection conn;
  50. SqlCommand cmd;
  51. string connectionString = ConnectionManager.Singleton.ConnectionString;
  52. static readonly decimal SMALLMONEY_MAX = 214748.3647m;
  53. static readonly decimal SMALLMONEY_MIN = -214748.3648m;
  54. [TearDown]
  55. public void TearDown ()
  56. {
  57. if (cmd != null) {
  58. cmd.Dispose ();
  59. cmd = null;
  60. }
  61. if (conn != null) {
  62. conn.Close ();
  63. conn = null;
  64. }
  65. }
  66. [Test] // ctor (String, SqlConnection, SqlTransaction)
  67. public void Constructor4 ()
  68. {
  69. string cmdText = "select @@version";
  70. SqlTransaction trans = null;
  71. SqlConnection connA = null;
  72. SqlConnection connB = null;
  73. // transaction from same connection
  74. try {
  75. connA = new SqlConnection (connectionString);
  76. connA.Open ();
  77. trans = connA.BeginTransaction ();
  78. cmd = new SqlCommand (cmdText, connA, trans);
  79. Assert.AreEqual (cmdText, cmd.CommandText, "#A1");
  80. Assert.AreEqual (30, cmd.CommandTimeout, "#A2");
  81. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#A3");
  82. Assert.AreSame (connA, cmd.Connection, "#A4");
  83. Assert.IsNull (cmd.Container, "#A5");
  84. Assert.IsTrue (cmd.DesignTimeVisible, "#A6");
  85. #if NET_2_0
  86. Assert.IsNull (cmd.Notification, "#A7");
  87. Assert.IsTrue (cmd.NotificationAutoEnlist, "#A8");
  88. #endif
  89. Assert.IsNotNull (cmd.Parameters, "#A9");
  90. Assert.AreEqual (0, cmd.Parameters.Count, "#A10");
  91. Assert.IsNull (cmd.Site, "#A11");
  92. Assert.AreSame (trans, cmd.Transaction, "#A12");
  93. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#A13");
  94. } finally {
  95. if (trans != null)
  96. trans.Dispose ();
  97. if (connA != null)
  98. connA.Close ();
  99. }
  100. // transaction from other connection
  101. try {
  102. connA = new SqlConnection (connectionString);
  103. connA.Open ();
  104. connB = new SqlConnection (connectionString);
  105. connB.Open ();
  106. trans = connB.BeginTransaction ();
  107. cmd = new SqlCommand (cmdText, connA, trans);
  108. Assert.AreEqual (cmdText, cmd.CommandText, "#B1");
  109. Assert.AreEqual (30, cmd.CommandTimeout, "#B2");
  110. Assert.AreEqual (CommandType.Text, cmd.CommandType, "#B3");
  111. Assert.AreSame (connA, cmd.Connection, "#B4");
  112. Assert.IsNull (cmd.Container, "#B5");
  113. Assert.IsTrue (cmd.DesignTimeVisible, "#B6");
  114. #if NET_2_0
  115. Assert.IsNull (cmd.Notification, "#B7");
  116. Assert.IsTrue (cmd.NotificationAutoEnlist, "#B8");
  117. #endif
  118. Assert.IsNotNull (cmd.Parameters, "#B9");
  119. Assert.AreEqual (0, cmd.Parameters.Count, "#B10");
  120. Assert.IsNull (cmd.Site, "#B11");
  121. Assert.AreSame (trans, cmd.Transaction, "#B12");
  122. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource, "#B13");
  123. } finally {
  124. if (trans != null)
  125. trans.Dispose ();
  126. if (connA != null)
  127. connA.Close ();
  128. }
  129. }
  130. [Test] // bug #341743
  131. public void Dispose_Connection_Disposed ()
  132. {
  133. IDbConnection conn = ConnectionManager.Singleton.Connection;
  134. ConnectionManager.Singleton.OpenConnection ();
  135. IDbCommand cmd = null;
  136. try {
  137. cmd = conn.CreateCommand ();
  138. cmd.CommandText = "SELECT 'a'";
  139. cmd.ExecuteNonQuery ();
  140. conn.Dispose ();
  141. Assert.AreSame (conn, cmd.Connection, "#1");
  142. cmd.Dispose ();
  143. Assert.AreSame (conn, cmd.Connection, "#2");
  144. } finally {
  145. if (cmd != null)
  146. cmd.Dispose ();
  147. ConnectionManager.Singleton.CloseConnection ();
  148. }
  149. }
  150. [Test]
  151. public void ExecuteScalar ()
  152. {
  153. conn = new SqlConnection (connectionString);
  154. cmd = new SqlCommand ("", conn);
  155. cmd.CommandText = "Select count(*) from numeric_family where id<=4";
  156. // Check the Return value for a Correct Query
  157. object result = 0;
  158. conn.Open ();
  159. result = cmd.ExecuteScalar ();
  160. Assert.AreEqual (4, (int) result, "#A1 Query Result returned is incorrect");
  161. cmd.CommandText = "select id , type_bit from numeric_family order by id asc";
  162. result = Convert.ToInt32 (cmd.ExecuteScalar ());
  163. Assert.AreEqual (1, result,
  164. "#A2 ExecuteScalar Should return (1,1) the result set");
  165. cmd.CommandText = "select id from numeric_family where id=-1";
  166. result = cmd.ExecuteScalar ();
  167. Assert.IsNull (result, "#A3 Null should be returned if result set is empty");
  168. // Check SqlException is thrown for Invalid Query
  169. cmd.CommandText = "select count* from numeric_family";
  170. try {
  171. result = cmd.ExecuteScalar ();
  172. Assert.Fail ("#B1");
  173. } catch (SqlException ex) {
  174. // Incorrect syntax near the keyword 'from'
  175. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
  176. Assert.AreEqual ((byte) 15, ex.Class, "#B3");
  177. Assert.IsNull (ex.InnerException, "#B4");
  178. Assert.IsNotNull (ex.Message, "#B5");
  179. Assert.IsTrue (ex.Message.IndexOf ("'from'") != -1, "#B6");
  180. Assert.AreEqual (156, ex.Number, "#B7");
  181. Assert.AreEqual ((byte) 1, ex.State, "#B8");
  182. }
  183. // Parameterized stored procedure calls
  184. int int_value = 20;
  185. string string_value = "output value changed";
  186. string return_value = "first column of first rowset";
  187. cmd.CommandText =
  188. "create procedure #tmp_executescalar_outparams " +
  189. " (@p1 int, @p2 int out, @p3 varchar(200) out) " +
  190. "as " +
  191. "select '" + return_value + "' as 'col1', @p1 as 'col2' " +
  192. "set @p2 = @p2 * 2 " +
  193. "set @p3 = N'" + string_value + "' " +
  194. "select 'second rowset' as 'col1', 2 as 'col2' " +
  195. "return 1";
  196. cmd.CommandType = CommandType.Text;
  197. cmd.ExecuteNonQuery ();
  198. cmd.CommandText = "#tmp_executescalar_outparams";
  199. cmd.CommandType = CommandType.StoredProcedure;
  200. SqlParameter p1 = new SqlParameter ();
  201. p1.ParameterName = "@p1";
  202. p1.Direction = ParameterDirection.Input;
  203. p1.DbType = DbType.Int32;
  204. p1.Value = int_value;
  205. cmd.Parameters.Add (p1);
  206. SqlParameter p2 = new SqlParameter ();
  207. p2.ParameterName = "@p2";
  208. p2.Direction = ParameterDirection.InputOutput;
  209. p2.DbType = DbType.Int32;
  210. p2.Value = int_value;
  211. cmd.Parameters.Add (p2);
  212. SqlParameter p3 = new SqlParameter ();
  213. p3.ParameterName = "@p3";
  214. p3.Direction = ParameterDirection.Output;
  215. p3.DbType = DbType.String;
  216. p3.Size = 200;
  217. cmd.Parameters.Add (p3);
  218. result = cmd.ExecuteScalar ();
  219. Assert.AreEqual (return_value, result, "#C1 ExecuteScalar Should return 'first column of first rowset'");
  220. Assert.AreEqual (int_value * 2, p2.Value, "#C2 ExecuteScalar should fill the parameter collection with the outputted values");
  221. Assert.AreEqual (string_value, p3.Value, "#C3 ExecuteScalar should fill the parameter collection with the outputted values");
  222. p3.Size = 0;
  223. p3.Value = null;
  224. try {
  225. cmd.ExecuteScalar ();
  226. Assert.Fail ("#D1 Query should throw System.InvalidOperationException due to size = 0 and value = null");
  227. } catch (InvalidOperationException ex) {
  228. // String[2]: the Size property has an invalid
  229. // size of 0
  230. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#D2");
  231. Assert.IsNull (ex.InnerException, "#D3");
  232. Assert.IsNotNull (ex.Message, "#D4");
  233. } finally {
  234. conn.Close ();
  235. }
  236. }
  237. [Test]
  238. public void ExecuteScalar_Connection_PendingTransaction ()
  239. {
  240. conn = new SqlConnection (connectionString);
  241. conn.Open ();
  242. using (SqlTransaction trans = conn.BeginTransaction ()) {
  243. cmd = new SqlCommand ("select @@version", conn);
  244. try {
  245. cmd.ExecuteScalar ();
  246. Assert.Fail ("#1");
  247. } catch (InvalidOperationException ex) {
  248. // ExecuteScalar requires the command
  249. // to have a transaction object when the
  250. // connection assigned to the command is
  251. // in a pending local transaction. The
  252. // Transaction property of the command
  253. // has not been initialized
  254. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  255. Assert.IsNull (ex.InnerException, "#3");
  256. Assert.IsNotNull (ex.Message, "#4");
  257. #if NET_2_0
  258. Assert.IsTrue (ex.Message.IndexOf ("ExecuteScalar") != -1, "#5:" + ex.Message);
  259. #else
  260. Assert.IsTrue (ex.Message.IndexOf ("Execute") != -1, "#5:" + ex.Message);
  261. #endif
  262. }
  263. }
  264. }
  265. [Test]
  266. public void ExecuteScalar_Query_Invalid ()
  267. {
  268. conn = new SqlConnection (connectionString);
  269. conn.Open ();
  270. cmd = new SqlCommand ("InvalidQuery", conn);
  271. try {
  272. cmd.ExecuteScalar ();
  273. Assert.Fail ("#1");
  274. } catch (SqlException ex) {
  275. // Could not find stored procedure 'InvalidQuery'
  276. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
  277. Assert.AreEqual ((byte) 16, ex.Class, "#3");
  278. Assert.IsNull (ex.InnerException, "#4");
  279. Assert.IsNotNull (ex.Message, "#5");
  280. Assert.IsTrue (ex.Message.IndexOf ("'InvalidQuery'") != -1, "#6");
  281. Assert.AreEqual (2812, ex.Number, "#7");
  282. Assert.AreEqual ((byte) 62, ex.State, "#8");
  283. }
  284. }
  285. [Test]
  286. public void ExecuteScalar_Transaction_NotAssociated ()
  287. {
  288. SqlTransaction trans = null;
  289. SqlConnection connA = null;
  290. SqlConnection connB = null;
  291. try {
  292. connA = new SqlConnection (connectionString);
  293. connA.Open ();
  294. connB = new SqlConnection (connectionString);
  295. connB.Open ();
  296. trans = connA.BeginTransaction ();
  297. cmd = new SqlCommand ("select @@version", connB, trans);
  298. try {
  299. cmd.ExecuteScalar ();
  300. Assert.Fail ("#A1");
  301. } catch (InvalidOperationException ex) {
  302. // The transaction object is not associated
  303. // with the connection object
  304. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  305. Assert.IsNull (ex.InnerException, "#A3");
  306. Assert.IsNotNull (ex.Message, "#A4");
  307. } finally {
  308. cmd.Dispose ();
  309. }
  310. cmd = new SqlCommand ("select @@version", connB);
  311. cmd.Transaction = trans;
  312. try {
  313. cmd.ExecuteScalar ();
  314. Assert.Fail ("#B1");
  315. } catch (InvalidOperationException ex) {
  316. // The transaction object is not associated
  317. // with the connection object
  318. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  319. Assert.IsNull (ex.InnerException, "#B3");
  320. Assert.IsNotNull (ex.Message, "#B4");
  321. } finally {
  322. cmd.Dispose ();
  323. }
  324. } finally {
  325. if (trans != null)
  326. trans.Dispose ();
  327. if (connA != null)
  328. connA.Close ();
  329. if (connB != null)
  330. connB.Close ();
  331. }
  332. }
  333. [Test]
  334. public void ExecuteScalar_Transaction_Only ()
  335. {
  336. SqlTransaction trans = null;
  337. conn = new SqlConnection (connectionString);
  338. conn.Open ();
  339. trans = conn.BeginTransaction ();
  340. cmd = new SqlCommand ("select @@version");
  341. cmd.Transaction = trans;
  342. try {
  343. cmd.ExecuteScalar ();
  344. Assert.Fail ("#1");
  345. } catch (InvalidOperationException ex) {
  346. // ExecuteScalar: Connection property has not
  347. // been initialized
  348. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  349. Assert.IsNull (ex.InnerException, "#3");
  350. Assert.IsNotNull (ex.Message, "#4");
  351. #if NET_2_0
  352. Assert.IsTrue (ex.Message.StartsWith ("ExecuteScalar:"), "#5");
  353. #else
  354. Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader:"), "#5");
  355. #endif
  356. } finally {
  357. trans.Dispose ();
  358. }
  359. }
  360. [Test]
  361. public void ExecuteNonQuery ()
  362. {
  363. conn = new SqlConnection (connectionString);
  364. conn.Open ();
  365. SqlTransaction trans = conn.BeginTransaction ();
  366. cmd = conn.CreateCommand ();
  367. cmd.Transaction = trans;
  368. int result = 0;
  369. try {
  370. cmd.CommandText = "Select id from numeric_family where id=1";
  371. result = cmd.ExecuteNonQuery ();
  372. Assert.AreEqual (-1, result, "#A1");
  373. cmd.CommandText = "Insert into numeric_family (id,type_int) values (100,200)";
  374. result = cmd.ExecuteNonQuery ();
  375. Assert.AreEqual (1, result, "#A2 One row shud be inserted");
  376. cmd.CommandText = "Update numeric_family set type_int=300 where id=100";
  377. result = cmd.ExecuteNonQuery ();
  378. Assert.AreEqual (1, result, "#A3 One row shud be updated");
  379. // Test Batch Commands
  380. cmd.CommandText = "Select id from numeric_family where id=1;";
  381. cmd.CommandText += "update numeric_family set type_int=10 where id=1000";
  382. cmd.CommandText += "update numeric_family set type_int=10 where id=100";
  383. result = cmd.ExecuteNonQuery ();
  384. Assert.AreEqual (1, result, "#A4 One row shud be updated");
  385. cmd.CommandText = "Delete from numeric_family where id=100";
  386. result = cmd.ExecuteNonQuery ();
  387. Assert.AreEqual (1, result, "#A5 One row shud be deleted");
  388. } finally {
  389. trans.Dispose ();
  390. }
  391. // Parameterized stored procedure calls
  392. int int_value = 20;
  393. string string_value = "output value changed";
  394. cmd.CommandText =
  395. "create procedure #tmp_executescalar_outparams " +
  396. " (@p1 int, @p2 int out, @p3 varchar(200) out) " +
  397. "as " +
  398. "select 'test' as 'col1', @p1 as 'col2' " +
  399. "set @p2 = @p2 * 2 " +
  400. "set @p3 = N'" + string_value + "' " +
  401. "select 'second rowset' as 'col1', 2 as 'col2' " +
  402. "return 1";
  403. cmd.CommandType = CommandType.Text;
  404. cmd.ExecuteNonQuery ();
  405. cmd.CommandText = "#tmp_executescalar_outparams";
  406. cmd.CommandType = CommandType.StoredProcedure;
  407. SqlParameter p1 = new SqlParameter ();
  408. p1.ParameterName = "@p1";
  409. p1.Direction = ParameterDirection.Input;
  410. p1.DbType = DbType.Int32;
  411. p1.Value = int_value;
  412. cmd.Parameters.Add (p1);
  413. SqlParameter p2 = new SqlParameter ("@p2", int_value);
  414. p2.Direction = ParameterDirection.InputOutput;
  415. cmd.Parameters.Add (p2);
  416. SqlParameter p3 = new SqlParameter ();
  417. p3.ParameterName = "@p3";
  418. p3.Direction = ParameterDirection.Output;
  419. p3.DbType = DbType.String;
  420. p3.Size = 200;
  421. cmd.Parameters.Add (p3);
  422. cmd.ExecuteNonQuery ();
  423. Assert.AreEqual (int_value * 2, p2.Value, "#B1");
  424. Assert.AreEqual (string_value, p3.Value, "#B2");
  425. }
  426. [Test]
  427. public void ExecuteNonQuery_Connection_PendingTransaction ()
  428. {
  429. conn = new SqlConnection (connectionString);
  430. conn.Open ();
  431. using (SqlTransaction trans = conn.BeginTransaction ()) {
  432. cmd = new SqlCommand ("select @@version", conn);
  433. try {
  434. cmd.ExecuteNonQuery ();
  435. Assert.Fail ("#1");
  436. } catch (InvalidOperationException ex) {
  437. // ExecuteNonQuery requires the command
  438. // to have a transaction object when the
  439. // connection assigned to the command is
  440. // in a pending local transaction. The
  441. // Transaction property of the command
  442. // has not been initialized
  443. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  444. Assert.IsNull (ex.InnerException, "#3");
  445. Assert.IsNotNull (ex.Message, "#4");
  446. #if NET_2_0
  447. Assert.IsTrue (ex.Message.IndexOf ("ExecuteNonQuery") != -1, "#5:" + ex.Message);
  448. #else
  449. Assert.IsTrue (ex.Message.IndexOf ("Execute") != -1, "#5:" + ex.Message);
  450. #endif
  451. }
  452. }
  453. }
  454. [Test]
  455. public void ExecuteNonQuery_Query_Invalid ()
  456. {
  457. conn = new SqlConnection (connectionString);
  458. conn.Open ();
  459. cmd = new SqlCommand ("select id1 from numeric_family", conn);
  460. try {
  461. cmd.ExecuteNonQuery ();
  462. Assert.Fail ("#A1");
  463. } catch (SqlException ex) {
  464. // Invalid column name 'id1'
  465. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#A2");
  466. Assert.AreEqual ((byte) 16, ex.Class, "#A3");
  467. Assert.IsNull (ex.InnerException, "#A4");
  468. Assert.IsNotNull (ex.Message, "#A5");
  469. Assert.IsTrue (ex.Message.IndexOf ("'id1'") != -1, "#A6");
  470. Assert.AreEqual (207, ex.Number, "#A7");
  471. Assert.AreEqual ((byte) 1, ex.State, "#A8");
  472. }
  473. // ensure connection is not closed after error
  474. int result;
  475. cmd.CommandText = "INSERT INTO numeric_family (id, type_int) VALUES (6100, 200)";
  476. result = cmd.ExecuteNonQuery ();
  477. Assert.AreEqual (1, result, "#B1");
  478. cmd.CommandText = "DELETE FROM numeric_family WHERE id = 6100";
  479. result = cmd.ExecuteNonQuery ();
  480. Assert.AreEqual (1, result, "#B1");
  481. }
  482. [Test]
  483. public void ExecuteNonQuery_Transaction_NotAssociated ()
  484. {
  485. SqlTransaction trans = null;
  486. SqlConnection connA = null;
  487. SqlConnection connB = null;
  488. try {
  489. connA = new SqlConnection (connectionString);
  490. connA.Open ();
  491. connB = new SqlConnection (connectionString);
  492. connB.Open ();
  493. trans = connA.BeginTransaction ();
  494. cmd = new SqlCommand ("select @@version", connB, trans);
  495. try {
  496. cmd.ExecuteNonQuery ();
  497. Assert.Fail ("#A1");
  498. } catch (InvalidOperationException ex) {
  499. // The transaction object is not associated
  500. // with the connection object
  501. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  502. Assert.IsNull (ex.InnerException, "#A3");
  503. Assert.IsNotNull (ex.Message, "#A4");
  504. } finally {
  505. cmd.Dispose ();
  506. }
  507. cmd = new SqlCommand ("select @@version", connB);
  508. cmd.Transaction = trans;
  509. try {
  510. cmd.ExecuteNonQuery ();
  511. Assert.Fail ("#B1");
  512. } catch (InvalidOperationException ex) {
  513. // The transaction object is not associated
  514. // with the connection object
  515. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  516. Assert.IsNull (ex.InnerException, "#B3");
  517. Assert.IsNotNull (ex.Message, "#B4");
  518. } finally {
  519. cmd.Dispose ();
  520. }
  521. } finally {
  522. if (trans != null)
  523. trans.Dispose ();
  524. if (connA != null)
  525. connA.Close ();
  526. if (connB != null)
  527. connB.Close ();
  528. }
  529. }
  530. [Test]
  531. public void ExecuteNonQuery_Transaction_Only ()
  532. {
  533. conn = new SqlConnection (connectionString);
  534. conn.Open ();
  535. SqlTransaction trans = conn.BeginTransaction ();
  536. cmd = new SqlCommand ("select @@version");
  537. cmd.Transaction = trans;
  538. try {
  539. cmd.ExecuteNonQuery ();
  540. Assert.Fail ("#1");
  541. } catch (InvalidOperationException ex) {
  542. // ExecuteNonQuery: Connection property has not
  543. // been initialized
  544. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  545. Assert.IsNull (ex.InnerException, "#3");
  546. Assert.IsNotNull (ex.Message, "#4");
  547. Assert.IsTrue (ex.Message.StartsWith ("ExecuteNonQuery:"), "#5");
  548. } finally {
  549. trans.Dispose ();
  550. }
  551. }
  552. [Test] // bug #412569
  553. public void ExecuteReader ()
  554. {
  555. // Test for command behaviors
  556. DataTable schemaTable = null;
  557. SqlDataReader reader = null;
  558. conn = new SqlConnection (connectionString);
  559. conn.Open ();
  560. cmd = new SqlCommand ("", conn);
  561. cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
  562. cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
  563. // Test for default command behavior
  564. reader = cmd.ExecuteReader ();
  565. int rows = 0;
  566. int results = 0;
  567. do {
  568. while (reader.Read ())
  569. rows++;
  570. Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
  571. results++;
  572. rows = 0;
  573. } while (reader.NextResult ());
  574. Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
  575. reader.Close ();
  576. // Test if closing reader, closes the connection
  577. reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
  578. reader.Close ();
  579. Assert.AreEqual (ConnectionState.Closed, conn.State,
  580. "#3 Command Behavior is not followed");
  581. conn.Open ();
  582. // Test if row info and primary Key info is returned
  583. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  584. schemaTable = reader.GetSchemaTable ();
  585. Assert.IsTrue (reader.HasRows, "#4 Data Rows shud also be returned");
  586. Assert.IsTrue ((bool) schemaTable.Rows [0] ["IsKey"],
  587. "#5 Primary Key info shud be returned");
  588. reader.Close ();
  589. // Test only column information is returned
  590. reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
  591. schemaTable = reader.GetSchemaTable ();
  592. Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
  593. Assert.AreEqual (DBNull.Value, schemaTable.Rows [0] ["IsKey"],
  594. "#7 Primary Key info shud not be returned");
  595. Assert.AreEqual ("id", schemaTable.Rows [0] ["ColumnName"],
  596. "#8 Schema Data is Incorrect");
  597. reader.Close ();
  598. // Test only one result set (first) is returned
  599. reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
  600. schemaTable = reader.GetSchemaTable ();
  601. Assert.IsFalse (reader.NextResult (),
  602. "#9 Only one result set shud be returned");
  603. Assert.AreEqual ("id", schemaTable.Rows [0] ["ColumnName"],
  604. "#10 The result set returned shud be the first result set");
  605. reader.Close ();
  606. // Test only one row is returned for all result sets
  607. // msdotnet doesnt work correctly.. returns only one result set
  608. reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
  609. rows = 0;
  610. results = 0;
  611. do {
  612. while (reader.Read ())
  613. rows++;
  614. Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
  615. results++;
  616. rows = 0;
  617. } while (reader.NextResult ());
  618. // LAMESPEC:
  619. // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=357085
  620. Assert.AreEqual (1, results, "#12 Multiple result sets shud be returned");
  621. reader.Close ();
  622. }
  623. [Test]
  624. public void ExecuteReader_Connection_PendingTransaction ()
  625. {
  626. conn = new SqlConnection (connectionString);
  627. conn.Open ();
  628. using (SqlTransaction trans = conn.BeginTransaction ()) {
  629. cmd = new SqlCommand ("select @@version", conn);
  630. try {
  631. cmd.ExecuteReader ();
  632. Assert.Fail ("#1");
  633. } catch (InvalidOperationException ex) {
  634. // ExecuteReader requires the command
  635. // to have a transaction object when the
  636. // connection assigned to the command is
  637. // in a pending local transaction. The
  638. // Transaction property of the command
  639. // has not been initialized
  640. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  641. Assert.IsNull (ex.InnerException, "#3");
  642. Assert.IsNotNull (ex.Message, "#4");
  643. #if NET_2_0
  644. Assert.IsTrue (ex.Message.IndexOf ("ExecuteReader") != -1, "#5:" + ex.Message);
  645. #else
  646. Assert.IsTrue (ex.Message.IndexOf ("Execute") != -1, "#5:" + ex.Message);
  647. #endif
  648. }
  649. }
  650. }
  651. [Test]
  652. public void ExecuteReader_Query_Invalid ()
  653. {
  654. conn = new SqlConnection (connectionString);
  655. conn.Open ();
  656. cmd = new SqlCommand ("InvalidQuery", conn);
  657. try {
  658. cmd.ExecuteReader ();
  659. Assert.Fail ("#1");
  660. } catch (SqlException ex) {
  661. // Could not find stored procedure 'InvalidQuery'
  662. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#2");
  663. Assert.AreEqual ((byte) 16, ex.Class, "#3");
  664. Assert.IsNull (ex.InnerException, "#4");
  665. Assert.IsNotNull (ex.Message, "#5");
  666. Assert.IsTrue (ex.Message.IndexOf ("'InvalidQuery'") != -1, "#6");
  667. Assert.AreEqual (2812, ex.Number, "#7");
  668. Assert.AreEqual ((byte) 62, ex.State, "#8");
  669. }
  670. }
  671. [Test]
  672. public void ExecuteReader_Transaction_NotAssociated ()
  673. {
  674. SqlTransaction trans = null;
  675. SqlConnection connA = null;
  676. SqlConnection connB = null;
  677. try {
  678. connA = new SqlConnection (connectionString);
  679. connA.Open ();
  680. connB = new SqlConnection (connectionString);
  681. connB.Open ();
  682. trans = connA.BeginTransaction ();
  683. cmd = new SqlCommand ("select @@version", connB, trans);
  684. try {
  685. cmd.ExecuteReader ();
  686. Assert.Fail ("#A1");
  687. } catch (InvalidOperationException ex) {
  688. // The transaction object is not associated
  689. // with the connection object
  690. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  691. Assert.IsNull (ex.InnerException, "#A3");
  692. Assert.IsNotNull (ex.Message, "#A4");
  693. } finally {
  694. cmd.Dispose ();
  695. }
  696. cmd = new SqlCommand ("select @@version", connB);
  697. cmd.Transaction = trans;
  698. try {
  699. cmd.ExecuteReader ();
  700. Assert.Fail ("#B1");
  701. } catch (InvalidOperationException ex) {
  702. // The transaction object is not associated
  703. // with the connection object
  704. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  705. Assert.IsNull (ex.InnerException, "#B3");
  706. Assert.IsNotNull (ex.Message, "#B4");
  707. } finally {
  708. cmd.Dispose ();
  709. }
  710. } finally {
  711. if (trans != null)
  712. trans.Dispose ();
  713. if (connA != null)
  714. connA.Close ();
  715. if (connB != null)
  716. connB.Close ();
  717. }
  718. }
  719. [Test]
  720. public void ExecuteReader_Transaction_Only ()
  721. {
  722. SqlTransaction trans = null;
  723. conn = new SqlConnection (connectionString);
  724. conn.Open ();
  725. trans = conn.BeginTransaction ();
  726. cmd = new SqlCommand ("select @@version");
  727. cmd.Transaction = trans;
  728. try {
  729. cmd.ExecuteReader ();
  730. Assert.Fail ("#1");
  731. } catch (InvalidOperationException ex) {
  732. // ExecuteReader: Connection property has not
  733. // been initialized
  734. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  735. Assert.IsNull (ex.InnerException, "#3");
  736. Assert.IsNotNull (ex.Message, "#4");
  737. Assert.IsTrue (ex.Message.StartsWith ("ExecuteReader:"), "#5");
  738. } finally {
  739. trans.Dispose ();
  740. }
  741. }
  742. [Test]
  743. public void PrepareTest_CheckValidStatement ()
  744. {
  745. cmd = new SqlCommand ();
  746. conn = new SqlConnection (connectionString);
  747. conn.Open ();
  748. cmd.CommandText = "Select id from numeric_family where id=@ID";
  749. cmd.Connection = conn;
  750. // Test if Parameters are correctly populated
  751. cmd.Parameters.Clear ();
  752. cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
  753. cmd.Parameters ["@ID"].Value = 2;
  754. cmd.Prepare ();
  755. Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
  756. cmd.Parameters [0].Value = 3;
  757. Assert.AreEqual (3, cmd.ExecuteScalar (), "#4 Prep Stmt not working");
  758. conn.Close ();
  759. }
  760. [Test]
  761. public void Prepare ()
  762. {
  763. cmd = new SqlCommand ();
  764. conn = new SqlConnection (connectionString);
  765. conn.Open ();
  766. cmd.CommandText = "Select id from numeric_family where id=@ID";
  767. cmd.Connection = conn;
  768. // Test InvalidOperation Exception is thrown if Parameter Type
  769. // is not explicitly set
  770. #if NET_2_0
  771. cmd.Parameters.AddWithValue ("@ID", 2);
  772. #else
  773. cmd.Parameters.Add ("@ID", 2);
  774. #endif
  775. try {
  776. cmd.Prepare ();
  777. Assert.Fail ("#A1");
  778. } catch (InvalidOperationException ex) {
  779. // SqlCommand.Prepare method requires all parameters
  780. // to have an explicitly set type
  781. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  782. Assert.IsNull (ex.InnerException, "#A3");
  783. Assert.IsNotNull (ex.Message, "#A4");
  784. }
  785. // Test Exception is thrown for variable size data if precision/scale
  786. // is not set
  787. cmd.CommandText = "select type_varchar from string_family where type_varchar=@p1";
  788. cmd.Parameters.Clear ();
  789. cmd.Parameters.Add ("@p1", SqlDbType.VarChar);
  790. cmd.Parameters ["@p1"].Value = "afasasadadada";
  791. try {
  792. cmd.Prepare ();
  793. Assert.Fail ("#B1");
  794. } catch (InvalidOperationException ex) {
  795. // SqlCommand.Prepare method requires all variable
  796. // length parameters to have an explicitly set
  797. // non-zero Size
  798. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  799. Assert.IsNull (ex.InnerException, "#B3");
  800. Assert.IsNotNull (ex.Message, "#B4");
  801. }
  802. // Test Exception is not thrown for Stored Procs
  803. cmd.CommandType = CommandType.StoredProcedure;
  804. cmd.CommandText = "ABFSDSFSF";
  805. cmd.Prepare ();
  806. cmd.CommandType = CommandType.Text;
  807. conn.Close ();
  808. }
  809. [Test]
  810. public void Prepare_Connection_PendingTransaction ()
  811. {
  812. conn = new SqlConnection (connectionString);
  813. conn.Open ();
  814. using (SqlTransaction trans = conn.BeginTransaction ()) {
  815. // Text, without parameters
  816. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  817. cmd.Prepare ();
  818. // Text, with parameters
  819. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  820. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  821. try {
  822. cmd.Prepare ();
  823. Assert.Fail ("#1");
  824. } catch (InvalidOperationException ex) {
  825. // Prepare requires the command to have a
  826. // transaction object when the connection
  827. // assigned to the command is in a pending
  828. // local transaction. The Transaction
  829. // property of the command has not been
  830. // initialized
  831. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  832. Assert.IsNull (ex.InnerException, "#3");
  833. Assert.IsNotNull (ex.Message, "#4");
  834. #if NET_2_0
  835. Assert.IsTrue (ex.Message.IndexOf ("Prepare") != -1, "#5:" + ex.Message);
  836. #else
  837. Assert.IsTrue (ex.Message.IndexOf ("Execute") != -1, "#5:" + ex.Message);
  838. #endif
  839. }
  840. // Text, parameters cleared
  841. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  842. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  843. cmd.Parameters.Clear ();
  844. cmd.Prepare ();
  845. // StoredProcedure, without parameters
  846. cmd = new SqlCommand ("FindCustomer", conn);
  847. cmd.CommandType = CommandType.StoredProcedure;
  848. cmd.Prepare ();
  849. // StoredProcedure, with parameters
  850. cmd = new SqlCommand ("FindCustomer", conn);
  851. cmd.CommandType = CommandType.StoredProcedure;
  852. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  853. cmd.Prepare ();
  854. }
  855. }
  856. [Test]
  857. public void Prepare_Transaction_NotAssociated ()
  858. {
  859. SqlTransaction trans = null;
  860. SqlConnection connA = null;
  861. SqlConnection connB = null;
  862. try {
  863. connA = new SqlConnection (connectionString);
  864. connA.Open ();
  865. connB = new SqlConnection (connectionString);
  866. connB.Open ();
  867. trans = connA.BeginTransaction ();
  868. // Text, without parameters
  869. cmd = new SqlCommand ("select @@version", connB, trans);
  870. cmd.Transaction = trans;
  871. cmd.Prepare ();
  872. // Text, with parameters
  873. cmd = new SqlCommand ("select @@version", connB, trans);
  874. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  875. try {
  876. cmd.Prepare ();
  877. Assert.Fail ("#1");
  878. } catch (InvalidOperationException ex) {
  879. // The transaction is either not associated
  880. // with the current connection or has been
  881. // completed
  882. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  883. Assert.IsNull (ex.InnerException, "#3");
  884. Assert.IsNotNull (ex.Message, "#4");
  885. }
  886. // Text, parameters cleared
  887. cmd = new SqlCommand ("select @@version", connB, trans);
  888. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  889. cmd.Parameters.Clear ();
  890. cmd.Prepare ();
  891. // StoredProcedure, without parameters
  892. cmd = new SqlCommand ("FindCustomer", connB, trans);
  893. cmd.CommandType = CommandType.StoredProcedure;
  894. cmd.Prepare ();
  895. // StoredProcedure, with parameters
  896. cmd = new SqlCommand ("FindCustomer", connB, trans);
  897. cmd.CommandType = CommandType.StoredProcedure;
  898. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  899. cmd.Prepare ();
  900. } finally {
  901. if (trans != null)
  902. trans.Dispose ();
  903. if (connA != null)
  904. connA.Close ();
  905. if (connB != null)
  906. connB.Close ();
  907. }
  908. }
  909. [Test]
  910. public void Prepare_Transaction_Only ()
  911. {
  912. SqlTransaction trans = null;
  913. conn = new SqlConnection (connectionString);
  914. conn.Open ();
  915. trans = conn.BeginTransaction ();
  916. // Text, without parameters
  917. cmd = new SqlCommand ("select count(*) from whatever");
  918. cmd.Transaction = trans;
  919. #if NET_2_0
  920. try {
  921. cmd.Prepare ();
  922. Assert.Fail ("#A1");
  923. } catch (NullReferenceException) {
  924. }
  925. #else
  926. cmd.Prepare ();
  927. #endif
  928. // Text, with parameters
  929. cmd = new SqlCommand ("select count(*) from whatever");
  930. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  931. cmd.Transaction = trans;
  932. try {
  933. cmd.Prepare ();
  934. Assert.Fail ("#B1");
  935. #if NET_2_0
  936. } catch (NullReferenceException) {
  937. }
  938. #else
  939. } catch (InvalidOperationException ex) {
  940. // Prepare: Connection property has not been
  941. // initialized
  942. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  943. Assert.IsNull (ex.InnerException, "#B3");
  944. Assert.IsNotNull (ex.Message, "#B4");
  945. Assert.IsTrue (ex.Message.StartsWith ("Prepare:"), "#B5");
  946. }
  947. #endif
  948. // Text, parameters cleared
  949. cmd = new SqlCommand ("select count(*) from whatever");
  950. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  951. cmd.Parameters.Clear ();
  952. cmd.Transaction = trans;
  953. #if NET_2_0
  954. try {
  955. cmd.Prepare ();
  956. Assert.Fail ("#C1");
  957. } catch (NullReferenceException) {
  958. }
  959. #else
  960. cmd.Prepare ();
  961. #endif
  962. // StoredProcedure, without parameters
  963. cmd = new SqlCommand ("FindCustomer");
  964. cmd.CommandType = CommandType.StoredProcedure;
  965. cmd.Transaction = trans;
  966. #if NET_2_0
  967. try {
  968. cmd.Prepare ();
  969. Assert.Fail ("#D1");
  970. } catch (NullReferenceException) {
  971. }
  972. #else
  973. cmd.Prepare ();
  974. #endif
  975. // StoredProcedure, with parameters
  976. cmd = new SqlCommand ("FindCustomer");
  977. cmd.CommandType = CommandType.StoredProcedure;
  978. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  979. cmd.Transaction = trans;
  980. #if NET_2_0
  981. try {
  982. cmd.Prepare ();
  983. Assert.Fail ("#E1");
  984. } catch (NullReferenceException) {
  985. }
  986. #else
  987. cmd.Prepare ();
  988. #endif
  989. }
  990. [Test] // bug #412576
  991. public void Connection ()
  992. {
  993. SqlConnection connA = null;
  994. SqlConnection connB = null;
  995. SqlTransaction trans = null;
  996. try {
  997. connA = new SqlConnection (connectionString);
  998. connA.Open ();
  999. connB = new SqlConnection (connectionString);
  1000. connB.Open ();
  1001. cmd = connA.CreateCommand ();
  1002. cmd.Connection = connB;
  1003. Assert.AreSame (connB, cmd.Connection, "#A1");
  1004. Assert.IsNull (cmd.Transaction, "#A2");
  1005. cmd.Dispose ();
  1006. trans = connA.BeginTransaction ();
  1007. cmd = new SqlCommand ("select @@version", connA, trans);
  1008. cmd.Connection = connB;
  1009. Assert.AreSame (connB, cmd.Connection, "#B1");
  1010. Assert.AreSame (trans, cmd.Transaction, "#B2");
  1011. trans.Dispose ();
  1012. trans = connA.BeginTransaction ();
  1013. cmd = new SqlCommand ("select @@version", connA, trans);
  1014. trans.Rollback ();
  1015. Assert.AreSame (connA, cmd.Connection, "#C1");
  1016. Assert.IsNull (cmd.Transaction, "#C2");
  1017. cmd.Connection = connB;
  1018. Assert.AreSame (connB, cmd.Connection, "#C3");
  1019. Assert.IsNull (cmd.Transaction, "#C4");
  1020. trans = connA.BeginTransaction ();
  1021. cmd = new SqlCommand ("select @@version", connA, trans);
  1022. cmd.Connection = null;
  1023. Assert.IsNull (cmd.Connection, "#D1");
  1024. Assert.AreSame (trans, cmd.Transaction, "#D2");
  1025. } finally {
  1026. if (trans != null)
  1027. trans.Dispose ();
  1028. if (connA != null)
  1029. connA.Close ();
  1030. if (connB != null)
  1031. connB.Close ();
  1032. }
  1033. }
  1034. [Test]
  1035. public void Connection_Reader_Open ()
  1036. {
  1037. SqlConnection connA = null;
  1038. SqlConnection connB = null;
  1039. SqlTransaction trans = null;
  1040. try {
  1041. connA = new SqlConnection (connectionString);
  1042. connA.Open ();
  1043. connB = new SqlConnection (connectionString);
  1044. connB.Open ();
  1045. trans = connA.BeginTransaction ();
  1046. SqlCommand cmdA = new SqlCommand ("select @@version", connA, trans);
  1047. SqlCommand cmdB = new SqlCommand ("select @@version", connA, trans);
  1048. using (SqlDataReader reader = cmdB.ExecuteReader ()) {
  1049. #if NET_2_0
  1050. cmdA.Connection = connA;
  1051. Assert.AreSame (connA, cmdA.Connection, "#A1");
  1052. Assert.AreSame (trans, cmdA.Transaction, "#A2");
  1053. #else
  1054. try {
  1055. cmdA.Connection = connA;
  1056. Assert.Fail ("#A1");
  1057. } catch (InvalidOperationException ex) {
  1058. // The SqlCommand is currently busy
  1059. // Open, Fetching
  1060. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1061. Assert.IsNull (ex.InnerException, "#A3");
  1062. Assert.IsNotNull (ex.Message, "#A4");
  1063. Assert.AreSame (connA, cmdA.Connection, "#A5");
  1064. Assert.AreSame (trans, cmdA.Transaction, "#A6");
  1065. }
  1066. #endif
  1067. #if NET_2_0
  1068. cmdA.Connection = connB;
  1069. Assert.AreSame (connB, cmdA.Connection, "#B1");
  1070. Assert.AreSame (trans, cmdA.Transaction, "#B2");
  1071. #else
  1072. try {
  1073. cmdA.Connection = connB;
  1074. Assert.Fail ("#B1");
  1075. } catch (InvalidOperationException ex) {
  1076. // The SqlCommand is currently busy
  1077. // Open, Fetching
  1078. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1079. Assert.IsNull (ex.InnerException, "#B3");
  1080. Assert.IsNotNull (ex.Message, "#B4");
  1081. Assert.AreSame (connA, cmdA.Connection, "#B5");
  1082. Assert.AreSame (trans, cmdA.Transaction, "#B6");
  1083. }
  1084. #endif
  1085. #if NET_2_0
  1086. cmdA.Connection = null;
  1087. Assert.IsNull (cmdA.Connection, "#C1");
  1088. Assert.AreSame (trans, cmdA.Transaction, "#C2");
  1089. #else
  1090. try {
  1091. cmdA.Connection = null;
  1092. Assert.Fail ("#C1");
  1093. } catch (InvalidOperationException ex) {
  1094. // The SqlCommand is currently busy
  1095. // Open, Fetching
  1096. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  1097. Assert.IsNull (ex.InnerException, "#C3");
  1098. Assert.IsNotNull (ex.Message, "#C4");
  1099. Assert.AreSame (connA, cmdA.Connection, "#C5");
  1100. Assert.AreSame (trans, cmdA.Transaction, "#C6");
  1101. }
  1102. #endif
  1103. }
  1104. } finally {
  1105. if (trans != null)
  1106. trans.Dispose ();
  1107. if (connA != null)
  1108. connA.Close ();
  1109. if (connB != null)
  1110. connB.Close ();
  1111. }
  1112. }
  1113. [Test]
  1114. public void Transaction ()
  1115. {
  1116. SqlConnection connA = null;
  1117. SqlConnection connB = null;
  1118. SqlTransaction transA = null;
  1119. SqlTransaction transB = null;
  1120. try {
  1121. connA = new SqlConnection (connectionString);
  1122. connA.Open ();
  1123. connB = new SqlConnection (connectionString);
  1124. connB.Open ();
  1125. transA = connA.BeginTransaction ();
  1126. transB = connB.BeginTransaction ();
  1127. SqlCommand cmd = new SqlCommand ("select @@version", connA, transA);
  1128. cmd.Transaction = transA;
  1129. Assert.AreSame (connA, cmd.Connection, "#A1");
  1130. Assert.AreSame (transA, cmd.Transaction, "#A2");
  1131. cmd.Transaction = transB;
  1132. Assert.AreSame (connA, cmd.Connection, "#B1");
  1133. Assert.AreSame (transB, cmd.Transaction, "#B2");
  1134. cmd.Transaction = null;
  1135. Assert.AreSame (connA, cmd.Connection, "#C1");
  1136. Assert.IsNull (cmd.Transaction, "#C2");
  1137. } finally {
  1138. if (transA != null)
  1139. transA.Dispose ();
  1140. if (transB != null)
  1141. transA.Dispose ();
  1142. if (connA != null)
  1143. connA.Close ();
  1144. if (connB != null)
  1145. connB.Close ();
  1146. }
  1147. }
  1148. [Test] // bug #412579
  1149. public void Transaction_Reader_Open ()
  1150. {
  1151. SqlConnection connA = null;
  1152. SqlConnection connB = null;
  1153. SqlTransaction transA = null;
  1154. SqlTransaction transB = null;
  1155. try {
  1156. connA = new SqlConnection (connectionString);
  1157. connA.Open ();
  1158. connB = new SqlConnection (connectionString);
  1159. connB.Open ();
  1160. transA = connA.BeginTransaction ();
  1161. transB = connB.BeginTransaction ();
  1162. SqlCommand cmdA = new SqlCommand ("select * from employee", connA, transA);
  1163. SqlCommand cmdB = new SqlCommand ("select * from employee", connA, transA);
  1164. using (SqlDataReader reader = cmdB.ExecuteReader ()) {
  1165. #if NET_2_0
  1166. cmdA.Transaction = transA;
  1167. Assert.AreSame (transA, cmdA.Transaction, "#A1");
  1168. #else
  1169. try {
  1170. cmdA.Transaction = transA;
  1171. Assert.Fail ("#A1");
  1172. } catch (InvalidOperationException ex) {
  1173. // The SqlCommand is currently busy
  1174. // Open, Fetching
  1175. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1176. Assert.IsNull (ex.InnerException, "#A3");
  1177. Assert.IsNotNull (ex.Message, "#A4");
  1178. Assert.AreSame (transA, cmdA.Transaction, "#A5");
  1179. }
  1180. #endif
  1181. #if NET_2_0
  1182. cmdA.Transaction = transB;
  1183. Assert.AreSame (transB, cmdA.Transaction, "#B1");
  1184. #else
  1185. try {
  1186. cmdA.Transaction = transB;
  1187. Assert.Fail ("#B1");
  1188. } catch (InvalidOperationException ex) {
  1189. // The SqlCommand is currently busy
  1190. // Open, Fetching
  1191. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1192. Assert.IsNull (ex.InnerException, "#B3");
  1193. Assert.IsNotNull (ex.Message, "#B4");
  1194. Assert.AreSame (transA, cmdA.Transaction, "#B5");
  1195. }
  1196. #endif
  1197. #if NET_2_0
  1198. cmdA.Transaction = null;
  1199. Assert.IsNull (cmdA.Transaction, "#C1");
  1200. #else
  1201. try {
  1202. cmdA.Transaction = null;
  1203. Assert.Fail ("#C1");
  1204. } catch (InvalidOperationException ex) {
  1205. // The SqlCommand is currently busy
  1206. // Open, Fetching
  1207. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  1208. Assert.IsNull (ex.InnerException, "#C3");
  1209. Assert.IsNotNull (ex.Message, "#C4");
  1210. Assert.AreSame (transA, cmdA.Transaction, "#C5");
  1211. }
  1212. #endif
  1213. }
  1214. cmdA.Transaction = transA;
  1215. Assert.AreSame (transA, cmdA.Transaction, "#D1");
  1216. cmdA.Transaction = transB;
  1217. Assert.AreSame (transB, cmdA.Transaction, "#D2");
  1218. } finally {
  1219. if (transA != null)
  1220. transA.Dispose ();
  1221. if (transB != null)
  1222. transA.Dispose ();
  1223. if (connA != null)
  1224. connA.Close ();
  1225. if (connB != null)
  1226. connB.Close ();
  1227. }
  1228. }
  1229. [Test]
  1230. public void ExecuteNonQuery_StoredProcedure ()
  1231. {
  1232. SqlParameter param;
  1233. SqlCommand cmd = null;
  1234. SqlDataReader dr = null;
  1235. SqlParameter idParam;
  1236. SqlParameter dojParam;
  1237. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1238. conn.Open ();
  1239. // parameters with leading '@'
  1240. try {
  1241. // create temp sp here, should normally be created in Setup of test
  1242. // case, but cannot be done right now because of bug #68978
  1243. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  1244. cmd = conn.CreateCommand ();
  1245. cmd.CommandText = "#sp_temp_insert_employee";
  1246. cmd.CommandType = CommandType.StoredProcedure;
  1247. param = cmd.Parameters.Add ("@fname", SqlDbType.VarChar);
  1248. param.Value = "testA";
  1249. dojParam = cmd.Parameters.Add ("@doj", SqlDbType.DateTime);
  1250. dojParam.Direction = ParameterDirection.Output;
  1251. param = cmd.Parameters.Add ("@dob", SqlDbType.DateTime);
  1252. param.Value = new DateTime (2004, 8, 20);
  1253. idParam = cmd.Parameters.Add ("@id", SqlDbType.Int);
  1254. idParam.Direction = ParameterDirection.ReturnValue;
  1255. Assert.AreEqual (1, cmd.ExecuteNonQuery (), "#A1");
  1256. cmd.Dispose ();
  1257. cmd = conn.CreateCommand ();
  1258. cmd.CommandText = "select fname, dob, doj from employee where id = @id";
  1259. param = cmd.Parameters.Add ("@id", SqlDbType.Int);
  1260. param.Value = idParam.Value;
  1261. dr = cmd.ExecuteReader ();
  1262. Assert.IsTrue (dr.Read (), "#A2");
  1263. Assert.AreEqual (typeof (string), dr.GetFieldType (0), "#A3");
  1264. Assert.AreEqual ("testA", dr.GetValue (0), "#A4");
  1265. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (1), "#A5");
  1266. Assert.AreEqual (new DateTime (2004, 8, 20), dr.GetValue (1), "#A6");
  1267. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (2), "#A7");
  1268. Assert.AreEqual (dojParam.Value, dr.GetValue (2), "#A8");
  1269. Assert.IsFalse (dr.Read (), "#A9");
  1270. cmd.Dispose ();
  1271. dr.Close ();
  1272. } finally {
  1273. if (cmd != null)
  1274. cmd.Dispose ();
  1275. if (dr != null)
  1276. dr.Close ();
  1277. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  1278. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  1279. conn.Close ();
  1280. }
  1281. conn.Open ();
  1282. // parameters without leading '@'
  1283. try {
  1284. // create temp sp here, should normally be created in Setup of test
  1285. // case, but cannot be done right now because of bug #68978
  1286. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  1287. cmd = conn.CreateCommand ();
  1288. cmd.CommandText = "#sp_temp_insert_employee";
  1289. cmd.CommandType = CommandType.StoredProcedure;
  1290. param = cmd.Parameters.Add ("fname", SqlDbType.VarChar);
  1291. param.Value = "testB";
  1292. dojParam = cmd.Parameters.Add ("doj", SqlDbType.DateTime);
  1293. dojParam.Direction = ParameterDirection.Output;
  1294. param = cmd.Parameters.Add ("dob", SqlDbType.DateTime);
  1295. param.Value = new DateTime (2004, 8, 20);
  1296. idParam = cmd.Parameters.Add ("id", SqlDbType.Int);
  1297. idParam.Direction = ParameterDirection.ReturnValue;
  1298. #if NET_2_0
  1299. Assert.AreEqual (1, cmd.ExecuteNonQuery (), "#B1");
  1300. cmd.Dispose ();
  1301. cmd = conn.CreateCommand ();
  1302. cmd.CommandText = "select fname, dob, doj from employee where id = @id";
  1303. param = cmd.Parameters.Add ("id", SqlDbType.Int);
  1304. param.Value = idParam.Value;
  1305. dr = cmd.ExecuteReader ();
  1306. Assert.IsTrue (dr.Read (), "#B2");
  1307. Assert.AreEqual (typeof (string), dr.GetFieldType (0), "#B3");
  1308. Assert.AreEqual ("testB", dr.GetValue (0), "#B4");
  1309. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (1), "#B5");
  1310. Assert.AreEqual (new DateTime (2004, 8, 20), dr.GetValue (1), "#B6");
  1311. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (2), "#B7");
  1312. Assert.AreEqual (dojParam.Value, dr.GetValue (2), "#B8");
  1313. Assert.IsFalse (dr.Read (), "#B9");
  1314. cmd.Dispose ();
  1315. dr.Close ();
  1316. #else
  1317. try {
  1318. cmd.ExecuteNonQuery ();
  1319. Assert.Fail ("#B1");
  1320. } catch (SqlException) {
  1321. // Procedure or Function '#sp_temp_insert_employee'
  1322. // expects parameter '@fname', which was not supplied
  1323. }
  1324. #endif
  1325. } finally {
  1326. if (cmd != null)
  1327. cmd.Dispose ();
  1328. if (dr != null)
  1329. dr.Close ();
  1330. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  1331. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  1332. conn.Close ();
  1333. }
  1334. }
  1335. [Test] // bug #319598
  1336. public void LongQueryTest ()
  1337. {
  1338. SqlConnection conn = new SqlConnection (
  1339. connectionString + ";Pooling=false");
  1340. using (conn) {
  1341. conn.Open ();
  1342. SqlCommand cmd = conn.CreateCommand ();
  1343. String value = new String ('a', 10000);
  1344. cmd.CommandText = String.Format ("Select '{0}'", value);
  1345. cmd.ExecuteNonQuery ();
  1346. }
  1347. }
  1348. [Test] // bug #319598
  1349. public void LongStoredProcTest ()
  1350. {
  1351. SqlConnection conn = new SqlConnection (
  1352. connectionString + ";Pooling=false");
  1353. using (conn) {
  1354. conn.Open ();
  1355. /*int size = conn.PacketSize;*/
  1356. SqlCommand cmd = conn.CreateCommand ();
  1357. // create a temp stored proc
  1358. cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
  1359. cmd.CommandText += "@p1 nvarchar (4000), ";
  1360. cmd.CommandText += "@p2 nvarchar (4000), ";
  1361. cmd.CommandText += "@p3 nvarchar (4000), ";
  1362. cmd.CommandText += "@p4 nvarchar (4000) out ";
  1363. cmd.CommandText += "As ";
  1364. cmd.CommandText += "Begin ";
  1365. cmd.CommandText += "Set @p4 = N'Hello' ";
  1366. cmd.CommandText += "Return 2 ";
  1367. cmd.CommandText += "End";
  1368. cmd.ExecuteNonQuery ();
  1369. //execute the proc
  1370. cmd.CommandType = CommandType.StoredProcedure;
  1371. cmd.CommandText = "#sp_tmp_long_params";
  1372. String value = new String ('a', 4000);
  1373. SqlParameter p1 = new SqlParameter ("@p1",
  1374. SqlDbType.NVarChar, 4000);
  1375. p1.Value = value;
  1376. SqlParameter p2 = new SqlParameter ("@p2",
  1377. SqlDbType.NVarChar, 4000);
  1378. p2.Value = value;
  1379. SqlParameter p3 = new SqlParameter ("@p3",
  1380. SqlDbType.NVarChar, 4000);
  1381. p3.Value = value;
  1382. SqlParameter p4 = new SqlParameter ("@p4",
  1383. SqlDbType.NVarChar, 4000);
  1384. p4.Direction = ParameterDirection.Output;
  1385. // for now, name shud be @RETURN_VALUE
  1386. // can be changed once RPC is implemented
  1387. SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  1388. p5.Direction = ParameterDirection.ReturnValue;
  1389. cmd.Parameters.Add (p1);
  1390. cmd.Parameters.Add (p2);
  1391. cmd.Parameters.Add (p3);
  1392. cmd.Parameters.Add (p4);
  1393. cmd.Parameters.Add (p5);
  1394. cmd.ExecuteNonQuery ();
  1395. Assert.AreEqual ("Hello", p4.Value, "#1");
  1396. Assert.AreEqual (2, p5.Value, "#2");
  1397. }
  1398. }
  1399. [Test] // bug #319694
  1400. public void DateTimeParameterTest ()
  1401. {
  1402. SqlConnection conn = new SqlConnection (connectionString);
  1403. using (conn) {
  1404. conn.Open ();
  1405. SqlCommand cmd = conn.CreateCommand ();
  1406. cmd.CommandText = "select * from datetime_family where type_datetime=@p1";
  1407. cmd.Parameters.Add ("@p1", SqlDbType.DateTime).Value = "10-10-2005";
  1408. // shudnt cause and exception
  1409. SqlDataReader rdr = cmd.ExecuteReader ();
  1410. rdr.Close ();
  1411. }
  1412. }
  1413. /**
  1414. * Verifies whether an enum value is converted to a numeric value when
  1415. * used as value for a numeric parameter (bug #66630)
  1416. */
  1417. [Test]
  1418. public void EnumParameterTest ()
  1419. {
  1420. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1421. try {
  1422. ConnectionManager.Singleton.OpenConnection ();
  1423. // create temp sp here, should normally be created in Setup of test
  1424. // case, but cannot be done right now because of ug #68978
  1425. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  1426. + "@Status smallint = 7"
  1427. + ")"
  1428. + "AS" + Environment.NewLine
  1429. + "BEGIN" + Environment.NewLine
  1430. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  1431. + "END");
  1432. SqlCommand cmd = new SqlCommand ("#Bug66630", conn);
  1433. cmd.CommandType = CommandType.StoredProcedure;
  1434. cmd.Parameters.Add ("@Status", SqlDbType.Int).Value = Status.Error;
  1435. using (SqlDataReader dr = cmd.ExecuteReader ()) {
  1436. // one record should be returned
  1437. Assert.IsTrue (dr.Read (), "EnumParameterTest#1");
  1438. // we should get two field in the result
  1439. Assert.AreEqual (2, dr.FieldCount, "EnumParameterTest#2");
  1440. // field 1
  1441. Assert.AreEqual ("int", dr.GetDataTypeName (0), "EnumParameterTest#3");
  1442. Assert.AreEqual (5, dr.GetInt32 (0), "EnumParameterTest#4");
  1443. // field 2
  1444. Assert.AreEqual ("smallint", dr.GetDataTypeName (1), "EnumParameterTest#5");
  1445. Assert.AreEqual ((short) Status.Error, dr.GetInt16 (1), "EnumParameterTest#6");
  1446. // only one record should be returned
  1447. Assert.IsFalse (dr.Read (), "EnumParameterTest#7");
  1448. }
  1449. } finally {
  1450. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  1451. " where name like '#temp_Bug66630' and type like 'P') " +
  1452. " drop procedure #temp_Bug66630; ");
  1453. ConnectionManager.Singleton.CloseConnection ();
  1454. }
  1455. }
  1456. [Test]
  1457. public void CloneTest ()
  1458. {
  1459. conn = new SqlConnection (connectionString);
  1460. conn.Open ();
  1461. SqlTransaction trans = conn.BeginTransaction ();
  1462. cmd = new SqlCommand ();
  1463. cmd.Connection = conn;
  1464. cmd.Transaction = trans;
  1465. SqlCommand clone = (((ICloneable) (cmd)).Clone ()) as SqlCommand;
  1466. Assert.AreSame (conn, clone.Connection);
  1467. Assert.AreSame (trans, clone.Transaction);
  1468. }
  1469. [Test]
  1470. public void StoredProc_NoParameterTest ()
  1471. {
  1472. string query = "create procedure #tmp_sp_proc as begin";
  1473. query += " select 'data' end";
  1474. SqlConnection conn = new SqlConnection (connectionString);
  1475. SqlCommand cmd = conn.CreateCommand ();
  1476. cmd.CommandText = query;
  1477. conn.Open ();
  1478. cmd.ExecuteNonQuery ();
  1479. cmd.CommandType = CommandType.StoredProcedure;
  1480. cmd.CommandText = "#tmp_sp_proc";
  1481. using (SqlDataReader reader = cmd.ExecuteReader ()) {
  1482. if (reader.Read ())
  1483. Assert.AreEqual ("data", reader.GetString (0), "#1");
  1484. else
  1485. Assert.Fail ("#2 Select shud return data");
  1486. }
  1487. conn.Close ();
  1488. }
  1489. [Test]
  1490. public void StoredProc_ParameterTest ()
  1491. {
  1492. string create_query = CREATE_TMP_SP_PARAM_TEST;
  1493. SqlConnection conn = new SqlConnection (connectionString);
  1494. conn.Open ();
  1495. SqlCommand cmd = conn.CreateCommand ();
  1496. int label = 0;
  1497. string error = string.Empty;
  1498. while (label != -1) {
  1499. try {
  1500. switch (label) {
  1501. case 0:
  1502. // Test BigInt Param
  1503. DBHelper.ExecuteNonQuery (conn,
  1504. String.Format (create_query, "bigint"));
  1505. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1506. Int64.MaxValue, Int64.MaxValue,
  1507. Int64.MaxValue, Int64.MaxValue);
  1508. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1509. Int64.MinValue, Int64.MinValue,
  1510. Int64.MinValue, Int64.MinValue);
  1511. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1512. DBNull.Value, DBNull.Value,
  1513. DBNull.Value, DBNull.Value);
  1514. break;
  1515. case 1:
  1516. // Test Binary Param
  1517. DBHelper.ExecuteNonQuery (conn,
  1518. String.Format (create_query, "binary(5)"));
  1519. rpc_helper_function (cmd, SqlDbType.Binary, 5,
  1520. new byte [] { 1, 2, 3, 4, 5 },
  1521. new byte [] { 1, 2, 3, 4, 5 },
  1522. new byte [] { 1, 2, 3, 4, 5 },
  1523. new byte [] { 1, 2, 3, 4, 5 });
  1524. /*
  1525. rpc_helper_function (cmd, SqlDbType.Binary, 5,
  1526. DBNull.Value, DBNull.Value,
  1527. DBNull.Value);
  1528. */
  1529. rpc_helper_function (cmd, SqlDbType.Binary, 2,
  1530. new byte [0],
  1531. new byte [] { 0, 0, 0, 0, 0 },
  1532. new byte [] { 0, 0 },
  1533. new byte [] { 0, 0 });
  1534. break;
  1535. case 2:
  1536. // Test Bit Param
  1537. DBHelper.ExecuteNonQuery (conn,
  1538. String.Format (create_query, "bit"));
  1539. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1540. true, true, true, true);
  1541. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1542. false, false, false, false);
  1543. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1544. DBNull.Value, DBNull.Value,
  1545. DBNull.Value, DBNull.Value);
  1546. break;
  1547. case 3:
  1548. // Testing Char
  1549. DBHelper.ExecuteNonQuery (conn,
  1550. String.Format (create_query, "char(10)"));
  1551. rpc_helper_function (cmd, SqlDbType.Char, 10,
  1552. "characters", "characters",
  1553. "characters", "characters");
  1554. /*
  1555. rpc_helper_function (cmd, SqlDbType.Char, 3,
  1556. "characters", "cha ",
  1557. "cha");
  1558. rpc_helper_function (cmd, SqlDbType.Char, 3,
  1559. string.Empty, " ",
  1560. " ");
  1561. */
  1562. rpc_helper_function (cmd, SqlDbType.Char, 5,
  1563. DBNull.Value, DBNull.Value,
  1564. DBNull.Value, DBNull.Value);
  1565. break;
  1566. case 4:
  1567. // Testing DateTime
  1568. DBHelper.ExecuteNonQuery (conn,
  1569. String.Format (create_query, "datetime"));
  1570. rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2079-06-06 23:59:00",
  1571. new DateTime (2079, 6, 6, 23, 59, 0),
  1572. new DateTime (2079, 6, 6, 23, 59, 0),
  1573. new DateTime (2079, 6, 6, 23, 59, 0));
  1574. rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2009-04-12 10:39:45",
  1575. new DateTime (2009, 4, 12, 10, 39, 45),
  1576. new DateTime (2009, 4, 12, 10, 39, 45),
  1577. new DateTime (2009, 4, 12, 10, 39, 45));
  1578. rpc_helper_function (cmd, SqlDbType.DateTime, 0,
  1579. DBNull.Value, DBNull.Value,
  1580. DBNull.Value, DBNull.Value);
  1581. break;
  1582. case 5:
  1583. // Test Decimal Param
  1584. DBHelper.ExecuteNonQuery (conn,
  1585. String.Format (create_query, "decimal(10,2)"));
  1586. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1587. 10.665m, 10.67m, 11m, 10.67m);
  1588. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1589. 0m, 0m, 0m, 0m);
  1590. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1591. -5.657m, -5.66m, -6m, -5.66m);
  1592. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1593. DBNull.Value, DBNull.Value,
  1594. DBNull.Value, DBNull.Value);
  1595. // conversion
  1596. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1597. AttributeTargets.Constructor,
  1598. 32.0m, 32m, 32m);
  1599. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1600. 4.325f, 4.33m, 4m, 4.33m);
  1601. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1602. 10.0d, 10.00m, 10m, 10m);
  1603. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1604. 10.665d, 10.67m, 11m, 10.67m);
  1605. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1606. -5.657d, -5.66m, -6m, -5.66m);
  1607. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1608. 4, 4m, 4m, 4m);
  1609. break;
  1610. case 6:
  1611. // Test Float Param
  1612. DBHelper.ExecuteNonQuery (conn,
  1613. String.Format (create_query, "float"));
  1614. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1615. 10.0, 10.0, 10.0, 10.0);
  1616. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1617. 10.54, 10.54, 10.54, 10.54);
  1618. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1619. 0, 0d, 0d, 0d);
  1620. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1621. -5.34, -5.34, -5.34, -5.34);
  1622. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1623. DBNull.Value, DBNull.Value,
  1624. DBNull.Value, DBNull.Value);
  1625. break;
  1626. case 7:
  1627. // Testing Image
  1628. /* NOT WORKING
  1629. DBHelper.ExecuteNonQuery (conn,
  1630. String.Format(create_query, "image"));
  1631. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1632. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1633. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1634. /* NOT WORKING*/
  1635. break;
  1636. case 8:
  1637. // Test Integer Param
  1638. DBHelper.ExecuteNonQuery (conn,
  1639. String.Format (create_query, "int"));
  1640. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1641. 10, 10, 10, 10);
  1642. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1643. 0, 0, 0, 0);
  1644. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1645. -5, -5, -5, -5);
  1646. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1647. int.MaxValue, int.MaxValue,
  1648. int.MaxValue, int.MaxValue);
  1649. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1650. int.MinValue, int.MinValue,
  1651. int.MinValue, int.MinValue);
  1652. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1653. DBNull.Value, DBNull.Value,
  1654. DBNull.Value, DBNull.Value);
  1655. break;
  1656. case 9:
  1657. // Test Money Param
  1658. DBHelper.ExecuteNonQuery (conn,
  1659. String.Format (create_query, "money"));
  1660. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1661. 10m, 10m, 10m, 10m);
  1662. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1663. 10.54, 10.54m, 10.54m, 10.54m);
  1664. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1665. 0, 0m, 0m, 0m);
  1666. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1667. -5.34, -5.34m, -5.34m, -5.34m);
  1668. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1669. 5.34, 5.34m, 5.34m, 5.34m);
  1670. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1671. -10.1234m, -10.1234m, -10.1234m,
  1672. -10.1234m);
  1673. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1674. 10.1234m, 10.1234m, 10.1234m,
  1675. 10.1234m);
  1676. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1677. -2000000000m, -2000000000m,
  1678. -2000000000m, -2000000000m);
  1679. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1680. 2000000000m, 2000000000m,
  1681. 2000000000m, 2000000000m);
  1682. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1683. -200000000.2345m, -200000000.2345m,
  1684. -200000000.2345m, -200000000.2345m);
  1685. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1686. 200000000.2345m, 200000000.2345m,
  1687. 200000000.2345m, 200000000.2345m);
  1688. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1689. DBNull.Value, DBNull.Value,
  1690. DBNull.Value, DBNull.Value);
  1691. // rounding tests
  1692. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1693. -200000000.234561m, -200000000.2346m,
  1694. -200000000.2346m, -200000000.2346m);
  1695. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1696. -200000000.234551m, -200000000.2346m,
  1697. -200000000.2346m, -200000000.2346m);
  1698. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1699. -200000000.234541m, -200000000.2345m,
  1700. -200000000.2345m, -200000000.2345m);
  1701. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1702. 200000000.234561m, 200000000.2346m,
  1703. 200000000.2346m, 200000000.2346m);
  1704. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1705. 200000000.234551m, 200000000.2346m,
  1706. 200000000.2346m, 200000000.2346m);
  1707. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1708. 200000000.234541m, 200000000.2345m,
  1709. 200000000.2345m, 200000000.2345m);
  1710. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1711. -200000000.234461m, -200000000.2345m,
  1712. -200000000.2345m, -200000000.2345m);
  1713. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1714. -200000000.234451m, -200000000.2345m,
  1715. -200000000.2345m, -200000000.2345m);
  1716. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1717. -200000000.234441m, -200000000.2344m,
  1718. -200000000.2344m, -200000000.2344m);
  1719. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1720. 200000000.234461m, 200000000.2345m,
  1721. 200000000.2345m, 200000000.2345m);
  1722. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1723. 200000000.234451m, 200000000.2345m,
  1724. 200000000.2345m, 200000000.2345m);
  1725. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1726. 200000000.234441m, 200000000.2344m,
  1727. 200000000.2344m, 200000000.2344m);
  1728. // FIXME: we round toward even in SqlParameter.ConvertToFrameworkType
  1729. /*
  1730. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1731. -200000000.234550m, -200000000.2346m, -200000000.2346m);
  1732. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1733. 200000000.234550m, 200000000.2346m, 200000000.2346m);
  1734. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1735. -200000000.234450m, -200000000.2345m, -200000000.2345m);
  1736. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1737. 200000000.234450m, 200000000.2345m, 200000000.2345m);
  1738. */
  1739. break;
  1740. case 23:
  1741. // Test NChar Param
  1742. DBHelper.ExecuteNonQuery (conn,
  1743. String.Format (create_query, "nchar(10)"));
  1744. rpc_helper_function (cmd, SqlDbType.NChar, 10,
  1745. "characters", "characters",
  1746. "characters", "characters");
  1747. rpc_helper_function (cmd, SqlDbType.NChar, 3,
  1748. "characters", "cha ",
  1749. "cha", "cha");
  1750. rpc_helper_function (cmd, SqlDbType.NChar, 3,
  1751. string.Empty, " ",
  1752. " ", " ");
  1753. /*
  1754. rpc_helper_function (cmd, SqlDbType.NChar, 5,
  1755. DBNull.Value, DBNull.Value,
  1756. DBNull.Value);
  1757. */
  1758. break;
  1759. case 10:
  1760. // Test NText Param
  1761. DBHelper.ExecuteNonQuery (conn,
  1762. String.Format (create_query, "ntext"));
  1763. /*
  1764. rpc_helper_function (cmd, SqlDbType.NText, 0, "ntext");
  1765. rpc_helper_function (cmd, SqlDbType.NText, 0, "");
  1766. rpc_helper_function (cmd, SqlDbType.NText, 0, null);
  1767. */
  1768. break;
  1769. case 11:
  1770. // Test NVarChar Param
  1771. DBHelper.ExecuteNonQuery (conn,
  1772. String.Format (create_query, "nvarchar(10)"));
  1773. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1774. "nvarchar", "nvarchar", "nvarchar",
  1775. "nvarchar");
  1776. rpc_helper_function (cmd, SqlDbType.NVarChar, 3,
  1777. "nvarchar", "nva", "nva", "nva");
  1778. /*
  1779. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1780. string.Empty, string.Empty, string.Empty);
  1781. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1782. DBNull.Value, DBNull.Value, DBNull.Value);
  1783. */
  1784. break;
  1785. case 12:
  1786. // Test Real Param
  1787. DBHelper.ExecuteNonQuery (conn,
  1788. String.Format (create_query, "real"));
  1789. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1790. 10m, 10f, 10f, 10f);
  1791. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1792. 10d, 10f, 10f, 10f);
  1793. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1794. 0, 0f, 0f, 0f);
  1795. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1796. 3.54d, 3.54f, 3.54f, 3.54f);
  1797. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1798. 10, 10f, 10f, 10f);
  1799. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1800. 10.5f, 10.5f, 10.5f, 10.5f);
  1801. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1802. 3.5d, 3.5f, 3.5f, 3.5f);
  1803. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1804. 4.54m, 4.54f, 4.54f, 4.54f);
  1805. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1806. -4.54m, -4.54f, -4.54f, -4.54f);
  1807. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1808. DBNull.Value, DBNull.Value,
  1809. DBNull.Value, DBNull.Value);
  1810. break;
  1811. case 13:
  1812. // Test SmallDateTime Param
  1813. DBHelper.ExecuteNonQuery (conn,
  1814. String.Format (create_query, "smalldatetime"));
  1815. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
  1816. "6/6/2079 11:59:00 PM",
  1817. new DateTime (2079, 6, 6, 23, 59, 0),
  1818. new DateTime (2079, 6, 6, 23, 59, 0),
  1819. new DateTime (2079, 6, 6, 23, 59, 0));
  1820. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
  1821. DBNull.Value, DBNull.Value,
  1822. DBNull.Value, DBNull.Value);
  1823. break;
  1824. case 14:
  1825. // Test SmallInt Param
  1826. DBHelper.ExecuteNonQuery (conn,
  1827. String.Format (create_query, "smallint"));
  1828. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1829. 10, (short) 10, (short) 10, (short) 10);
  1830. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1831. -10, (short) -10, (short) -10,
  1832. (short) -10);
  1833. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1834. short.MaxValue, short.MaxValue,
  1835. short.MaxValue, short.MaxValue);
  1836. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1837. short.MinValue, short.MinValue,
  1838. short.MinValue, short.MinValue);
  1839. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1840. DBNull.Value, DBNull.Value,
  1841. DBNull.Value, DBNull.Value);
  1842. break;
  1843. case 15:
  1844. // Test SmallMoney Param
  1845. DBHelper.ExecuteNonQuery (conn,
  1846. String.Format (create_query, "smallmoney"));
  1847. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1848. 10.0d, 10m, 10m, 10m);
  1849. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1850. 0, 0m, 0m, 0m);
  1851. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1852. 3.54d, 3.54m, 3.54m, 3.54m);
  1853. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1854. 10, 10m, 10m, 10m);
  1855. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1856. 10.5f, 10.5m, 10.5m, 10.5m);
  1857. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1858. 3.5d, 3.5m, 3.5m, 3.5m);
  1859. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1860. 4.54m, 4.54m, 4.54m, 4.54m);
  1861. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1862. -4.54m, -4.54m, -4.54m, -4.54m);
  1863. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1864. -214748.3648m, -214748.3648m,
  1865. -214748.3648m, -214748.3648m);
  1866. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1867. 214748.3647m, 214748.3647m, 214748.3647m,
  1868. 214748.3647m);
  1869. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1870. DBNull.Value, DBNull.Value, DBNull.Value,
  1871. DBNull.Value);
  1872. // rounding tests
  1873. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1874. -4.543361m, -4.5434m, -4.5434m, -4.5434m);
  1875. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1876. -4.543351m, -4.5434m, -4.5434m, -4.5434m);
  1877. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1878. -4.543341m, -4.5433m, -4.5433m, -4.5433m);
  1879. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1880. 4.543361m, 4.5434m, 4.5434m, 4.5434m);
  1881. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1882. 4.543351m, 4.5434m, 4.5434m, 4.5434m);
  1883. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1884. 4.543341m, 4.5433m, 4.5433m, 4.5433m);
  1885. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1886. -4.543261m, -4.5433m, -4.5433m, -4.5433m);
  1887. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1888. -4.543251m, -4.5433m, -4.5433m, -4.5433m);
  1889. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1890. -4.543241m, -4.5432m, -4.5432m, -4.5432m);
  1891. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1892. 4.543261m, 4.5433m, 4.5433m, 4.5433m);
  1893. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1894. 4.543251m, 4.5433m, 4.5433m, 4.5433m);
  1895. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1896. 4.543241m, 4.5432m, 4.5432m, 4.5432m);
  1897. // FIXME: we round toward even in SqlParameter.ConvertToFrameworkType
  1898. /*
  1899. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1900. -4.543350m, -4.5434m, -4.5434m);
  1901. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1902. 4.543350m, 4.5434m, 4.5434m);
  1903. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1904. -4.543250m, -4.5433m, -4.5433m);
  1905. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1906. 4.543250m, 4.5433m, 4.5433m);
  1907. */
  1908. break;
  1909. case 16:
  1910. // Test Text Param
  1911. DBHelper.ExecuteNonQuery (conn,
  1912. String.Format (create_query, "text"));
  1913. /*
  1914. rpc_helper_function (cmd, SqlDbType.Text, 0, "text");
  1915. rpc_helper_function (cmd, SqlDbType.Text, 0, "");
  1916. rpc_helper_function (cmd, SqlDbType.Text, 0, null);
  1917. */
  1918. break;
  1919. case 17:
  1920. // Test TimeStamp Param
  1921. /* NOT WORKING
  1922. DBHelper.ExecuteNonQuery (conn,
  1923. String.Format(create_query,"timestamp"));
  1924. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  1925. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  1926. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, null);
  1927. */
  1928. break;
  1929. case 18:
  1930. // Test TinyInt Param
  1931. DBHelper.ExecuteNonQuery (conn,
  1932. String.Format (create_query, "tinyint"));
  1933. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  1934. 10.0d, (byte) 10, (byte) 10,
  1935. (byte) 10);
  1936. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  1937. 0, (byte) 0, (byte) 0, (byte) 0);
  1938. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  1939. byte.MaxValue, byte.MaxValue,
  1940. byte.MaxValue, byte.MaxValue);
  1941. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  1942. byte.MinValue, byte.MinValue,
  1943. byte.MinValue, byte.MinValue);
  1944. break;
  1945. case 19:
  1946. // Test UniqueIdentifier Param
  1947. /*
  1948. DBHelper.ExecuteNonQuery (conn,
  1949. String.Format(create_query,"uniqueidentifier"));
  1950. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, "0f159bf395b1d04f8c2ef5c02c3add96");
  1951. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, null);
  1952. */
  1953. break;
  1954. case 20:
  1955. // Test VarBinary Param
  1956. /* NOT WORKING
  1957. DBHelper.ExecuteNonQuery (conn,
  1958. String.Format(create_query,"varbinary (10)"));
  1959. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  1960. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  1961. rpc_helper_function (cmd, SqlDbType.VarBinary, 0, null);
  1962. */
  1963. break;
  1964. case 21:
  1965. // Test Varchar Param
  1966. DBHelper.ExecuteNonQuery (conn,
  1967. String.Format (create_query, "varchar(10)"));
  1968. rpc_helper_function (cmd, SqlDbType.VarChar, 7,
  1969. "VarChar", "VarChar", "VarChar",
  1970. "VarChar");
  1971. rpc_helper_function (cmd, SqlDbType.VarChar, 5,
  1972. "Var", "Var", "Var", "Var");
  1973. /*
  1974. rpc_helper_function (cmd, SqlDbType.VarChar, 3,
  1975. "Varchar", "Var", "Var");
  1976. rpc_helper_function (cmd, SqlDbType.VarChar, 10,
  1977. string.Empty, string.Empty, string.Empty);
  1978. rpc_helper_function (cmd, SqlDbType.VarChar, 10,
  1979. DBNull.Value, DBNull.Value,
  1980. DBNull.Value);
  1981. */
  1982. break;
  1983. case 22:
  1984. // Test Variant Param
  1985. /* NOT WORKING
  1986. DBHelper.ExecuteNonQuery (conn,
  1987. String.Format(create_query,"variant"));
  1988. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  1989. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  1990. rpc_helper_function (cmd, SqlDbType.Variant, 0, null);
  1991. */
  1992. break;
  1993. default:
  1994. label = -2;
  1995. break;
  1996. }
  1997. } catch (AssertionException ex) {
  1998. error += String.Format (" Case {0} INCORRECT VALUE : {1}\n", label, ex.ToString ());
  1999. } catch (Exception ex) {
  2000. error += String.Format (" Case {0} NOT WORKING : {1}\n", label, ex.ToString ());
  2001. }
  2002. label++;
  2003. if (label != -1)
  2004. DBHelper.ExecuteNonQuery (conn, string.Format (
  2005. CultureInfo.InvariantCulture,
  2006. DROP_STORED_PROCEDURE, "#tmp_sp_param_test"));
  2007. }
  2008. if (error.Length != 0)
  2009. Assert.Fail (error);
  2010. }
  2011. private void rpc_helper_function (SqlCommand cmd, SqlDbType type, int size, object input, object expectedRead, object expectedOut, object expectedInOut)
  2012. {
  2013. cmd.Parameters.Clear ();
  2014. SqlParameter param1, param2, param3;
  2015. if (size != 0) {
  2016. param1 = new SqlParameter ("@param1", type, size);
  2017. param2 = new SqlParameter ("@param2", type, size);
  2018. param3 = new SqlParameter ("@param3", type, size);
  2019. } else {
  2020. param1 = new SqlParameter ("@param1", type);
  2021. param2 = new SqlParameter ("@param2", type);
  2022. param3 = new SqlParameter ("@param3", type);
  2023. }
  2024. SqlParameter retval = new SqlParameter ("retval", SqlDbType.Int);
  2025. param1.Value = input;
  2026. param1.Direction = ParameterDirection.Input;
  2027. param2.Direction = ParameterDirection.Output;
  2028. param3.Direction = ParameterDirection.InputOutput;
  2029. param3.Value = input;
  2030. retval.Direction = ParameterDirection.ReturnValue;
  2031. cmd.Parameters.Add (param1);
  2032. cmd.Parameters.Add (param2);
  2033. cmd.Parameters.Add (param3);
  2034. cmd.Parameters.Add (retval);
  2035. cmd.CommandText = "#tmp_sp_param_test";
  2036. cmd.CommandType = CommandType.StoredProcedure;
  2037. using (SqlDataReader reader = cmd.ExecuteReader ()) {
  2038. Assert.IsTrue (reader.Read (), "#1");
  2039. AreEqual (expectedRead, reader.GetValue (0), "#2");
  2040. Assert.IsFalse (reader.Read (), "#3");
  2041. }
  2042. AreEqual (expectedOut, param2.Value, "#4");
  2043. AreEqual (expectedInOut, param3.Value, "#5");
  2044. Assert.AreEqual (5, retval.Value, "#6");
  2045. }
  2046. [Test]
  2047. public void OutputParamSizeTest1 ()
  2048. {
  2049. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2050. ConnectionManager.Singleton.OpenConnection ();
  2051. cmd = new SqlCommand ();
  2052. cmd.Connection = conn;
  2053. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2054. cmd.CommandType = CommandType.Text;
  2055. cmd.ExecuteNonQuery ();
  2056. cmd.CommandText = "#testsize";
  2057. cmd.CommandType = CommandType.StoredProcedure;
  2058. SqlParameter p1 = new SqlParameter ();
  2059. p1.ParameterName = "@p1";
  2060. p1.Direction = ParameterDirection.InputOutput;
  2061. p1.DbType = DbType.String;
  2062. p1.IsNullable = false;
  2063. cmd.Parameters.Add (p1);
  2064. try {
  2065. cmd.ExecuteNonQuery ();
  2066. Assert.Fail ("#1");
  2067. } catch (InvalidOperationException ex) {
  2068. // String[0]: the Size property has an invalid
  2069. // size of 0
  2070. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2071. Assert.IsNull (ex.InnerException, "#3");
  2072. Assert.IsNotNull (ex.Message, "#4");
  2073. }
  2074. }
  2075. [Test]
  2076. public void OutputParamSizeTest2 ()
  2077. {
  2078. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2079. ConnectionManager.Singleton.OpenConnection ();
  2080. cmd = new SqlCommand ();
  2081. cmd.Connection = conn;
  2082. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2083. cmd.CommandType = CommandType.Text;
  2084. cmd.ExecuteNonQuery ();
  2085. cmd.CommandText = "#testsize";
  2086. cmd.CommandType = CommandType.StoredProcedure;
  2087. SqlParameter p1 = new SqlParameter ();
  2088. p1.ParameterName = "@p1";
  2089. p1.Direction = ParameterDirection.Output;
  2090. p1.DbType = DbType.String;
  2091. p1.IsNullable = false;
  2092. cmd.Parameters.Add (p1);
  2093. try {
  2094. cmd.ExecuteNonQuery ();
  2095. Assert.Fail ("#1");
  2096. } catch (InvalidOperationException ex) {
  2097. // String[0]: the Size property has an invalid
  2098. // size of 0
  2099. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2100. Assert.IsNull (ex.InnerException, "#3");
  2101. Assert.IsNotNull (ex.Message, "#4");
  2102. }
  2103. }
  2104. [Test]
  2105. public void OutputParamSizeTest3 ()
  2106. {
  2107. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2108. ConnectionManager.Singleton.OpenConnection ();
  2109. cmd = new SqlCommand ();
  2110. cmd.Connection = conn;
  2111. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2112. cmd.CommandType = CommandType.Text;
  2113. cmd.ExecuteNonQuery ();
  2114. cmd.CommandText = "#testsize";
  2115. cmd.CommandType = CommandType.StoredProcedure;
  2116. SqlParameter p1 = new SqlParameter ();
  2117. p1.ParameterName = "@p1";
  2118. p1.Direction = ParameterDirection.InputOutput;
  2119. p1.DbType = DbType.String;
  2120. p1.IsNullable = true;
  2121. cmd.Parameters.Add (p1);
  2122. try {
  2123. cmd.ExecuteNonQuery ();
  2124. Assert.Fail ("#1");
  2125. } catch (InvalidOperationException ex) {
  2126. // String[0]: the Size property has an invalid
  2127. // size of 0
  2128. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2129. Assert.IsNull (ex.InnerException, "#3");
  2130. Assert.IsNotNull (ex.Message, "#4");
  2131. }
  2132. }
  2133. [Test]
  2134. public void OutputParamSizeTest4 ()
  2135. {
  2136. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2137. ConnectionManager.Singleton.OpenConnection ();
  2138. cmd = new SqlCommand ();
  2139. cmd.Connection = conn;
  2140. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2141. cmd.CommandType = CommandType.Text;
  2142. cmd.ExecuteNonQuery ();
  2143. cmd.CommandText = "#testsize";
  2144. cmd.CommandType = CommandType.StoredProcedure;
  2145. SqlParameter p1 = new SqlParameter ();
  2146. p1.ParameterName = "@p1";
  2147. p1.Direction = ParameterDirection.Output;
  2148. p1.DbType = DbType.String;
  2149. p1.IsNullable = true;
  2150. cmd.Parameters.Add (p1);
  2151. try {
  2152. cmd.ExecuteNonQuery ();
  2153. Assert.Fail ("#1");
  2154. } catch (InvalidOperationException ex) {
  2155. // String[0]: the Size property has an invalid
  2156. // size of 0
  2157. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2158. Assert.IsNull (ex.InnerException, "#3");
  2159. Assert.IsNotNull (ex.Message, "#4");
  2160. }
  2161. }
  2162. [Test]
  2163. public void SmallMoney_Overflow_Max ()
  2164. {
  2165. conn = new SqlConnection (connectionString);
  2166. conn.Open ();
  2167. DBHelper.ExecuteNonQuery (conn, string.Format (
  2168. CultureInfo.InvariantCulture, CREATE_TMP_SP_TYPE_TEST,
  2169. "SMALLMONEY"));
  2170. //decimal overflow = 214748.36471m;
  2171. decimal overflow = 214748.3648m;
  2172. cmd = conn.CreateCommand ();
  2173. cmd.CommandText = "#tmp_sp_type_test";
  2174. cmd.CommandType = CommandType.StoredProcedure;
  2175. SqlParameter param = cmd.Parameters.Add ("@param",
  2176. SqlDbType.SmallMoney);
  2177. param.Value = overflow;
  2178. try {
  2179. cmd.ExecuteScalar ();
  2180. Assert.Fail ("#1");
  2181. } catch (OverflowException ex) {
  2182. // SqlDbType.SmallMoney overflow. Value '214748.36471'
  2183. // is out of range. Must be between -214,748.3648 and 214,748.3647
  2184. Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#2");
  2185. Assert.IsNull (ex.InnerException, "#3");
  2186. Assert.IsNotNull (ex.Message, "#4");
  2187. #if NET_2_0
  2188. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2189. CultureInfo.InvariantCulture, "'{0}'",
  2190. overflow)) != -1, "#5:" + ex.Message);
  2191. #else
  2192. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2193. CultureInfo.CurrentCulture, "'{0}'",
  2194. overflow)) != -1, "#5:" + ex.Message);
  2195. #endif
  2196. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2197. CultureInfo.InvariantCulture, "{0:N4}",
  2198. SMALLMONEY_MIN)) != -1, "#6:" + ex.Message);
  2199. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2200. CultureInfo.InvariantCulture, "{0:N4}",
  2201. SMALLMONEY_MAX)) != -1, "#7:" + ex.Message);
  2202. } finally {
  2203. DBHelper.ExecuteNonQuery (conn, string.Format (
  2204. CultureInfo.InvariantCulture,
  2205. DROP_STORED_PROCEDURE, "#tmp_sp_type_test"));
  2206. }
  2207. }
  2208. [Test]
  2209. public void SmallMoney_Overflow_Min ()
  2210. {
  2211. conn = new SqlConnection (connectionString);
  2212. conn.Open ();
  2213. DBHelper.ExecuteNonQuery (conn, string.Format (
  2214. CultureInfo.InvariantCulture, CREATE_TMP_SP_TYPE_TEST,
  2215. "SMALLMONEY"));
  2216. //decimal overflow = -214748.36481m;
  2217. decimal overflow = -214748.3649m;
  2218. cmd = conn.CreateCommand ();
  2219. cmd.CommandText = "#tmp_sp_type_test";
  2220. cmd.CommandType = CommandType.StoredProcedure;
  2221. SqlParameter param = cmd.Parameters.Add ("@param",
  2222. SqlDbType.SmallMoney);
  2223. param.Value = overflow;
  2224. try {
  2225. cmd.ExecuteScalar ();
  2226. Assert.Fail ("#1");
  2227. } catch (OverflowException ex) {
  2228. // SqlDbType.SmallMoney overflow. Value '-214748,36481'
  2229. // is out of range. Must be between -214,748.3648 and 214,748.3647
  2230. Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#2");
  2231. Assert.IsNull (ex.InnerException, "#3");
  2232. Assert.IsNotNull (ex.Message, "#4");
  2233. #if NET_2_0
  2234. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2235. CultureInfo.InvariantCulture, "'{0}'",
  2236. overflow)) != -1, "#5:" + ex.Message);
  2237. #else
  2238. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2239. CultureInfo.CurrentCulture, "'{0}'",
  2240. overflow)) != -1, "#5:" + ex.Message);
  2241. #endif
  2242. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2243. CultureInfo.InvariantCulture, "{0:N4}",
  2244. SMALLMONEY_MIN)) != -1, "#6:" + ex.Message);
  2245. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2246. CultureInfo.InvariantCulture, "{0:N4}",
  2247. SMALLMONEY_MAX)) != -1, "#7:" + ex.Message);
  2248. } finally {
  2249. DBHelper.ExecuteNonQuery (conn, string.Format (
  2250. CultureInfo.InvariantCulture,
  2251. DROP_STORED_PROCEDURE, "#tmp_sp_type_test"));
  2252. }
  2253. }
  2254. #if NET_2_0
  2255. [Test]
  2256. public void NotificationTest ()
  2257. {
  2258. cmd = new SqlCommand ();
  2259. SqlNotificationRequest notification = new SqlNotificationRequest("MyNotification","MyService",15);
  2260. Assert.AreEqual (null, cmd.Notification, "#1 The default value for this property should be null");
  2261. cmd.Notification = notification;
  2262. Assert.AreEqual ("MyService", cmd.Notification.Options, "#2 The value should be MyService as the constructor is initiated with this value");
  2263. Assert.AreEqual (15, cmd.Notification.Timeout, "#2 The value should be 15 as the constructor is initiated with this value");
  2264. }
  2265. [Test]
  2266. public void NotificationAutoEnlistTest ()
  2267. {
  2268. cmd = new SqlCommand ();
  2269. Assert.AreEqual (true, cmd.NotificationAutoEnlist, "#1 Default value of the property should be true");
  2270. cmd.NotificationAutoEnlist = false;
  2271. Assert.AreEqual (false, cmd.NotificationAutoEnlist, "#2 The value of the property should be false after setting it to false");
  2272. }
  2273. [Test]
  2274. public void BeginExecuteXmlReaderTest ()
  2275. {
  2276. cmd = new SqlCommand ();
  2277. string connectionString1 = null;
  2278. connectionString1 = ConnectionManager.Singleton.ConnectionString + "Asynchronous Processing=true";
  2279. try {
  2280. SqlConnection conn1 = new SqlConnection (connectionString1);
  2281. conn1.Open ();
  2282. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
  2283. cmd.Connection = conn1;
  2284. IAsyncResult result = cmd.BeginExecuteXmlReader ();
  2285. XmlReader reader = cmd.EndExecuteXmlReader (result);
  2286. while (reader.Read ()) {
  2287. if (reader.LocalName.ToString () == "employee")
  2288. Assert.AreEqual ("kumar", reader["lname"], "#1 ");
  2289. }
  2290. } finally {
  2291. ConnectionManager.Singleton.CloseConnection ();
  2292. }
  2293. }
  2294. [Test]
  2295. public void BeginExecuteXmlReaderExceptionTest ()
  2296. {
  2297. cmd = new SqlCommand ();
  2298. try {
  2299. SqlConnection conn = new SqlConnection (connectionString);
  2300. conn.Open ();
  2301. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
  2302. cmd.Connection = conn;
  2303. try {
  2304. /*IAsyncResult result = */cmd.BeginExecuteXmlReader ();
  2305. } catch (InvalidOperationException) {
  2306. Assert.AreEqual (ConnectionManager.Singleton.ConnectionString, connectionString, "#1 Connection string has changed");
  2307. return;
  2308. }
  2309. Assert.Fail ("Expected Exception InvalidOperationException not thrown");
  2310. } finally {
  2311. ConnectionManager.Singleton.CloseConnection ();
  2312. }
  2313. }
  2314. #endif
  2315. [Test]
  2316. public void SqlCommandDisposeTest ()
  2317. {
  2318. IDataReader reader = null;
  2319. try {
  2320. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2321. ConnectionManager.Singleton.OpenConnection ();
  2322. IDbCommand command = conn.CreateCommand ();
  2323. try {
  2324. string sql = "SELECT * FROM employee";
  2325. command.CommandText = sql;
  2326. reader = command.ExecuteReader ();
  2327. } finally {
  2328. command.Dispose ();
  2329. }
  2330. while (reader.Read ()) ;
  2331. } finally {
  2332. reader.Dispose ();
  2333. ConnectionManager.Singleton.CloseConnection ();
  2334. }
  2335. }
  2336. private void bug326182_OutputParamMixupTestCommon (int paramOrder,
  2337. out int param0Val,
  2338. out int param1Val,
  2339. out int param2Val,
  2340. out int param3Val,
  2341. out int rvalVal)
  2342. {
  2343. try {
  2344. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2345. ConnectionManager.Singleton.OpenConnection ();
  2346. try {
  2347. SqlParameter param0 = new SqlParameter ("@param0", SqlDbType.Int);
  2348. param0.Direction = ParameterDirection.Output;
  2349. SqlParameter param1 = new SqlParameter ("@param1", SqlDbType.Int);
  2350. param1.Direction = ParameterDirection.Output;
  2351. SqlParameter param2 = new SqlParameter ("@param2", SqlDbType.Int);
  2352. param2.Direction = ParameterDirection.Output;
  2353. SqlParameter param3 = new SqlParameter ("@param3", SqlDbType.Int);
  2354. param3.Direction = ParameterDirection.Output;
  2355. SqlParameter rval = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  2356. rval.Direction = ParameterDirection.ReturnValue;
  2357. cmd = conn.CreateCommand ();
  2358. cmd.CommandText = "dbo.[sp_326182a]";
  2359. cmd.CommandType = CommandType.StoredProcedure;
  2360. switch (paramOrder) {
  2361. case 1: cmd.Parameters.Add (param0);
  2362. cmd.Parameters.Add (param1);
  2363. cmd.Parameters.Add (rval);
  2364. cmd.Parameters.Add (param2);
  2365. cmd.Parameters.Add (param3);
  2366. break;
  2367. case 2: cmd.Parameters.Add (rval);
  2368. cmd.Parameters.Add (param1);
  2369. cmd.Parameters.Add (param0);
  2370. cmd.Parameters.Add (param2);
  2371. cmd.Parameters.Add (param3);
  2372. break;
  2373. default: cmd.Parameters.Add (param0);
  2374. cmd.Parameters.Add (param1);
  2375. cmd.Parameters.Add (param2);
  2376. cmd.Parameters.Add (param3);
  2377. cmd.Parameters.Add (rval);
  2378. break;
  2379. }
  2380. cmd.ExecuteNonQuery ();
  2381. /* Copy the param values to variables, just in case if
  2382. * tests fail, we don't want the created sp to exist */
  2383. param3Val = (int) cmd.Parameters ["@param3"].Value;
  2384. param1Val = (int) cmd.Parameters ["@param1"].Value;
  2385. rvalVal = (int) cmd.Parameters ["@RETURN_VALUE"].Value;
  2386. param2Val = (int) cmd.Parameters ["@param2"].Value;
  2387. param0Val = (int) cmd.Parameters ["@param0"].Value;
  2388. } finally {
  2389. cmd.Dispose ();
  2390. cmd = null;
  2391. }
  2392. } finally {
  2393. ConnectionManager.Singleton.CloseConnection ();
  2394. conn = null;
  2395. }
  2396. }
  2397. [Test]
  2398. public void bug326182_OutputParamMixupTest_Normal ()
  2399. {
  2400. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2401. //param0Val = param1Val = param2Val = param3Val = rvalVal = 0;
  2402. bug326182_OutputParamMixupTestCommon (0, out param0Val, out param1Val,
  2403. out param2Val, out param3Val, out rvalVal);
  2404. Assert.AreEqual (103, param3Val);
  2405. Assert.AreEqual (101, param1Val);
  2406. Assert.AreEqual (2, rvalVal);
  2407. Assert.AreEqual (102, param2Val);
  2408. Assert.AreEqual (100, param0Val);
  2409. }
  2410. [Test]
  2411. public void bug326182_OutputParamMixupTest_RValInBetween ()
  2412. {
  2413. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2414. bug326182_OutputParamMixupTestCommon (1, out param0Val, out param1Val,
  2415. out param2Val, out param3Val, out rvalVal);
  2416. Assert.AreEqual (103, param3Val);
  2417. Assert.AreEqual (101, param1Val);
  2418. Assert.AreEqual (2, rvalVal);
  2419. Assert.AreEqual (102, param2Val);
  2420. Assert.AreEqual (100, param0Val);
  2421. }
  2422. [Test]
  2423. public void bug326182_OutputParamMixupTest_RValFirst ()
  2424. {
  2425. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2426. bug326182_OutputParamMixupTestCommon (2, out param0Val, out param1Val,
  2427. out param2Val, out param3Val, out rvalVal);
  2428. Assert.AreEqual (103, param3Val);
  2429. Assert.AreEqual (101, param1Val);
  2430. Assert.AreEqual (2, rvalVal);
  2431. Assert.AreEqual (102, param2Val);
  2432. Assert.AreEqual (100, param0Val);
  2433. }
  2434. // used as workaround for bugs in NUnit 2.2.0
  2435. static void AreEqual (object x, object y, string msg)
  2436. {
  2437. if (x == null && y == null)
  2438. return;
  2439. if ((x == null || y == null))
  2440. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2441. "Expected: {0}, but was: {1}. {2}",
  2442. x == null ? "<null>" : x, y == null ? "<null>" : y, msg));
  2443. bool isArrayX = x.GetType ().IsArray;
  2444. bool isArrayY = y.GetType ().IsArray;
  2445. if (isArrayX && isArrayY) {
  2446. Array arrayX = (Array) x;
  2447. Array arrayY = (Array) y;
  2448. if (arrayX.Length != arrayY.Length)
  2449. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2450. "Length of arrays differs. Expected: {0}, but was: {1}. {2}",
  2451. arrayX.Length, arrayY.Length, msg));
  2452. for (int i = 0; i < arrayX.Length; i++) {
  2453. object itemX = arrayX.GetValue (i);
  2454. object itemY = arrayY.GetValue (i);
  2455. if (!itemX.Equals (itemY))
  2456. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2457. "Arrays differ at position {0}. Expected: {1}, but was: {2}. {3}",
  2458. i, itemX, itemY, msg));
  2459. }
  2460. } else if (!x.Equals (y)) {
  2461. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2462. "Expected: {0} ({1}), but was: {2} ({3}). {4}",
  2463. x, x.GetType (), y, y.GetType (), msg));
  2464. }
  2465. }
  2466. private enum Status
  2467. {
  2468. OK = 0,
  2469. Error = 3
  2470. }
  2471. private readonly string CREATE_TMP_SP_PARAM_TEST =
  2472. "CREATE PROCEDURE #tmp_sp_param_test (" + Environment.NewLine +
  2473. " @param1 {0}," + Environment.NewLine +
  2474. " @param2 {0} output," + Environment.NewLine +
  2475. " @param3 {0} output)" + Environment.NewLine +
  2476. "AS" + Environment.NewLine +
  2477. "BEGIN" + Environment.NewLine +
  2478. " SELECT @param1" + Environment.NewLine +
  2479. " SET @param2=@param1" + Environment.NewLine +
  2480. " RETURN 5" + Environment.NewLine +
  2481. "END";
  2482. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  2483. "@fname varchar (20), " + Environment.NewLine +
  2484. "@dob datetime, " + Environment.NewLine +
  2485. "@doj datetime output " + Environment.NewLine +
  2486. ") " + Environment.NewLine +
  2487. "as " + Environment.NewLine +
  2488. "begin" + Environment.NewLine +
  2489. "declare @id int;" + Environment.NewLine +
  2490. "select @id = max (id) from employee;" + Environment.NewLine +
  2491. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  2492. "set @doj = getdate();" + Environment.NewLine +
  2493. "insert into employee (id, fname, dob, doj) values (@id, @fname, @dob, @doj);" + Environment.NewLine +
  2494. "return @id;" + Environment.NewLine +
  2495. "end");
  2496. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  2497. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  2498. "drop procedure #sp_temp_insert_employee; ");
  2499. private static readonly string CREATE_TMP_SP_TYPE_TEST =
  2500. "CREATE PROCEDURE #tmp_sp_type_test " +
  2501. "(" +
  2502. " @param {0}" +
  2503. ") AS SELECT @param";
  2504. private static readonly string DROP_STORED_PROCEDURE =
  2505. "DROP PROCEDURE {0}";
  2506. }
  2507. }