SqlCommandTest.cs 95 KB

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