SqlCommandTest.cs 84 KB

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