SqlCommandTest.cs 88 KB

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