SqlCommandTest.cs 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110
  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 if DbType is set - #569543
  882. try {
  883. cmd.CommandText = "select type_guid from string_family where type_guid=@p1";
  884. cmd.Parameters.Clear ();
  885. cmd.Parameters.Add ("@p1", SqlDbType.UniqueIdentifier);
  886. cmd.Parameters ["@p1"].Value = new Guid ("1C47DD1D-891B-47E8-AAC8-F36608B31BC5");
  887. cmd.Prepare ();
  888. } catch (Exception ex) {
  889. Assert.Fail ("#B5 "+ex.Message);
  890. }
  891. // Test Exception is not thrown for Stored Procs
  892. cmd.CommandType = CommandType.StoredProcedure;
  893. cmd.CommandText = "ABFSDSFSF";
  894. cmd.Prepare ();
  895. cmd.CommandType = CommandType.Text;
  896. conn.Close ();
  897. }
  898. [Test]
  899. public void Prepare_Connection_PendingTransaction ()
  900. {
  901. conn = new SqlConnection (connectionString);
  902. conn.Open ();
  903. using (SqlTransaction trans = conn.BeginTransaction ()) {
  904. // Text, without parameters
  905. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  906. cmd.Prepare ();
  907. // Text, with parameters
  908. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  909. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  910. try {
  911. cmd.Prepare ();
  912. Assert.Fail ("#1");
  913. } catch (InvalidOperationException ex) {
  914. // Prepare requires the command to have a
  915. // transaction object when the connection
  916. // assigned to the command is in a pending
  917. // local transaction. The Transaction
  918. // property of the command has not been
  919. // initialized
  920. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  921. Assert.IsNull (ex.InnerException, "#3");
  922. Assert.IsNotNull (ex.Message, "#4");
  923. #if NET_2_0
  924. Assert.IsTrue (ex.Message.IndexOf ("Prepare") != -1, "#5:" + ex.Message);
  925. #else
  926. Assert.IsTrue (ex.Message.IndexOf ("Execute") != -1, "#5:" + ex.Message);
  927. #endif
  928. }
  929. // Text, parameters cleared
  930. cmd = new SqlCommand ("select * from whatever where name=?", conn);
  931. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  932. cmd.Parameters.Clear ();
  933. cmd.Prepare ();
  934. // StoredProcedure, without parameters
  935. cmd = new SqlCommand ("FindCustomer", conn);
  936. cmd.CommandType = CommandType.StoredProcedure;
  937. cmd.Prepare ();
  938. // StoredProcedure, with parameters
  939. cmd = new SqlCommand ("FindCustomer", conn);
  940. cmd.CommandType = CommandType.StoredProcedure;
  941. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  942. cmd.Prepare ();
  943. }
  944. }
  945. [Test]
  946. public void Prepare_Transaction_NotAssociated ()
  947. {
  948. SqlTransaction trans = null;
  949. SqlConnection connA = null;
  950. SqlConnection connB = null;
  951. try {
  952. connA = new SqlConnection (connectionString);
  953. connA.Open ();
  954. connB = new SqlConnection (connectionString);
  955. connB.Open ();
  956. trans = connA.BeginTransaction ();
  957. // Text, without parameters
  958. cmd = new SqlCommand ("select @@version", connB, trans);
  959. cmd.Transaction = trans;
  960. cmd.Prepare ();
  961. // Text, with parameters
  962. cmd = new SqlCommand ("select @@version", connB, trans);
  963. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  964. try {
  965. cmd.Prepare ();
  966. Assert.Fail ("#1");
  967. } catch (InvalidOperationException ex) {
  968. // The transaction is either not associated
  969. // with the current connection or has been
  970. // completed
  971. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  972. Assert.IsNull (ex.InnerException, "#3");
  973. Assert.IsNotNull (ex.Message, "#4");
  974. }
  975. // Text, parameters cleared
  976. cmd = new SqlCommand ("select @@version", connB, trans);
  977. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  978. cmd.Parameters.Clear ();
  979. cmd.Prepare ();
  980. // StoredProcedure, without parameters
  981. cmd = new SqlCommand ("FindCustomer", connB, trans);
  982. cmd.CommandType = CommandType.StoredProcedure;
  983. cmd.Prepare ();
  984. // StoredProcedure, with parameters
  985. cmd = new SqlCommand ("FindCustomer", connB, trans);
  986. cmd.CommandType = CommandType.StoredProcedure;
  987. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  988. cmd.Prepare ();
  989. } finally {
  990. if (trans != null)
  991. trans.Dispose ();
  992. if (connA != null)
  993. connA.Close ();
  994. if (connB != null)
  995. connB.Close ();
  996. }
  997. }
  998. [Test]
  999. public void Prepare_Transaction_Only ()
  1000. {
  1001. SqlTransaction trans = null;
  1002. conn = new SqlConnection (connectionString);
  1003. conn.Open ();
  1004. trans = conn.BeginTransaction ();
  1005. // Text, without parameters
  1006. cmd = new SqlCommand ("select count(*) from whatever");
  1007. cmd.Transaction = trans;
  1008. #if NET_2_0
  1009. try {
  1010. cmd.Prepare ();
  1011. Assert.Fail ("#A1");
  1012. } catch (NullReferenceException) {
  1013. }
  1014. #else
  1015. cmd.Prepare ();
  1016. #endif
  1017. // Text, with parameters
  1018. cmd = new SqlCommand ("select count(*) from whatever");
  1019. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  1020. cmd.Transaction = trans;
  1021. try {
  1022. cmd.Prepare ();
  1023. Assert.Fail ("#B1");
  1024. #if NET_2_0
  1025. } catch (NullReferenceException) {
  1026. }
  1027. #else
  1028. } catch (InvalidOperationException ex) {
  1029. // Prepare: Connection property has not been
  1030. // initialized
  1031. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1032. Assert.IsNull (ex.InnerException, "#B3");
  1033. Assert.IsNotNull (ex.Message, "#B4");
  1034. Assert.IsTrue (ex.Message.StartsWith ("Prepare:"), "#B5");
  1035. }
  1036. #endif
  1037. // Text, parameters cleared
  1038. cmd = new SqlCommand ("select count(*) from whatever");
  1039. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  1040. cmd.Parameters.Clear ();
  1041. cmd.Transaction = trans;
  1042. #if NET_2_0
  1043. try {
  1044. cmd.Prepare ();
  1045. Assert.Fail ("#C1");
  1046. } catch (NullReferenceException) {
  1047. }
  1048. #else
  1049. cmd.Prepare ();
  1050. #endif
  1051. // StoredProcedure, without parameters
  1052. cmd = new SqlCommand ("FindCustomer");
  1053. cmd.CommandType = CommandType.StoredProcedure;
  1054. cmd.Transaction = trans;
  1055. #if NET_2_0
  1056. try {
  1057. cmd.Prepare ();
  1058. Assert.Fail ("#D1");
  1059. } catch (NullReferenceException) {
  1060. }
  1061. #else
  1062. cmd.Prepare ();
  1063. #endif
  1064. // StoredProcedure, with parameters
  1065. cmd = new SqlCommand ("FindCustomer");
  1066. cmd.CommandType = CommandType.StoredProcedure;
  1067. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  1068. cmd.Transaction = trans;
  1069. #if NET_2_0
  1070. try {
  1071. cmd.Prepare ();
  1072. Assert.Fail ("#E1");
  1073. } catch (NullReferenceException) {
  1074. }
  1075. #else
  1076. cmd.Prepare ();
  1077. #endif
  1078. }
  1079. [Test] // bug #412576
  1080. public void Connection ()
  1081. {
  1082. SqlConnection connA = null;
  1083. SqlConnection connB = null;
  1084. SqlTransaction trans = null;
  1085. try {
  1086. connA = new SqlConnection (connectionString);
  1087. connA.Open ();
  1088. connB = new SqlConnection (connectionString);
  1089. connB.Open ();
  1090. cmd = connA.CreateCommand ();
  1091. cmd.Connection = connB;
  1092. Assert.AreSame (connB, cmd.Connection, "#A1");
  1093. Assert.IsNull (cmd.Transaction, "#A2");
  1094. cmd.Dispose ();
  1095. trans = connA.BeginTransaction ();
  1096. cmd = new SqlCommand ("select @@version", connA, trans);
  1097. cmd.Connection = connB;
  1098. Assert.AreSame (connB, cmd.Connection, "#B1");
  1099. Assert.AreSame (trans, cmd.Transaction, "#B2");
  1100. trans.Dispose ();
  1101. trans = connA.BeginTransaction ();
  1102. cmd = new SqlCommand ("select @@version", connA, trans);
  1103. trans.Rollback ();
  1104. Assert.AreSame (connA, cmd.Connection, "#C1");
  1105. Assert.IsNull (cmd.Transaction, "#C2");
  1106. cmd.Connection = connB;
  1107. Assert.AreSame (connB, cmd.Connection, "#C3");
  1108. Assert.IsNull (cmd.Transaction, "#C4");
  1109. trans = connA.BeginTransaction ();
  1110. cmd = new SqlCommand ("select @@version", connA, trans);
  1111. cmd.Connection = null;
  1112. Assert.IsNull (cmd.Connection, "#D1");
  1113. Assert.AreSame (trans, cmd.Transaction, "#D2");
  1114. } finally {
  1115. if (trans != null)
  1116. trans.Dispose ();
  1117. if (connA != null)
  1118. connA.Close ();
  1119. if (connB != null)
  1120. connB.Close ();
  1121. }
  1122. }
  1123. [Test]
  1124. public void Connection_Reader_Open ()
  1125. {
  1126. SqlConnection connA = null;
  1127. SqlConnection connB = null;
  1128. SqlTransaction trans = null;
  1129. try {
  1130. connA = new SqlConnection (connectionString);
  1131. connA.Open ();
  1132. connB = new SqlConnection (connectionString);
  1133. connB.Open ();
  1134. trans = connA.BeginTransaction ();
  1135. SqlCommand cmdA = new SqlCommand ("select @@version", connA, trans);
  1136. SqlCommand cmdB = new SqlCommand ("select @@version", connA, trans);
  1137. using (SqlDataReader reader = cmdB.ExecuteReader ()) {
  1138. #if NET_2_0
  1139. cmdA.Connection = connA;
  1140. Assert.AreSame (connA, cmdA.Connection, "#A1");
  1141. Assert.AreSame (trans, cmdA.Transaction, "#A2");
  1142. #else
  1143. try {
  1144. cmdA.Connection = connA;
  1145. Assert.Fail ("#A1");
  1146. } catch (InvalidOperationException ex) {
  1147. // The SqlCommand is currently busy
  1148. // Open, Fetching
  1149. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1150. Assert.IsNull (ex.InnerException, "#A3");
  1151. Assert.IsNotNull (ex.Message, "#A4");
  1152. Assert.AreSame (connA, cmdA.Connection, "#A5");
  1153. Assert.AreSame (trans, cmdA.Transaction, "#A6");
  1154. }
  1155. #endif
  1156. #if NET_2_0
  1157. cmdA.Connection = connB;
  1158. Assert.AreSame (connB, cmdA.Connection, "#B1");
  1159. Assert.AreSame (trans, cmdA.Transaction, "#B2");
  1160. #else
  1161. try {
  1162. cmdA.Connection = connB;
  1163. Assert.Fail ("#B1");
  1164. } catch (InvalidOperationException ex) {
  1165. // The SqlCommand is currently busy
  1166. // Open, Fetching
  1167. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1168. Assert.IsNull (ex.InnerException, "#B3");
  1169. Assert.IsNotNull (ex.Message, "#B4");
  1170. Assert.AreSame (connA, cmdA.Connection, "#B5");
  1171. Assert.AreSame (trans, cmdA.Transaction, "#B6");
  1172. }
  1173. #endif
  1174. #if NET_2_0
  1175. cmdA.Connection = null;
  1176. Assert.IsNull (cmdA.Connection, "#C1");
  1177. Assert.AreSame (trans, cmdA.Transaction, "#C2");
  1178. #else
  1179. try {
  1180. cmdA.Connection = null;
  1181. Assert.Fail ("#C1");
  1182. } catch (InvalidOperationException ex) {
  1183. // The SqlCommand is currently busy
  1184. // Open, Fetching
  1185. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  1186. Assert.IsNull (ex.InnerException, "#C3");
  1187. Assert.IsNotNull (ex.Message, "#C4");
  1188. Assert.AreSame (connA, cmdA.Connection, "#C5");
  1189. Assert.AreSame (trans, cmdA.Transaction, "#C6");
  1190. }
  1191. #endif
  1192. }
  1193. } finally {
  1194. if (trans != null)
  1195. trans.Dispose ();
  1196. if (connA != null)
  1197. connA.Close ();
  1198. if (connB != null)
  1199. connB.Close ();
  1200. }
  1201. }
  1202. [Test]
  1203. public void Transaction ()
  1204. {
  1205. SqlConnection connA = null;
  1206. SqlConnection connB = null;
  1207. SqlTransaction transA = null;
  1208. SqlTransaction transB = null;
  1209. try {
  1210. connA = new SqlConnection (connectionString);
  1211. connA.Open ();
  1212. connB = new SqlConnection (connectionString);
  1213. connB.Open ();
  1214. transA = connA.BeginTransaction ();
  1215. transB = connB.BeginTransaction ();
  1216. SqlCommand cmd = new SqlCommand ("select @@version", connA, transA);
  1217. cmd.Transaction = transA;
  1218. Assert.AreSame (connA, cmd.Connection, "#A1");
  1219. Assert.AreSame (transA, cmd.Transaction, "#A2");
  1220. cmd.Transaction = transB;
  1221. Assert.AreSame (connA, cmd.Connection, "#B1");
  1222. Assert.AreSame (transB, cmd.Transaction, "#B2");
  1223. cmd.Transaction = null;
  1224. Assert.AreSame (connA, cmd.Connection, "#C1");
  1225. Assert.IsNull (cmd.Transaction, "#C2");
  1226. } finally {
  1227. if (transA != null)
  1228. transA.Dispose ();
  1229. if (transB != null)
  1230. transA.Dispose ();
  1231. if (connA != null)
  1232. connA.Close ();
  1233. if (connB != null)
  1234. connB.Close ();
  1235. }
  1236. }
  1237. [Test] // bug #412579
  1238. public void Transaction_Reader_Open ()
  1239. {
  1240. SqlConnection connA = null;
  1241. SqlConnection connB = null;
  1242. SqlTransaction transA = null;
  1243. SqlTransaction transB = null;
  1244. try {
  1245. connA = new SqlConnection (connectionString);
  1246. connA.Open ();
  1247. connB = new SqlConnection (connectionString);
  1248. connB.Open ();
  1249. transA = connA.BeginTransaction ();
  1250. transB = connB.BeginTransaction ();
  1251. SqlCommand cmdA = new SqlCommand ("select * from employee", connA, transA);
  1252. SqlCommand cmdB = new SqlCommand ("select * from employee", connA, transA);
  1253. using (SqlDataReader reader = cmdB.ExecuteReader ()) {
  1254. #if NET_2_0
  1255. cmdA.Transaction = transA;
  1256. Assert.AreSame (transA, cmdA.Transaction, "#A1");
  1257. #else
  1258. try {
  1259. cmdA.Transaction = transA;
  1260. Assert.Fail ("#A1");
  1261. } catch (InvalidOperationException ex) {
  1262. // The SqlCommand is currently busy
  1263. // Open, Fetching
  1264. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  1265. Assert.IsNull (ex.InnerException, "#A3");
  1266. Assert.IsNotNull (ex.Message, "#A4");
  1267. Assert.AreSame (transA, cmdA.Transaction, "#A5");
  1268. }
  1269. #endif
  1270. #if NET_2_0
  1271. cmdA.Transaction = transB;
  1272. Assert.AreSame (transB, cmdA.Transaction, "#B1");
  1273. #else
  1274. try {
  1275. cmdA.Transaction = transB;
  1276. Assert.Fail ("#B1");
  1277. } catch (InvalidOperationException ex) {
  1278. // The SqlCommand is currently busy
  1279. // Open, Fetching
  1280. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  1281. Assert.IsNull (ex.InnerException, "#B3");
  1282. Assert.IsNotNull (ex.Message, "#B4");
  1283. Assert.AreSame (transA, cmdA.Transaction, "#B5");
  1284. }
  1285. #endif
  1286. #if NET_2_0
  1287. cmdA.Transaction = null;
  1288. Assert.IsNull (cmdA.Transaction, "#C1");
  1289. #else
  1290. try {
  1291. cmdA.Transaction = null;
  1292. Assert.Fail ("#C1");
  1293. } catch (InvalidOperationException ex) {
  1294. // The SqlCommand is currently busy
  1295. // Open, Fetching
  1296. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#C2");
  1297. Assert.IsNull (ex.InnerException, "#C3");
  1298. Assert.IsNotNull (ex.Message, "#C4");
  1299. Assert.AreSame (transA, cmdA.Transaction, "#C5");
  1300. }
  1301. #endif
  1302. }
  1303. cmdA.Transaction = transA;
  1304. Assert.AreSame (transA, cmdA.Transaction, "#D1");
  1305. cmdA.Transaction = transB;
  1306. Assert.AreSame (transB, cmdA.Transaction, "#D2");
  1307. } finally {
  1308. if (transA != null)
  1309. transA.Dispose ();
  1310. if (transB != null)
  1311. transA.Dispose ();
  1312. if (connA != null)
  1313. connA.Close ();
  1314. if (connB != null)
  1315. connB.Close ();
  1316. }
  1317. }
  1318. [Test]
  1319. public void ExecuteNonQuery_StoredProcedure ()
  1320. {
  1321. SqlParameter param;
  1322. SqlCommand cmd = null;
  1323. SqlDataReader dr = null;
  1324. SqlParameter idParam;
  1325. SqlParameter dojParam;
  1326. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1327. conn.Open ();
  1328. // parameters with leading '@'
  1329. try {
  1330. // create temp sp here, should normally be created in Setup of test
  1331. // case, but cannot be done right now because of bug #68978
  1332. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  1333. cmd = conn.CreateCommand ();
  1334. cmd.CommandText = "#sp_temp_insert_employee";
  1335. cmd.CommandType = CommandType.StoredProcedure;
  1336. param = cmd.Parameters.Add ("@fname", SqlDbType.VarChar);
  1337. param.Value = "testA";
  1338. dojParam = cmd.Parameters.Add ("@doj", SqlDbType.DateTime);
  1339. dojParam.Direction = ParameterDirection.Output;
  1340. param = cmd.Parameters.Add ("@dob", SqlDbType.DateTime);
  1341. param.Value = new DateTime (2004, 8, 20);
  1342. idParam = cmd.Parameters.Add ("@id", SqlDbType.Int);
  1343. idParam.Direction = ParameterDirection.ReturnValue;
  1344. Assert.AreEqual (1, cmd.ExecuteNonQuery (), "#A1");
  1345. cmd.Dispose ();
  1346. cmd = conn.CreateCommand ();
  1347. cmd.CommandText = "select fname, dob, doj from employee where id = @id";
  1348. param = cmd.Parameters.Add ("@id", SqlDbType.Int);
  1349. param.Value = idParam.Value;
  1350. dr = cmd.ExecuteReader ();
  1351. Assert.IsTrue (dr.Read (), "#A2");
  1352. Assert.AreEqual (typeof (string), dr.GetFieldType (0), "#A3");
  1353. Assert.AreEqual ("testA", dr.GetValue (0), "#A4");
  1354. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (1), "#A5");
  1355. Assert.AreEqual (new DateTime (2004, 8, 20), dr.GetValue (1), "#A6");
  1356. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (2), "#A7");
  1357. Assert.AreEqual (dojParam.Value, dr.GetValue (2), "#A8");
  1358. Assert.IsFalse (dr.Read (), "#A9");
  1359. cmd.Dispose ();
  1360. dr.Close ();
  1361. } finally {
  1362. if (cmd != null)
  1363. cmd.Dispose ();
  1364. if (dr != null)
  1365. dr.Close ();
  1366. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  1367. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  1368. conn.Close ();
  1369. }
  1370. conn.Open ();
  1371. // parameters without leading '@'
  1372. try {
  1373. // create temp sp here, should normally be created in Setup of test
  1374. // case, but cannot be done right now because of bug #68978
  1375. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  1376. cmd = conn.CreateCommand ();
  1377. cmd.CommandText = "#sp_temp_insert_employee";
  1378. cmd.CommandType = CommandType.StoredProcedure;
  1379. param = cmd.Parameters.Add ("fname", SqlDbType.VarChar);
  1380. param.Value = "testB";
  1381. dojParam = cmd.Parameters.Add ("doj", SqlDbType.DateTime);
  1382. dojParam.Direction = ParameterDirection.Output;
  1383. param = cmd.Parameters.Add ("dob", SqlDbType.DateTime);
  1384. param.Value = new DateTime (2004, 8, 20);
  1385. idParam = cmd.Parameters.Add ("id", SqlDbType.Int);
  1386. idParam.Direction = ParameterDirection.ReturnValue;
  1387. #if NET_2_0
  1388. Assert.AreEqual (1, cmd.ExecuteNonQuery (), "#B1");
  1389. cmd.Dispose ();
  1390. cmd = conn.CreateCommand ();
  1391. cmd.CommandText = "select fname, dob, doj from employee where id = @id";
  1392. param = cmd.Parameters.Add ("id", SqlDbType.Int);
  1393. param.Value = idParam.Value;
  1394. dr = cmd.ExecuteReader ();
  1395. Assert.IsTrue (dr.Read (), "#B2");
  1396. Assert.AreEqual (typeof (string), dr.GetFieldType (0), "#B3");
  1397. Assert.AreEqual ("testB", dr.GetValue (0), "#B4");
  1398. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (1), "#B5");
  1399. Assert.AreEqual (new DateTime (2004, 8, 20), dr.GetValue (1), "#B6");
  1400. Assert.AreEqual (typeof (DateTime), dr.GetFieldType (2), "#B7");
  1401. Assert.AreEqual (dojParam.Value, dr.GetValue (2), "#B8");
  1402. Assert.IsFalse (dr.Read (), "#B9");
  1403. cmd.Dispose ();
  1404. dr.Close ();
  1405. #else
  1406. try {
  1407. cmd.ExecuteNonQuery ();
  1408. Assert.Fail ("#B1");
  1409. } catch (SqlException ex) {
  1410. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
  1411. Assert.AreEqual ((byte) 16, ex.Class, "#B3");
  1412. Assert.IsNull (ex.InnerException, "#B4");
  1413. Assert.IsNotNull (ex.Message, "#B5");
  1414. Assert.IsTrue (ex.Message.IndexOf ("#sp_temp_insert_employee") != -1, "#B6:"+ ex.Message);
  1415. if (ClientVersion == 7) {
  1416. // fname is not a parameter for procedure #sp_temp_insert_employee
  1417. Assert.IsTrue (ex.Message.IndexOf ("fname") != -1, "#B7: " + ex.Message);
  1418. Assert.AreEqual (8145, ex.Number, "#B8");
  1419. Assert.AreEqual ((byte) 2, ex.State, "#B9");
  1420. } else {
  1421. // Procedure or Function '#sp_temp_insert_employee' expects
  1422. // parameter '@fname', which was not supplied
  1423. Assert.IsTrue (ex.Message.IndexOf ("'@fname'") != -1, "#B7: " + ex.Message);
  1424. Assert.AreEqual (201, ex.Number, "#B8");
  1425. Assert.AreEqual ((byte) 4, ex.State, "#B9");
  1426. }
  1427. }
  1428. #endif
  1429. } finally {
  1430. if (cmd != null)
  1431. cmd.Dispose ();
  1432. if (dr != null)
  1433. dr.Close ();
  1434. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  1435. DBHelper.ExecuteSimpleSP (conn, "sp_clean_employee_table");
  1436. conn.Close ();
  1437. }
  1438. }
  1439. [Test] // bug #319598
  1440. public void LongQueryTest ()
  1441. {
  1442. if (ClientVersion == 7)
  1443. Assert.Ignore ("Hangs on SQL Server 7.0");
  1444. SqlConnection conn = new SqlConnection (
  1445. connectionString + ";Pooling=false");
  1446. using (conn) {
  1447. conn.Open ();
  1448. SqlCommand cmd = conn.CreateCommand ();
  1449. String value = new String ('a', 10000);
  1450. cmd.CommandText = String.Format ("Select '{0}'", value);
  1451. cmd.ExecuteNonQuery ();
  1452. }
  1453. }
  1454. [Test] // bug #319598
  1455. public void LongStoredProcTest ()
  1456. {
  1457. if (ClientVersion == 7)
  1458. Assert.Ignore ("Hangs on SQL Server 7.0");
  1459. SqlConnection conn = new SqlConnection (
  1460. connectionString + ";Pooling=false");
  1461. using (conn) {
  1462. conn.Open ();
  1463. /*int size = conn.PacketSize;*/
  1464. SqlCommand cmd = conn.CreateCommand ();
  1465. // create a temp stored proc
  1466. cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
  1467. cmd.CommandText += "@p1 nvarchar (4000), ";
  1468. cmd.CommandText += "@p2 nvarchar (4000), ";
  1469. cmd.CommandText += "@p3 nvarchar (4000), ";
  1470. cmd.CommandText += "@p4 nvarchar (4000) out ";
  1471. cmd.CommandText += "As ";
  1472. cmd.CommandText += "Begin ";
  1473. cmd.CommandText += "Set @p4 = N'Hello' ";
  1474. cmd.CommandText += "Return 2 ";
  1475. cmd.CommandText += "End";
  1476. cmd.ExecuteNonQuery ();
  1477. //execute the proc
  1478. cmd.CommandType = CommandType.StoredProcedure;
  1479. cmd.CommandText = "#sp_tmp_long_params";
  1480. String value = new String ('a', 4000);
  1481. SqlParameter p1 = new SqlParameter ("@p1",
  1482. SqlDbType.NVarChar, 4000);
  1483. p1.Value = value;
  1484. SqlParameter p2 = new SqlParameter ("@p2",
  1485. SqlDbType.NVarChar, 4000);
  1486. p2.Value = value;
  1487. SqlParameter p3 = new SqlParameter ("@p3",
  1488. SqlDbType.NVarChar, 4000);
  1489. p3.Value = value;
  1490. SqlParameter p4 = new SqlParameter ("@p4",
  1491. SqlDbType.NVarChar, 4000);
  1492. p4.Direction = ParameterDirection.Output;
  1493. // for now, name shud be @RETURN_VALUE
  1494. // can be changed once RPC is implemented
  1495. SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  1496. p5.Direction = ParameterDirection.ReturnValue;
  1497. cmd.Parameters.Add (p1);
  1498. cmd.Parameters.Add (p2);
  1499. cmd.Parameters.Add (p3);
  1500. cmd.Parameters.Add (p4);
  1501. cmd.Parameters.Add (p5);
  1502. cmd.ExecuteNonQuery ();
  1503. Assert.AreEqual ("Hello", p4.Value, "#1");
  1504. Assert.AreEqual (2, p5.Value, "#2");
  1505. }
  1506. }
  1507. [Test] // bug #319694
  1508. public void DateTimeParameterTest ()
  1509. {
  1510. SqlConnection conn = new SqlConnection (connectionString);
  1511. using (conn) {
  1512. conn.Open ();
  1513. SqlCommand cmd = conn.CreateCommand ();
  1514. cmd.CommandText = "select * from datetime_family where type_datetime=@p1";
  1515. cmd.Parameters.Add ("@p1", SqlDbType.DateTime).Value = "10-10-2005";
  1516. // shudnt cause and exception
  1517. SqlDataReader rdr = cmd.ExecuteReader ();
  1518. rdr.Close ();
  1519. }
  1520. }
  1521. /**
  1522. * Verifies whether an enum value is converted to a numeric value when
  1523. * used as value for a numeric parameter (bug #66630)
  1524. */
  1525. [Test]
  1526. public void EnumParameterTest ()
  1527. {
  1528. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1529. try {
  1530. ConnectionManager.Singleton.OpenConnection ();
  1531. // create temp sp here, should normally be created in Setup of test
  1532. // case, but cannot be done right now because of ug #68978
  1533. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  1534. + "@Status smallint = 7"
  1535. + ")"
  1536. + "AS" + Environment.NewLine
  1537. + "BEGIN" + Environment.NewLine
  1538. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  1539. + "END");
  1540. SqlCommand cmd = new SqlCommand ("#Bug66630", conn);
  1541. cmd.CommandType = CommandType.StoredProcedure;
  1542. cmd.Parameters.Add ("@Status", SqlDbType.Int).Value = Status.Error;
  1543. using (SqlDataReader dr = cmd.ExecuteReader ()) {
  1544. // one record should be returned
  1545. Assert.IsTrue (dr.Read (), "EnumParameterTest#1");
  1546. // we should get two field in the result
  1547. Assert.AreEqual (2, dr.FieldCount, "EnumParameterTest#2");
  1548. // field 1
  1549. Assert.AreEqual ("int", dr.GetDataTypeName (0), "EnumParameterTest#3");
  1550. Assert.AreEqual (5, dr.GetInt32 (0), "EnumParameterTest#4");
  1551. // field 2
  1552. Assert.AreEqual ("smallint", dr.GetDataTypeName (1), "EnumParameterTest#5");
  1553. Assert.AreEqual ((short) Status.Error, dr.GetInt16 (1), "EnumParameterTest#6");
  1554. // only one record should be returned
  1555. Assert.IsFalse (dr.Read (), "EnumParameterTest#7");
  1556. }
  1557. } finally {
  1558. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  1559. " where name like '#temp_Bug66630' and type like 'P') " +
  1560. " drop procedure #temp_Bug66630; ");
  1561. ConnectionManager.Singleton.CloseConnection ();
  1562. }
  1563. }
  1564. [Test]
  1565. public void CloneTest ()
  1566. {
  1567. conn = new SqlConnection (connectionString);
  1568. conn.Open ();
  1569. SqlTransaction trans = conn.BeginTransaction ();
  1570. cmd = new SqlCommand ();
  1571. cmd.Connection = conn;
  1572. cmd.Transaction = trans;
  1573. SqlCommand clone = (((ICloneable) (cmd)).Clone ()) as SqlCommand;
  1574. Assert.AreSame (conn, clone.Connection);
  1575. Assert.AreSame (trans, clone.Transaction);
  1576. }
  1577. [Test]
  1578. public void StoredProc_NoParameterTest ()
  1579. {
  1580. string query = "create procedure #tmp_sp_proc as begin";
  1581. query += " select 'data' end";
  1582. SqlConnection conn = new SqlConnection (connectionString);
  1583. SqlCommand cmd = conn.CreateCommand ();
  1584. cmd.CommandText = query;
  1585. conn.Open ();
  1586. cmd.ExecuteNonQuery ();
  1587. cmd.CommandType = CommandType.StoredProcedure;
  1588. cmd.CommandText = "#tmp_sp_proc";
  1589. using (SqlDataReader reader = cmd.ExecuteReader ()) {
  1590. if (reader.Read ())
  1591. Assert.AreEqual ("data", reader.GetString (0), "#1");
  1592. else
  1593. Assert.Fail ("#2 Select shud return data");
  1594. }
  1595. conn.Close ();
  1596. }
  1597. [Test]
  1598. public void StoredProc_ParameterTest ()
  1599. {
  1600. string create_query = CREATE_TMP_SP_PARAM_TEST;
  1601. SqlConnection conn = new SqlConnection (connectionString);
  1602. conn.Open ();
  1603. SqlCommand cmd = conn.CreateCommand ();
  1604. int label = 0;
  1605. string error = string.Empty;
  1606. while (label != -1) {
  1607. try {
  1608. switch (label) {
  1609. case 0:
  1610. // Test BigInt Param
  1611. DBHelper.ExecuteNonQuery (conn,
  1612. String.Format (create_query, "bigint"));
  1613. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1614. Int64.MaxValue, Int64.MaxValue,
  1615. Int64.MaxValue, Int64.MaxValue);
  1616. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1617. Int64.MinValue, Int64.MinValue,
  1618. Int64.MinValue, Int64.MinValue);
  1619. rpc_helper_function (cmd, SqlDbType.BigInt, 0,
  1620. DBNull.Value, DBNull.Value,
  1621. DBNull.Value, DBNull.Value);
  1622. break;
  1623. case 1:
  1624. // Test Binary Param
  1625. DBHelper.ExecuteNonQuery (conn,
  1626. String.Format (create_query, "binary(5)"));
  1627. rpc_helper_function (cmd, SqlDbType.Binary, 5,
  1628. new byte [] { 1, 2, 3, 4, 5 },
  1629. new byte [] { 1, 2, 3, 4, 5 },
  1630. new byte [] { 1, 2, 3, 4, 5 },
  1631. new byte [] { 1, 2, 3, 4, 5 });
  1632. /*
  1633. rpc_helper_function (cmd, SqlDbType.Binary, 5,
  1634. DBNull.Value, DBNull.Value,
  1635. DBNull.Value);
  1636. */
  1637. rpc_helper_function (cmd, SqlDbType.Binary, 2,
  1638. new byte [0],
  1639. new byte [] { 0, 0, 0, 0, 0 },
  1640. new byte [] { 0, 0 },
  1641. new byte [] { 0, 0 });
  1642. break;
  1643. case 2:
  1644. // Test Bit Param
  1645. DBHelper.ExecuteNonQuery (conn,
  1646. String.Format (create_query, "bit"));
  1647. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1648. true, true, true, true);
  1649. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1650. false, false, false, false);
  1651. rpc_helper_function (cmd, SqlDbType.Bit, 0,
  1652. DBNull.Value, DBNull.Value,
  1653. DBNull.Value, DBNull.Value);
  1654. break;
  1655. case 3:
  1656. // Testing Char
  1657. DBHelper.ExecuteNonQuery (conn,
  1658. String.Format (create_query, "char(10)"));
  1659. rpc_helper_function (cmd, SqlDbType.Char, 10,
  1660. "characters", "characters",
  1661. "characters", "characters");
  1662. /*
  1663. rpc_helper_function (cmd, SqlDbType.Char, 3,
  1664. "characters", "cha ",
  1665. "cha");
  1666. rpc_helper_function (cmd, SqlDbType.Char, 3,
  1667. string.Empty, " ",
  1668. " ");
  1669. */
  1670. rpc_helper_function (cmd, SqlDbType.Char, 5,
  1671. DBNull.Value, DBNull.Value,
  1672. DBNull.Value, DBNull.Value);
  1673. break;
  1674. case 4:
  1675. // Testing DateTime
  1676. DBHelper.ExecuteNonQuery (conn,
  1677. String.Format (create_query, "datetime"));
  1678. rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2079-06-06 23:59:00",
  1679. new DateTime (2079, 6, 6, 23, 59, 0),
  1680. new DateTime (2079, 6, 6, 23, 59, 0),
  1681. new DateTime (2079, 6, 6, 23, 59, 0));
  1682. rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2009-04-12 10:39:45",
  1683. new DateTime (2009, 4, 12, 10, 39, 45),
  1684. new DateTime (2009, 4, 12, 10, 39, 45),
  1685. new DateTime (2009, 4, 12, 10, 39, 45));
  1686. rpc_helper_function (cmd, SqlDbType.DateTime, 0,
  1687. DBNull.Value, DBNull.Value,
  1688. DBNull.Value, DBNull.Value);
  1689. break;
  1690. case 5:
  1691. // Test Decimal Param
  1692. DBHelper.ExecuteNonQuery (conn,
  1693. String.Format (create_query, "decimal(10,2)"));
  1694. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1695. 10.665m, 10.67m, 11m, 10.67m);
  1696. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1697. 0m, 0m, 0m, 0m);
  1698. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1699. -5.657m, -5.66m, -6m, -5.66m);
  1700. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1701. DBNull.Value, DBNull.Value,
  1702. DBNull.Value, DBNull.Value);
  1703. // conversion
  1704. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1705. AttributeTargets.Constructor,
  1706. 32.0m, 32m, 32m);
  1707. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1708. 4.325f, 4.33m, 4m, 4.33m);
  1709. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1710. 10.0d, 10.00m, 10m, 10m);
  1711. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1712. 10.665d, 10.67m, 11m, 10.67m);
  1713. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1714. -5.657d, -5.66m, -6m, -5.66m);
  1715. rpc_helper_function (cmd, SqlDbType.Decimal, 0,
  1716. 4, 4m, 4m, 4m);
  1717. break;
  1718. case 6:
  1719. // Test Float Param
  1720. DBHelper.ExecuteNonQuery (conn,
  1721. String.Format (create_query, "float"));
  1722. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1723. 10.0, 10.0, 10.0, 10.0);
  1724. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1725. 10.54, 10.54, 10.54, 10.54);
  1726. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1727. 0, 0d, 0d, 0d);
  1728. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1729. -5.34, -5.34, -5.34, -5.34);
  1730. rpc_helper_function (cmd, SqlDbType.Float, 0,
  1731. DBNull.Value, DBNull.Value,
  1732. DBNull.Value, DBNull.Value);
  1733. break;
  1734. case 7:
  1735. // Testing Image
  1736. /* NOT WORKING
  1737. DBHelper.ExecuteNonQuery (conn,
  1738. String.Format(create_query, "image"));
  1739. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1740. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1741. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  1742. /* NOT WORKING*/
  1743. break;
  1744. case 8:
  1745. // Test Integer Param
  1746. DBHelper.ExecuteNonQuery (conn,
  1747. String.Format (create_query, "int"));
  1748. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1749. 10, 10, 10, 10);
  1750. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1751. 0, 0, 0, 0);
  1752. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1753. -5, -5, -5, -5);
  1754. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1755. int.MaxValue, int.MaxValue,
  1756. int.MaxValue, int.MaxValue);
  1757. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1758. int.MinValue, int.MinValue,
  1759. int.MinValue, int.MinValue);
  1760. rpc_helper_function (cmd, SqlDbType.Int, 0,
  1761. DBNull.Value, DBNull.Value,
  1762. DBNull.Value, DBNull.Value);
  1763. break;
  1764. case 9:
  1765. // Test Money Param
  1766. DBHelper.ExecuteNonQuery (conn,
  1767. String.Format (create_query, "money"));
  1768. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1769. 10m, 10m, 10m, 10m);
  1770. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1771. 10.54, 10.54m, 10.54m, 10.54m);
  1772. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1773. 0, 0m, 0m, 0m);
  1774. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1775. -5.34, -5.34m, -5.34m, -5.34m);
  1776. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1777. 5.34, 5.34m, 5.34m, 5.34m);
  1778. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1779. -10.1234m, -10.1234m, -10.1234m,
  1780. -10.1234m);
  1781. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1782. 10.1234m, 10.1234m, 10.1234m,
  1783. 10.1234m);
  1784. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1785. -2000000000m, -2000000000m,
  1786. -2000000000m, -2000000000m);
  1787. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1788. 2000000000m, 2000000000m,
  1789. 2000000000m, 2000000000m);
  1790. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1791. -200000000.2345m, -200000000.2345m,
  1792. -200000000.2345m, -200000000.2345m);
  1793. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1794. 200000000.2345m, 200000000.2345m,
  1795. 200000000.2345m, 200000000.2345m);
  1796. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1797. DBNull.Value, DBNull.Value,
  1798. DBNull.Value, DBNull.Value);
  1799. // rounding tests
  1800. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1801. -200000000.234561m, -200000000.2346m,
  1802. -200000000.2346m, -200000000.2346m);
  1803. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1804. -200000000.234551m, -200000000.2346m,
  1805. -200000000.2346m, -200000000.2346m);
  1806. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1807. -200000000.234541m, -200000000.2345m,
  1808. -200000000.2345m, -200000000.2345m);
  1809. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1810. 200000000.234561m, 200000000.2346m,
  1811. 200000000.2346m, 200000000.2346m);
  1812. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1813. 200000000.234551m, 200000000.2346m,
  1814. 200000000.2346m, 200000000.2346m);
  1815. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1816. 200000000.234541m, 200000000.2345m,
  1817. 200000000.2345m, 200000000.2345m);
  1818. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1819. -200000000.234461m, -200000000.2345m,
  1820. -200000000.2345m, -200000000.2345m);
  1821. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1822. -200000000.234451m, -200000000.2345m,
  1823. -200000000.2345m, -200000000.2345m);
  1824. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1825. -200000000.234441m, -200000000.2344m,
  1826. -200000000.2344m, -200000000.2344m);
  1827. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1828. 200000000.234461m, 200000000.2345m,
  1829. 200000000.2345m, 200000000.2345m);
  1830. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1831. 200000000.234451m, 200000000.2345m,
  1832. 200000000.2345m, 200000000.2345m);
  1833. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1834. 200000000.234441m, 200000000.2344m,
  1835. 200000000.2344m, 200000000.2344m);
  1836. // FIXME: we round toward even in SqlParameter.ConvertToFrameworkType
  1837. /*
  1838. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1839. -200000000.234550m, -200000000.2346m, -200000000.2346m);
  1840. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1841. 200000000.234550m, 200000000.2346m, 200000000.2346m);
  1842. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1843. -200000000.234450m, -200000000.2345m, -200000000.2345m);
  1844. rpc_helper_function (cmd, SqlDbType.Money, 0,
  1845. 200000000.234450m, 200000000.2345m, 200000000.2345m);
  1846. */
  1847. break;
  1848. case 23:
  1849. // Test NChar Param
  1850. DBHelper.ExecuteNonQuery (conn,
  1851. String.Format (create_query, "nchar(10)"));
  1852. rpc_helper_function (cmd, SqlDbType.NChar, 10,
  1853. "characters", "characters",
  1854. "characters", "characters");
  1855. rpc_helper_function (cmd, SqlDbType.NChar, 3,
  1856. "characters", "cha ",
  1857. "cha", "cha");
  1858. rpc_helper_function (cmd, SqlDbType.NChar, 3,
  1859. string.Empty, " ",
  1860. " ", " ");
  1861. /*
  1862. rpc_helper_function (cmd, SqlDbType.NChar, 5,
  1863. DBNull.Value, DBNull.Value,
  1864. DBNull.Value);
  1865. */
  1866. break;
  1867. case 10:
  1868. // Test NText Param
  1869. DBHelper.ExecuteNonQuery (conn,
  1870. String.Format (create_query, "ntext"));
  1871. /*
  1872. rpc_helper_function (cmd, SqlDbType.NText, 0, "ntext");
  1873. rpc_helper_function (cmd, SqlDbType.NText, 0, "");
  1874. rpc_helper_function (cmd, SqlDbType.NText, 0, null);
  1875. */
  1876. break;
  1877. case 11:
  1878. // Test NVarChar Param
  1879. DBHelper.ExecuteNonQuery (conn,
  1880. String.Format (create_query, "nvarchar(10)"));
  1881. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1882. "nvarchar", "nvarchar", "nvarchar",
  1883. "nvarchar");
  1884. rpc_helper_function (cmd, SqlDbType.NVarChar, 3,
  1885. "nvarchar", "nva", "nva", "nva");
  1886. /*
  1887. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1888. string.Empty, string.Empty, string.Empty);
  1889. rpc_helper_function (cmd, SqlDbType.NVarChar, 10,
  1890. DBNull.Value, DBNull.Value, DBNull.Value);
  1891. */
  1892. break;
  1893. case 12:
  1894. // Test Real Param
  1895. DBHelper.ExecuteNonQuery (conn,
  1896. String.Format (create_query, "real"));
  1897. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1898. 10m, 10f, 10f, 10f);
  1899. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1900. 10d, 10f, 10f, 10f);
  1901. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1902. 0, 0f, 0f, 0f);
  1903. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1904. 3.54d, 3.54f, 3.54f, 3.54f);
  1905. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1906. 10, 10f, 10f, 10f);
  1907. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1908. 10.5f, 10.5f, 10.5f, 10.5f);
  1909. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1910. 3.5d, 3.5f, 3.5f, 3.5f);
  1911. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1912. 4.54m, 4.54f, 4.54f, 4.54f);
  1913. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1914. -4.54m, -4.54f, -4.54f, -4.54f);
  1915. rpc_helper_function (cmd, SqlDbType.Real, 0,
  1916. DBNull.Value, DBNull.Value,
  1917. DBNull.Value, DBNull.Value);
  1918. break;
  1919. case 13:
  1920. // Test SmallDateTime Param
  1921. DBHelper.ExecuteNonQuery (conn,
  1922. String.Format (create_query, "smalldatetime"));
  1923. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
  1924. "6/6/2079 11:59:00 PM",
  1925. new DateTime (2079, 6, 6, 23, 59, 0),
  1926. new DateTime (2079, 6, 6, 23, 59, 0),
  1927. new DateTime (2079, 6, 6, 23, 59, 0));
  1928. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0,
  1929. DBNull.Value, DBNull.Value,
  1930. DBNull.Value, DBNull.Value);
  1931. break;
  1932. case 14:
  1933. // Test SmallInt Param
  1934. DBHelper.ExecuteNonQuery (conn,
  1935. String.Format (create_query, "smallint"));
  1936. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1937. 10, (short) 10, (short) 10, (short) 10);
  1938. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1939. -10, (short) -10, (short) -10,
  1940. (short) -10);
  1941. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1942. short.MaxValue, short.MaxValue,
  1943. short.MaxValue, short.MaxValue);
  1944. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1945. short.MinValue, short.MinValue,
  1946. short.MinValue, short.MinValue);
  1947. rpc_helper_function (cmd, SqlDbType.SmallInt, 0,
  1948. DBNull.Value, DBNull.Value,
  1949. DBNull.Value, DBNull.Value);
  1950. break;
  1951. case 15:
  1952. // Test SmallMoney Param
  1953. DBHelper.ExecuteNonQuery (conn,
  1954. String.Format (create_query, "smallmoney"));
  1955. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1956. 10.0d, 10m, 10m, 10m);
  1957. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1958. 0, 0m, 0m, 0m);
  1959. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1960. 3.54d, 3.54m, 3.54m, 3.54m);
  1961. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1962. 10, 10m, 10m, 10m);
  1963. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1964. 10.5f, 10.5m, 10.5m, 10.5m);
  1965. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1966. 3.5d, 3.5m, 3.5m, 3.5m);
  1967. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1968. 4.54m, 4.54m, 4.54m, 4.54m);
  1969. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1970. -4.54m, -4.54m, -4.54m, -4.54m);
  1971. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1972. -214748.3648m, -214748.3648m,
  1973. -214748.3648m, -214748.3648m);
  1974. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1975. 214748.3647m, 214748.3647m, 214748.3647m,
  1976. 214748.3647m);
  1977. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1978. DBNull.Value, DBNull.Value, DBNull.Value,
  1979. DBNull.Value);
  1980. // rounding tests
  1981. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1982. -4.543361m, -4.5434m, -4.5434m, -4.5434m);
  1983. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1984. -4.543351m, -4.5434m, -4.5434m, -4.5434m);
  1985. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1986. -4.543341m, -4.5433m, -4.5433m, -4.5433m);
  1987. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1988. 4.543361m, 4.5434m, 4.5434m, 4.5434m);
  1989. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1990. 4.543351m, 4.5434m, 4.5434m, 4.5434m);
  1991. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1992. 4.543341m, 4.5433m, 4.5433m, 4.5433m);
  1993. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1994. -4.543261m, -4.5433m, -4.5433m, -4.5433m);
  1995. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1996. -4.543251m, -4.5433m, -4.5433m, -4.5433m);
  1997. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  1998. -4.543241m, -4.5432m, -4.5432m, -4.5432m);
  1999. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2000. 4.543261m, 4.5433m, 4.5433m, 4.5433m);
  2001. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2002. 4.543251m, 4.5433m, 4.5433m, 4.5433m);
  2003. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2004. 4.543241m, 4.5432m, 4.5432m, 4.5432m);
  2005. // FIXME: we round toward even in SqlParameter.ConvertToFrameworkType
  2006. /*
  2007. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2008. -4.543350m, -4.5434m, -4.5434m);
  2009. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2010. 4.543350m, 4.5434m, 4.5434m);
  2011. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2012. -4.543250m, -4.5433m, -4.5433m);
  2013. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0,
  2014. 4.543250m, 4.5433m, 4.5433m);
  2015. */
  2016. break;
  2017. case 16:
  2018. // Test Text Param
  2019. DBHelper.ExecuteNonQuery (conn,
  2020. String.Format (create_query, "text"));
  2021. /*
  2022. rpc_helper_function (cmd, SqlDbType.Text, 0, "text");
  2023. rpc_helper_function (cmd, SqlDbType.Text, 0, "");
  2024. rpc_helper_function (cmd, SqlDbType.Text, 0, null);
  2025. */
  2026. break;
  2027. case 17:
  2028. // Test TimeStamp Param
  2029. /* NOT WORKING
  2030. DBHelper.ExecuteNonQuery (conn,
  2031. String.Format(create_query,"timestamp"));
  2032. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  2033. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  2034. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, null);
  2035. */
  2036. break;
  2037. case 18:
  2038. // Test TinyInt Param
  2039. DBHelper.ExecuteNonQuery (conn,
  2040. String.Format (create_query, "tinyint"));
  2041. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  2042. 10.0d, (byte) 10, (byte) 10,
  2043. (byte) 10);
  2044. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  2045. 0, (byte) 0, (byte) 0, (byte) 0);
  2046. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  2047. byte.MaxValue, byte.MaxValue,
  2048. byte.MaxValue, byte.MaxValue);
  2049. rpc_helper_function (cmd, SqlDbType.TinyInt, 0,
  2050. byte.MinValue, byte.MinValue,
  2051. byte.MinValue, byte.MinValue);
  2052. break;
  2053. case 19:
  2054. // Test UniqueIdentifier Param
  2055. /*
  2056. DBHelper.ExecuteNonQuery (conn,
  2057. String.Format(create_query,"uniqueidentifier"));
  2058. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, "0f159bf395b1d04f8c2ef5c02c3add96");
  2059. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, null);
  2060. */
  2061. break;
  2062. case 20:
  2063. // Test VarBinary Param
  2064. /* NOT WORKING
  2065. DBHelper.ExecuteNonQuery (conn,
  2066. String.Format(create_query,"varbinary (10)"));
  2067. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  2068. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  2069. rpc_helper_function (cmd, SqlDbType.VarBinary, 0, null);
  2070. */
  2071. break;
  2072. case 21:
  2073. // Test Varchar Param
  2074. DBHelper.ExecuteNonQuery (conn,
  2075. String.Format (create_query, "varchar(10)"));
  2076. rpc_helper_function (cmd, SqlDbType.VarChar, 7,
  2077. "VarChar", "VarChar", "VarChar",
  2078. "VarChar");
  2079. rpc_helper_function (cmd, SqlDbType.VarChar, 5,
  2080. "Var", "Var", "Var", "Var");
  2081. /*
  2082. rpc_helper_function (cmd, SqlDbType.VarChar, 3,
  2083. "Varchar", "Var", "Var");
  2084. rpc_helper_function (cmd, SqlDbType.VarChar, 10,
  2085. string.Empty, string.Empty, string.Empty);
  2086. rpc_helper_function (cmd, SqlDbType.VarChar, 10,
  2087. DBNull.Value, DBNull.Value,
  2088. DBNull.Value);
  2089. */
  2090. break;
  2091. case 22:
  2092. // Test Variant Param
  2093. /* NOT WORKING
  2094. DBHelper.ExecuteNonQuery (conn,
  2095. String.Format(create_query,"variant"));
  2096. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  2097. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  2098. rpc_helper_function (cmd, SqlDbType.Variant, 0, null);
  2099. */
  2100. break;
  2101. default:
  2102. label = -2;
  2103. break;
  2104. }
  2105. } catch (AssertionException ex) {
  2106. error += String.Format (" Case {0} INCORRECT VALUE : {1}\n", label, ex.ToString ());
  2107. } catch (Exception ex) {
  2108. error += String.Format (" Case {0} NOT WORKING : {1}\n", label, ex.ToString ());
  2109. }
  2110. label++;
  2111. if (label != -1)
  2112. DBHelper.ExecuteNonQuery (conn, string.Format (
  2113. CultureInfo.InvariantCulture,
  2114. DROP_STORED_PROCEDURE, "#tmp_sp_param_test"));
  2115. }
  2116. if (error.Length != 0)
  2117. Assert.Fail (error);
  2118. }
  2119. private void rpc_helper_function (SqlCommand cmd, SqlDbType type, int size, object input, object expectedRead, object expectedOut, object expectedInOut)
  2120. {
  2121. cmd.Parameters.Clear ();
  2122. SqlParameter param1, param2, param3;
  2123. if (size != 0) {
  2124. param1 = new SqlParameter ("@param1", type, size);
  2125. param2 = new SqlParameter ("@param2", type, size);
  2126. param3 = new SqlParameter ("@param3", type, size);
  2127. } else {
  2128. param1 = new SqlParameter ("@param1", type);
  2129. param2 = new SqlParameter ("@param2", type);
  2130. param3 = new SqlParameter ("@param3", type);
  2131. }
  2132. SqlParameter retval = new SqlParameter ("retval", SqlDbType.Int);
  2133. param1.Value = input;
  2134. param1.Direction = ParameterDirection.Input;
  2135. param2.Direction = ParameterDirection.Output;
  2136. param3.Direction = ParameterDirection.InputOutput;
  2137. param3.Value = input;
  2138. retval.Direction = ParameterDirection.ReturnValue;
  2139. cmd.Parameters.Add (param1);
  2140. cmd.Parameters.Add (param2);
  2141. cmd.Parameters.Add (param3);
  2142. cmd.Parameters.Add (retval);
  2143. cmd.CommandText = "#tmp_sp_param_test";
  2144. cmd.CommandType = CommandType.StoredProcedure;
  2145. using (SqlDataReader reader = cmd.ExecuteReader ()) {
  2146. Assert.IsTrue (reader.Read (), "#1");
  2147. AreEqual (expectedRead, reader.GetValue (0), "#2");
  2148. Assert.IsFalse (reader.Read (), "#3");
  2149. }
  2150. AreEqual (expectedOut, param2.Value, "#4");
  2151. AreEqual (expectedInOut, param3.Value, "#5");
  2152. Assert.AreEqual (5, retval.Value, "#6");
  2153. }
  2154. [Test]
  2155. public void OutputParamSizeTest1 ()
  2156. {
  2157. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2158. ConnectionManager.Singleton.OpenConnection ();
  2159. cmd = new SqlCommand ();
  2160. cmd.Connection = conn;
  2161. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2162. cmd.CommandType = CommandType.Text;
  2163. cmd.ExecuteNonQuery ();
  2164. cmd.CommandText = "#testsize";
  2165. cmd.CommandType = CommandType.StoredProcedure;
  2166. SqlParameter p1 = new SqlParameter ();
  2167. p1.ParameterName = "@p1";
  2168. p1.Direction = ParameterDirection.InputOutput;
  2169. p1.DbType = DbType.String;
  2170. p1.IsNullable = false;
  2171. cmd.Parameters.Add (p1);
  2172. try {
  2173. cmd.ExecuteNonQuery ();
  2174. Assert.Fail ("#1");
  2175. } catch (InvalidOperationException ex) {
  2176. // String[0]: the Size property has an invalid
  2177. // size of 0
  2178. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2179. Assert.IsNull (ex.InnerException, "#3");
  2180. Assert.IsNotNull (ex.Message, "#4");
  2181. }
  2182. }
  2183. [Test]
  2184. public void OutputParamSizeTest2 ()
  2185. {
  2186. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2187. ConnectionManager.Singleton.OpenConnection ();
  2188. cmd = new SqlCommand ();
  2189. cmd.Connection = conn;
  2190. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2191. cmd.CommandType = CommandType.Text;
  2192. cmd.ExecuteNonQuery ();
  2193. cmd.CommandText = "#testsize";
  2194. cmd.CommandType = CommandType.StoredProcedure;
  2195. SqlParameter p1 = new SqlParameter ();
  2196. p1.ParameterName = "@p1";
  2197. p1.Direction = ParameterDirection.Output;
  2198. p1.DbType = DbType.String;
  2199. p1.IsNullable = false;
  2200. cmd.Parameters.Add (p1);
  2201. try {
  2202. cmd.ExecuteNonQuery ();
  2203. Assert.Fail ("#1");
  2204. } catch (InvalidOperationException ex) {
  2205. // String[0]: the Size property has an invalid
  2206. // size of 0
  2207. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2208. Assert.IsNull (ex.InnerException, "#3");
  2209. Assert.IsNotNull (ex.Message, "#4");
  2210. }
  2211. }
  2212. [Test]
  2213. public void OutputParamSizeTest3 ()
  2214. {
  2215. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2216. ConnectionManager.Singleton.OpenConnection ();
  2217. cmd = new SqlCommand ();
  2218. cmd.Connection = conn;
  2219. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2220. cmd.CommandType = CommandType.Text;
  2221. cmd.ExecuteNonQuery ();
  2222. cmd.CommandText = "#testsize";
  2223. cmd.CommandType = CommandType.StoredProcedure;
  2224. SqlParameter p1 = new SqlParameter ();
  2225. p1.ParameterName = "@p1";
  2226. p1.Direction = ParameterDirection.InputOutput;
  2227. p1.DbType = DbType.String;
  2228. p1.IsNullable = true;
  2229. cmd.Parameters.Add (p1);
  2230. try {
  2231. cmd.ExecuteNonQuery ();
  2232. Assert.Fail ("#1");
  2233. } catch (InvalidOperationException ex) {
  2234. // String[0]: the Size property has an invalid
  2235. // size of 0
  2236. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2237. Assert.IsNull (ex.InnerException, "#3");
  2238. Assert.IsNotNull (ex.Message, "#4");
  2239. }
  2240. }
  2241. [Test]
  2242. public void OutputParamSizeTest4 ()
  2243. {
  2244. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2245. ConnectionManager.Singleton.OpenConnection ();
  2246. cmd = new SqlCommand ();
  2247. cmd.Connection = conn;
  2248. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  2249. cmd.CommandType = CommandType.Text;
  2250. cmd.ExecuteNonQuery ();
  2251. cmd.CommandText = "#testsize";
  2252. cmd.CommandType = CommandType.StoredProcedure;
  2253. SqlParameter p1 = new SqlParameter ();
  2254. p1.ParameterName = "@p1";
  2255. p1.Direction = ParameterDirection.Output;
  2256. p1.DbType = DbType.String;
  2257. p1.IsNullable = true;
  2258. cmd.Parameters.Add (p1);
  2259. try {
  2260. cmd.ExecuteNonQuery ();
  2261. Assert.Fail ("#1");
  2262. } catch (InvalidOperationException ex) {
  2263. // String[0]: the Size property has an invalid
  2264. // size of 0
  2265. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  2266. Assert.IsNull (ex.InnerException, "#3");
  2267. Assert.IsNotNull (ex.Message, "#4");
  2268. }
  2269. }
  2270. [Test] // bug #470579
  2271. public void OutputParamTest ()
  2272. {
  2273. SqlParameter newId, id;
  2274. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2275. ConnectionManager.Singleton.OpenConnection ();
  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 = 3;
  2283. cmd.ExecuteNonQuery ();
  2284. Assert.AreEqual (5, newId.Value, "#A1");
  2285. Assert.AreEqual (3, id.Value, "#A2");
  2286. cmd = conn.CreateCommand ();
  2287. cmd.CommandText = "set @NewId=@Id + 2";
  2288. cmd.CommandType = CommandType.Text;
  2289. newId = cmd.Parameters.Add ("NewId", SqlDbType.Int);
  2290. newId.Direction = ParameterDirection.Output;
  2291. id = cmd.Parameters.Add ("Id", SqlDbType.Int);
  2292. id.Value = 6;
  2293. #if NET_2_0
  2294. cmd.ExecuteNonQuery ();
  2295. Assert.AreEqual (8, newId.Value, "#B1");
  2296. Assert.AreEqual (6, id.Value, "#B2");
  2297. #else
  2298. try {
  2299. cmd.ExecuteNonQuery ();
  2300. Assert.Fail ("#B1");
  2301. } catch (SqlException ex) {
  2302. // Incorrect syntax near 'NewId'.
  2303. // Must declare the scalar variable "@Id"
  2304. Assert.AreEqual (typeof (SqlException), ex.GetType (), "#B2");
  2305. Assert.AreEqual ((byte) 15, ex.Class, "#B3");
  2306. Assert.IsNull (ex.InnerException, "#B4");
  2307. Assert.IsNotNull (ex.Message, "#B5");
  2308. Assert.IsTrue (ex.Message.IndexOf ("'NewId'") != -1, "#B6: " + ex.Message);
  2309. if (ClientVersion == 7) {
  2310. Assert.IsTrue (ex.Message.IndexOf ("'@Id'") != -1, "#B7: " + ex.Message);
  2311. Assert.AreEqual (170, ex.Number, "#B8");
  2312. } else {
  2313. Assert.IsTrue (ex.Message.IndexOf ("\"@Id\"") != -1, "#B7: " + ex.Message);
  2314. Assert.AreEqual (102, ex.Number, "#B8");
  2315. }
  2316. Assert.AreEqual ((byte) 1, ex.State, "#B9");
  2317. }
  2318. #endif
  2319. }
  2320. [Test]
  2321. public void SmallMoney_Overflow_Max ()
  2322. {
  2323. conn = new SqlConnection (connectionString);
  2324. conn.Open ();
  2325. DBHelper.ExecuteNonQuery (conn, string.Format (
  2326. CultureInfo.InvariantCulture, CREATE_TMP_SP_TYPE_TEST,
  2327. "SMALLMONEY"));
  2328. //decimal overflow = 214748.36471m;
  2329. decimal overflow = 214748.3648m;
  2330. cmd = conn.CreateCommand ();
  2331. cmd.CommandText = "#tmp_sp_type_test";
  2332. cmd.CommandType = CommandType.StoredProcedure;
  2333. SqlParameter param = cmd.Parameters.Add ("@param",
  2334. SqlDbType.SmallMoney);
  2335. param.Value = overflow;
  2336. try {
  2337. cmd.ExecuteScalar ();
  2338. Assert.Fail ("#1");
  2339. } catch (OverflowException ex) {
  2340. // SqlDbType.SmallMoney overflow. Value '214748.36471'
  2341. // is out of range. Must be between -214,748.3648 and 214,748.3647
  2342. Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#2");
  2343. Assert.IsNull (ex.InnerException, "#3");
  2344. Assert.IsNotNull (ex.Message, "#4");
  2345. #if NET_2_0
  2346. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2347. CultureInfo.InvariantCulture, "'{0}'",
  2348. overflow)) != -1, "#5:" + ex.Message);
  2349. #else
  2350. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2351. CultureInfo.CurrentCulture, "'{0}'",
  2352. overflow)) != -1, "#5:" + ex.Message);
  2353. #endif
  2354. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2355. CultureInfo.InvariantCulture, "{0:N4}",
  2356. SMALLMONEY_MIN)) != -1, "#6:" + ex.Message);
  2357. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2358. CultureInfo.InvariantCulture, "{0:N4}",
  2359. SMALLMONEY_MAX)) != -1, "#7:" + ex.Message);
  2360. } finally {
  2361. DBHelper.ExecuteNonQuery (conn, string.Format (
  2362. CultureInfo.InvariantCulture,
  2363. DROP_STORED_PROCEDURE, "#tmp_sp_type_test"));
  2364. }
  2365. }
  2366. [Test]
  2367. public void SmallMoney_Overflow_Min ()
  2368. {
  2369. conn = new SqlConnection (connectionString);
  2370. conn.Open ();
  2371. DBHelper.ExecuteNonQuery (conn, string.Format (
  2372. CultureInfo.InvariantCulture, CREATE_TMP_SP_TYPE_TEST,
  2373. "SMALLMONEY"));
  2374. //decimal overflow = -214748.36481m;
  2375. decimal overflow = -214748.3649m;
  2376. cmd = conn.CreateCommand ();
  2377. cmd.CommandText = "#tmp_sp_type_test";
  2378. cmd.CommandType = CommandType.StoredProcedure;
  2379. SqlParameter param = cmd.Parameters.Add ("@param",
  2380. SqlDbType.SmallMoney);
  2381. param.Value = overflow;
  2382. try {
  2383. cmd.ExecuteScalar ();
  2384. Assert.Fail ("#1");
  2385. } catch (OverflowException ex) {
  2386. // SqlDbType.SmallMoney overflow. Value '-214748,36481'
  2387. // is out of range. Must be between -214,748.3648 and 214,748.3647
  2388. Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#2");
  2389. Assert.IsNull (ex.InnerException, "#3");
  2390. Assert.IsNotNull (ex.Message, "#4");
  2391. #if NET_2_0
  2392. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2393. CultureInfo.InvariantCulture, "'{0}'",
  2394. overflow)) != -1, "#5:" + ex.Message);
  2395. #else
  2396. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2397. CultureInfo.CurrentCulture, "'{0}'",
  2398. overflow)) != -1, "#5:" + ex.Message);
  2399. #endif
  2400. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2401. CultureInfo.InvariantCulture, "{0:N4}",
  2402. SMALLMONEY_MIN)) != -1, "#6:" + ex.Message);
  2403. Assert.IsTrue (ex.Message.IndexOf (string.Format (
  2404. CultureInfo.InvariantCulture, "{0:N4}",
  2405. SMALLMONEY_MAX)) != -1, "#7:" + ex.Message);
  2406. } finally {
  2407. DBHelper.ExecuteNonQuery (conn, string.Format (
  2408. CultureInfo.InvariantCulture,
  2409. DROP_STORED_PROCEDURE, "#tmp_sp_type_test"));
  2410. }
  2411. }
  2412. #if NET_2_0
  2413. [Test]
  2414. public void NotificationTest ()
  2415. {
  2416. cmd = new SqlCommand ();
  2417. SqlNotificationRequest notification = new SqlNotificationRequest("MyNotification","MyService",15);
  2418. Assert.AreEqual (null, cmd.Notification, "#1 The default value for this property should be null");
  2419. cmd.Notification = notification;
  2420. Assert.AreEqual ("MyService", cmd.Notification.Options, "#2 The value should be MyService as the constructor is initiated with this value");
  2421. Assert.AreEqual (15, cmd.Notification.Timeout, "#2 The value should be 15 as the constructor is initiated with this value");
  2422. }
  2423. [Test]
  2424. public void NotificationAutoEnlistTest ()
  2425. {
  2426. cmd = new SqlCommand ();
  2427. Assert.AreEqual (true, cmd.NotificationAutoEnlist, "#1 Default value of the property should be true");
  2428. cmd.NotificationAutoEnlist = false;
  2429. Assert.AreEqual (false, cmd.NotificationAutoEnlist, "#2 The value of the property should be false after setting it to false");
  2430. }
  2431. [Test]
  2432. public void BeginExecuteXmlReaderTest ()
  2433. {
  2434. cmd = new SqlCommand ();
  2435. string connectionString1 = null;
  2436. connectionString1 = ConnectionManager.Singleton.ConnectionString + "Asynchronous Processing=true";
  2437. try {
  2438. SqlConnection conn1 = new SqlConnection (connectionString1);
  2439. conn1.Open ();
  2440. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
  2441. cmd.Connection = conn1;
  2442. IAsyncResult result = cmd.BeginExecuteXmlReader ();
  2443. XmlReader reader = cmd.EndExecuteXmlReader (result);
  2444. while (reader.Read ()) {
  2445. if (reader.LocalName.ToString () == "employee")
  2446. Assert.AreEqual ("kumar", reader["lname"], "#1 ");
  2447. }
  2448. } finally {
  2449. ConnectionManager.Singleton.CloseConnection ();
  2450. }
  2451. }
  2452. [Test]
  2453. public void BeginExecuteXmlReaderExceptionTest ()
  2454. {
  2455. cmd = new SqlCommand ();
  2456. try {
  2457. SqlConnection conn = new SqlConnection (connectionString);
  2458. conn.Open ();
  2459. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA";
  2460. cmd.Connection = conn;
  2461. try {
  2462. /*IAsyncResult result = */cmd.BeginExecuteXmlReader ();
  2463. } catch (InvalidOperationException) {
  2464. Assert.AreEqual (ConnectionManager.Singleton.ConnectionString, connectionString, "#1 Connection string has changed");
  2465. return;
  2466. }
  2467. Assert.Fail ("Expected Exception InvalidOperationException not thrown");
  2468. } finally {
  2469. ConnectionManager.Singleton.CloseConnection ();
  2470. }
  2471. }
  2472. #endif
  2473. [Test]
  2474. public void SqlCommandDisposeTest ()
  2475. {
  2476. IDataReader reader = null;
  2477. try {
  2478. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2479. ConnectionManager.Singleton.OpenConnection ();
  2480. IDbCommand command = conn.CreateCommand ();
  2481. try {
  2482. string sql = "SELECT * FROM employee";
  2483. command.CommandText = sql;
  2484. reader = command.ExecuteReader ();
  2485. } finally {
  2486. command.Dispose ();
  2487. }
  2488. while (reader.Read ()) ;
  2489. } finally {
  2490. reader.Dispose ();
  2491. ConnectionManager.Singleton.CloseConnection ();
  2492. }
  2493. }
  2494. private void bug326182_OutputParamMixupTestCommon (int paramOrder,
  2495. out int param0Val,
  2496. out int param1Val,
  2497. out int param2Val,
  2498. out int param3Val,
  2499. out int rvalVal)
  2500. {
  2501. try {
  2502. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2503. ConnectionManager.Singleton.OpenConnection ();
  2504. try {
  2505. SqlParameter param0 = new SqlParameter ("@param0", SqlDbType.Int);
  2506. param0.Direction = ParameterDirection.Output;
  2507. SqlParameter param1 = new SqlParameter ("@param1", SqlDbType.Int);
  2508. param1.Direction = ParameterDirection.Output;
  2509. SqlParameter param2 = new SqlParameter ("@param2", SqlDbType.Int);
  2510. param2.Direction = ParameterDirection.Output;
  2511. SqlParameter param3 = new SqlParameter ("@param3", SqlDbType.Int);
  2512. param3.Direction = ParameterDirection.Output;
  2513. SqlParameter rval = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  2514. rval.Direction = ParameterDirection.ReturnValue;
  2515. cmd = conn.CreateCommand ();
  2516. cmd.CommandText = "dbo.[sp_326182a]";
  2517. cmd.CommandType = CommandType.StoredProcedure;
  2518. switch (paramOrder) {
  2519. case 1: cmd.Parameters.Add (param0);
  2520. cmd.Parameters.Add (param1);
  2521. cmd.Parameters.Add (rval);
  2522. cmd.Parameters.Add (param2);
  2523. cmd.Parameters.Add (param3);
  2524. break;
  2525. case 2: cmd.Parameters.Add (rval);
  2526. cmd.Parameters.Add (param1);
  2527. cmd.Parameters.Add (param0);
  2528. cmd.Parameters.Add (param2);
  2529. cmd.Parameters.Add (param3);
  2530. break;
  2531. default: cmd.Parameters.Add (param0);
  2532. cmd.Parameters.Add (param1);
  2533. cmd.Parameters.Add (param2);
  2534. cmd.Parameters.Add (param3);
  2535. cmd.Parameters.Add (rval);
  2536. break;
  2537. }
  2538. cmd.ExecuteNonQuery ();
  2539. /* Copy the param values to variables, just in case if
  2540. * tests fail, we don't want the created sp to exist */
  2541. param3Val = (int) cmd.Parameters ["@param3"].Value;
  2542. param1Val = (int) cmd.Parameters ["@param1"].Value;
  2543. rvalVal = (int) cmd.Parameters ["@RETURN_VALUE"].Value;
  2544. param2Val = (int) cmd.Parameters ["@param2"].Value;
  2545. param0Val = (int) cmd.Parameters ["@param0"].Value;
  2546. } finally {
  2547. cmd.Dispose ();
  2548. cmd = null;
  2549. }
  2550. } finally {
  2551. ConnectionManager.Singleton.CloseConnection ();
  2552. conn = null;
  2553. }
  2554. }
  2555. [Test]
  2556. public void bug326182_OutputParamMixupTest_Normal ()
  2557. {
  2558. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2559. //param0Val = param1Val = param2Val = param3Val = rvalVal = 0;
  2560. bug326182_OutputParamMixupTestCommon (0, out param0Val, out param1Val,
  2561. out param2Val, out param3Val, out rvalVal);
  2562. Assert.AreEqual (103, param3Val);
  2563. Assert.AreEqual (101, param1Val);
  2564. Assert.AreEqual (2, rvalVal);
  2565. Assert.AreEqual (102, param2Val);
  2566. Assert.AreEqual (100, param0Val);
  2567. }
  2568. [Test]
  2569. public void bug326182_OutputParamMixupTest_RValInBetween ()
  2570. {
  2571. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2572. bug326182_OutputParamMixupTestCommon (1, out param0Val, out param1Val,
  2573. out param2Val, out param3Val, out rvalVal);
  2574. Assert.AreEqual (103, param3Val);
  2575. Assert.AreEqual (101, param1Val);
  2576. Assert.AreEqual (2, rvalVal);
  2577. Assert.AreEqual (102, param2Val);
  2578. Assert.AreEqual (100, param0Val);
  2579. }
  2580. [Test]
  2581. public void bug326182_OutputParamMixupTest_RValFirst ()
  2582. {
  2583. int param0Val, param1Val, param2Val, param3Val, rvalVal;
  2584. bug326182_OutputParamMixupTestCommon (2, out param0Val, out param1Val,
  2585. out param2Val, out param3Val, out rvalVal);
  2586. Assert.AreEqual (103, param3Val);
  2587. Assert.AreEqual (101, param1Val);
  2588. Assert.AreEqual (2, rvalVal);
  2589. Assert.AreEqual (102, param2Val);
  2590. Assert.AreEqual (100, param0Val);
  2591. }
  2592. [Test]
  2593. public void DeriveParameterTest_FullSchema ()
  2594. {
  2595. string create_tbl = "CREATE TABLE decimalCheck (deccheck DECIMAL (19, 5) null)";
  2596. string create_sp = "CREATE PROCEDURE sp_bug584833(@deccheck decimal(19,5) OUT)"
  2597. + "AS " + Environment.NewLine
  2598. + "BEGIN" + Environment.NewLine
  2599. + "INSERT INTO decimalCheck values (@deccheck)" + Environment.NewLine
  2600. + "SELECT @deccheck=deccheck from decimalCheck" + Environment.NewLine
  2601. + "END";
  2602. try {
  2603. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2604. ConnectionManager.Singleton.OpenConnection ();
  2605. cmd = conn.CreateCommand ();
  2606. cmd.CommandText = create_tbl;
  2607. cmd.ExecuteNonQuery ();
  2608. cmd.CommandText = create_sp;
  2609. cmd.ExecuteNonQuery ();
  2610. cmd.CommandText = "monotest.dbo.sp_bug584833";
  2611. cmd.CommandType = CommandType.StoredProcedure;
  2612. SqlCommandBuilder.DeriveParameters (cmd);
  2613. Assert.AreEqual (2, cmd.Parameters.Count, "#DPT - FullSchema - Parameter count mismatch");
  2614. Assert.AreEqual ("@deccheck", cmd.Parameters[1].ParameterName, "#DPT - FullSchema - Parameter name mismatch");
  2615. Assert.AreEqual (SqlDbType.Decimal, cmd.Parameters[1].SqlDbType, "#DPT - FullSchema - Parameter type mismatch");
  2616. } finally {
  2617. cmd.Parameters.Clear ();
  2618. cmd.CommandText = "drop procedure sp_bug584833";
  2619. cmd.CommandType = CommandType.Text;
  2620. cmd.ExecuteNonQuery ();
  2621. cmd.CommandText = "drop table decimalCheck";
  2622. cmd.ExecuteNonQuery ();
  2623. cmd.Dispose ();
  2624. cmd = null;
  2625. ConnectionManager.Singleton.CloseConnection ();
  2626. conn = null;
  2627. }
  2628. }
  2629. [Test]
  2630. public void DeriveParameterTest_SPName ()
  2631. {
  2632. string create_tbl = "CREATE TABLE decimalCheck (deccheck DECIMAL (19, 5) null)";
  2633. string create_sp = "CREATE PROCEDURE sp_bug584833(@deccheck decimal(19,5) OUT)"
  2634. + "AS " + Environment.NewLine
  2635. + "BEGIN" + Environment.NewLine
  2636. + "INSERT INTO decimalCheck values (@deccheck)" + Environment.NewLine
  2637. + "SELECT @deccheck=deccheck from decimalCheck" + Environment.NewLine
  2638. + "END";
  2639. try {
  2640. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2641. ConnectionManager.Singleton.OpenConnection ();
  2642. cmd = conn.CreateCommand ();
  2643. cmd.CommandText = create_tbl;
  2644. cmd.ExecuteNonQuery ();
  2645. cmd.CommandText = create_sp;
  2646. cmd.ExecuteNonQuery ();
  2647. cmd.CommandText = "sp_bug584833";
  2648. cmd.CommandType = CommandType.StoredProcedure;
  2649. SqlCommandBuilder.DeriveParameters (cmd);
  2650. Assert.AreEqual (2, cmd.Parameters.Count, "#DPT - SPName - Parameter count mismatch");
  2651. Assert.AreEqual ("@deccheck", cmd.Parameters[1].ParameterName, "#DPT - SPName - Parameter name mismatch");
  2652. Assert.AreEqual (SqlDbType.Decimal, cmd.Parameters[1].SqlDbType, "#DPT - SPName - Parameter type mismatch");
  2653. } finally {
  2654. cmd.Parameters.Clear ();
  2655. cmd.CommandType = CommandType.Text;
  2656. cmd.CommandText = "drop procedure sp_bug584833";
  2657. cmd.ExecuteNonQuery ();
  2658. cmd.CommandText = "drop table decimalCheck";
  2659. cmd.ExecuteNonQuery ();
  2660. cmd.Dispose ();
  2661. cmd = null;
  2662. ConnectionManager.Singleton.CloseConnection ();
  2663. conn = null;
  2664. }
  2665. }
  2666. [Test]
  2667. public void DeriveParameterTest_UserSchema ()
  2668. {
  2669. string create_tbl = "CREATE TABLE decimalCheck (deccheck DECIMAL (19, 5) null)";
  2670. string create_sp = "CREATE PROCEDURE sp_bug584833(@deccheck decimal(19,5) OUT)"
  2671. + "AS " + Environment.NewLine
  2672. + "BEGIN" + Environment.NewLine
  2673. + "INSERT INTO decimalCheck values (@deccheck)" + Environment.NewLine
  2674. + "SELECT @deccheck=deccheck from decimalCheck" + Environment.NewLine
  2675. + "END";
  2676. try {
  2677. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  2678. ConnectionManager.Singleton.OpenConnection ();
  2679. cmd = conn.CreateCommand ();
  2680. cmd.CommandText = create_tbl;
  2681. cmd.ExecuteNonQuery ();
  2682. cmd.CommandText = create_sp;
  2683. cmd.ExecuteNonQuery ();
  2684. cmd.CommandText = "dbo.sp_bug584833";
  2685. cmd.CommandType = CommandType.StoredProcedure;
  2686. SqlCommandBuilder.DeriveParameters (cmd);
  2687. Assert.AreEqual (2, cmd.Parameters.Count, "#DPT - user schema - Parameter count mismatch");
  2688. Assert.AreEqual ("@deccheck", cmd.Parameters[1].ParameterName, "#DPT - user schema - Parameter name mismatch");
  2689. Assert.AreEqual (SqlDbType.Decimal, cmd.Parameters[1].SqlDbType, "#DPT - user schema - Parameter type mismatch");
  2690. } finally {
  2691. cmd.Parameters.Clear ();
  2692. cmd.CommandType = CommandType.Text;
  2693. cmd.CommandText = "drop procedure dbo.sp_bug584833";
  2694. cmd.ExecuteNonQuery ();
  2695. cmd.CommandText = "drop table decimalCheck";
  2696. cmd.ExecuteNonQuery ();
  2697. cmd.Dispose ();
  2698. cmd = null;
  2699. ConnectionManager.Singleton.CloseConnection ();
  2700. conn = null;
  2701. }
  2702. }
  2703. // used as workaround for bugs in NUnit 2.2.0
  2704. static void AreEqual (object x, object y, string msg)
  2705. {
  2706. if (x == null && y == null)
  2707. return;
  2708. if ((x == null || y == null))
  2709. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2710. "Expected: {0}, but was: {1}. {2}",
  2711. x == null ? "<null>" : x, y == null ? "<null>" : y, msg));
  2712. bool isArrayX = x.GetType ().IsArray;
  2713. bool isArrayY = y.GetType ().IsArray;
  2714. if (isArrayX && isArrayY) {
  2715. Array arrayX = (Array) x;
  2716. Array arrayY = (Array) y;
  2717. if (arrayX.Length != arrayY.Length)
  2718. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2719. "Length of arrays differs. Expected: {0}, but was: {1}. {2}",
  2720. arrayX.Length, arrayY.Length, msg));
  2721. for (int i = 0; i < arrayX.Length; i++) {
  2722. object itemX = arrayX.GetValue (i);
  2723. object itemY = arrayY.GetValue (i);
  2724. if (!itemX.Equals (itemY))
  2725. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2726. "Arrays differ at position {0}. Expected: {1}, but was: {2}. {3}",
  2727. i, itemX, itemY, msg));
  2728. }
  2729. } else if (!x.Equals (y)) {
  2730. throw new AssertionException (string.Format (CultureInfo.InvariantCulture,
  2731. "Expected: {0} ({1}), but was: {2} ({3}). {4}",
  2732. x, x.GetType (), y, y.GetType (), msg));
  2733. }
  2734. }
  2735. int ClientVersion {
  2736. get {
  2737. return (engine.ClientVersion);
  2738. }
  2739. }
  2740. private enum Status
  2741. {
  2742. OK = 0,
  2743. Error = 3
  2744. }
  2745. private readonly string CREATE_TMP_SP_PARAM_TEST =
  2746. "CREATE PROCEDURE #tmp_sp_param_test (" + Environment.NewLine +
  2747. " @param1 {0}," + Environment.NewLine +
  2748. " @param2 {0} output," + Environment.NewLine +
  2749. " @param3 {0} output)" + Environment.NewLine +
  2750. "AS" + Environment.NewLine +
  2751. "BEGIN" + Environment.NewLine +
  2752. " SELECT @param1" + Environment.NewLine +
  2753. " SET @param2=@param1" + Environment.NewLine +
  2754. " RETURN 5" + Environment.NewLine +
  2755. "END";
  2756. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  2757. "@fname varchar (20), " + Environment.NewLine +
  2758. "@dob datetime, " + Environment.NewLine +
  2759. "@doj datetime output " + Environment.NewLine +
  2760. ") " + Environment.NewLine +
  2761. "as " + Environment.NewLine +
  2762. "begin" + Environment.NewLine +
  2763. "declare @id int;" + Environment.NewLine +
  2764. "select @id = max (id) from employee;" + Environment.NewLine +
  2765. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  2766. "set @doj = getdate();" + Environment.NewLine +
  2767. "insert into employee (id, fname, dob, doj) values (@id, @fname, @dob, @doj);" + Environment.NewLine +
  2768. "return @id;" + Environment.NewLine +
  2769. "end");
  2770. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  2771. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  2772. "drop procedure #sp_temp_insert_employee; ");
  2773. private static readonly string CREATE_TMP_SP_TYPE_TEST =
  2774. "CREATE PROCEDURE #tmp_sp_type_test " +
  2775. "(" +
  2776. " @param {0}" +
  2777. ") AS SELECT @param";
  2778. private static readonly string DROP_STORED_PROCEDURE =
  2779. "DROP PROCEDURE {0}";
  2780. }
  2781. }