SqlCommandTest.cs 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  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. using System.Xml;
  38. #endif
  39. using NUnit.Framework;
  40. namespace MonoTests.System.Data.SqlClient
  41. {
  42. [TestFixture]
  43. [Category ("sqlserver")]
  44. public class SqlCommandTest
  45. {
  46. public SqlConnection conn = null ;
  47. SqlCommand cmd = null;
  48. string connectionString = ConnectionManager.Singleton.ConnectionString;
  49. [SetUp]
  50. public void Setup ()
  51. {
  52. }
  53. [TearDown]
  54. public void TearDown ()
  55. {
  56. if (conn != null)
  57. conn.Close ();
  58. }
  59. [Test]
  60. public void ConstructorTest ()
  61. {
  62. // Test Default Constructor
  63. cmd = new SqlCommand ();
  64. Assert.AreEqual (String.Empty, cmd.CommandText,
  65. "#1 Command Test should be empty");
  66. Assert.AreEqual (30, cmd.CommandTimeout,
  67. "#2 CommandTimeout should be 30");
  68. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  69. "#3 CommandType should be text");
  70. Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
  71. Assert.AreEqual (0, cmd.Parameters.Count,
  72. "#5 Parameter shud be empty");
  73. // Test Overloaded Constructor
  74. String cmdText = "select * from tbl1" ;
  75. cmd = new SqlCommand (cmdText);
  76. Assert.AreEqual (cmdText, cmd.CommandText,
  77. "#5 CommandText should be the same as passed");
  78. Assert.AreEqual (30, cmd.CommandTimeout,
  79. "#6 CommandTimeout should be 30");
  80. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  81. "#7 CommandType should be text");
  82. Assert.IsNull (cmd.Connection , "#8 Connection Should be null");
  83. // Test Overloaded Constructor
  84. SqlConnection conn = new SqlConnection ();
  85. cmd = new SqlCommand (cmdText , conn);
  86. Assert.AreEqual (cmdText, cmd.CommandText,
  87. "#9 CommandText should be the same as passed");
  88. Assert.AreEqual (30, cmd.CommandTimeout,
  89. "#10 CommandTimeout should be 30");
  90. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  91. "#11 CommandType should be text");
  92. Assert.AreSame (cmd.Connection, conn, "#12 Connection Should be same");
  93. // Test Overloaded Constructor
  94. SqlTransaction trans = null ;
  95. try {
  96. conn = new SqlConnection (connectionString);
  97. conn.Open ();
  98. trans = conn.BeginTransaction ();
  99. cmd = new SqlCommand (cmdText, conn, trans);
  100. Assert.AreEqual (cmdText, cmd.CommandText,
  101. "#9 CommandText should be the same as passed");
  102. Assert.AreEqual (30, cmd.CommandTimeout,
  103. "#10 CommandTimeout should be 30");
  104. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  105. "#11 CommandType should be text");
  106. Assert.AreEqual (cmd.Connection, conn,
  107. "#12 Connection Should be null");
  108. Assert.AreEqual (cmd.Transaction, trans,
  109. "#13 Transaction Property should be set");
  110. // Test if parameters are reset to Default Values
  111. cmd = new SqlCommand ();
  112. Assert.AreEqual (String.Empty, cmd.CommandText,
  113. "#1 Command Test should be empty");
  114. Assert.AreEqual (30, cmd.CommandTimeout,
  115. "#2 CommandTimeout should be 30");
  116. Assert.AreEqual (CommandType.Text, cmd.CommandType,
  117. "#3 CommandType should be text");
  118. Assert.IsNull (cmd.Connection, "#4 Connection Should be null");
  119. }finally {
  120. trans.Rollback ();
  121. }
  122. }
  123. [Test]
  124. public void ExecuteScalarTest ()
  125. {
  126. conn = new SqlConnection (connectionString);
  127. cmd = new SqlCommand ("" , conn);
  128. cmd.CommandText = "Select count(*) from numeric_family where id<=4";
  129. //Check Exception is thrown when executed on a closed connection
  130. try {
  131. cmd.ExecuteScalar ();
  132. Assert.Fail ("#1 InvalidOperation Exception must be thrown");
  133. }catch (AssertionException e) {
  134. throw e;
  135. }catch (Exception e) {
  136. #if NET_2_0
  137. Assert.AreEqual (typeof (NullReferenceException), e.GetType (),
  138. "#2 Incorrect Exception : " + e.StackTrace);
  139. #else
  140. Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
  141. "#2 Incorrect Exception : " + e.StackTrace);
  142. #endif
  143. }
  144. // Check the Return value for a Correct Query
  145. object result = 0;
  146. conn.Open ();
  147. result = cmd.ExecuteScalar ();
  148. Assert.AreEqual (4, (int)result, "#3 Query Result returned is incorrect");
  149. cmd.CommandText = "select id , type_bit from numeric_family order by id asc" ;
  150. result = Convert.ToInt32 (cmd.ExecuteScalar ());
  151. Assert.AreEqual (1, result,
  152. "#4 ExecuteScalar Should return (1,1) the result set" );
  153. cmd.CommandText = "select id from numeric_family where id=-1";
  154. result = cmd.ExecuteScalar ();
  155. Assert.IsNull (result, "#5 Null should be returned if result set is empty");
  156. // Check SqlException is thrown for Invalid Query
  157. cmd.CommandText = "select count* from numeric_family";
  158. try {
  159. result = cmd.ExecuteScalar ();
  160. Assert.Fail ("#6 InCorrect Query should cause an SqlException");
  161. }catch (AssertionException e) {
  162. throw e;
  163. }catch (Exception e) {
  164. Assert.AreEqual (typeof(SqlException), e.GetType(),
  165. "#7 Incorrect Exception : " + e.StackTrace);
  166. }
  167. // Parameterized stored procedure calls
  168. int int_value = 20;
  169. string string_value = "output value changed";
  170. string return_value = "first column of first rowset";
  171. cmd.CommandText =
  172. "create procedure #tmp_executescalar_outparams "+
  173. " (@p1 int, @p2 int out, @p3 varchar(200) out) "+
  174. "as " +
  175. "select '" + return_value + "' as 'col1', @p1 as 'col2' "+
  176. "set @p2 = @p2 * 2 "+
  177. "set @p3 = N'" + string_value + "' "+
  178. "select 'second rowset' as 'col1', 2 as 'col2' "+
  179. "return 1";
  180. cmd.CommandType = CommandType.Text;
  181. cmd.ExecuteNonQuery ();
  182. cmd.CommandText = "#tmp_executescalar_outparams";
  183. cmd.CommandType = CommandType.StoredProcedure;
  184. SqlParameter p1 = new SqlParameter ();
  185. p1.ParameterName = "@p1";
  186. p1.Direction = ParameterDirection.Input;
  187. p1.DbType = DbType.Int32;
  188. p1.Value = int_value;
  189. cmd.Parameters.Add (p1);
  190. SqlParameter p2 = new SqlParameter ();
  191. p2.ParameterName = "@p2";
  192. p2.Direction = ParameterDirection.InputOutput;
  193. p2.DbType = DbType.Int32;
  194. p2.Value = int_value;
  195. cmd.Parameters.Add (p2);
  196. SqlParameter p3 = new SqlParameter ();
  197. p3.ParameterName = "@p3";
  198. p3.Direction = ParameterDirection.Output;
  199. p3.DbType = DbType.String;
  200. p3.Size = 200;
  201. cmd.Parameters.Add (p3);
  202. result = cmd.ExecuteScalar ();
  203. Assert.AreEqual (return_value, result, "#8 ExecuteScalar Should return 'first column of first rowset'");
  204. Assert.AreEqual (int_value * 2, p2.Value, "#9 ExecuteScalar should fill the parameter collection with the outputted values");
  205. Assert.AreEqual (string_value, p3.Value, "#10 ExecuteScalar should fill the parameter collection with the outputted values");
  206. p3.Size = 0;
  207. p3.Value = null;
  208. try {
  209. cmd.ExecuteScalar ();
  210. Assert.Fail ("#11 Query should throw System.InvalidOperationException due to size = 0 and value = null");
  211. }
  212. catch (AssertionException e) {
  213. throw e;
  214. }
  215. catch (Exception e) {
  216. Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
  217. "#12 Incorrect Exception : " + e.StackTrace);
  218. }
  219. conn.Close ();
  220. }
  221. [Test]
  222. public void ExecuteNonQuery ()
  223. {
  224. conn = new SqlConnection (connectionString);
  225. cmd = new SqlCommand ("", conn);
  226. int result = 0;
  227. // Test for exceptions
  228. // Test exception is thrown if connection is closed
  229. cmd.CommandText = "Select id from numeric_family where id=1";
  230. try {
  231. cmd.ExecuteNonQuery ();
  232. Assert.Fail ("#1 Connextion shud be open");
  233. }catch (AssertionException e) {
  234. throw e;
  235. }catch (Exception e) {
  236. #if NET_2_0
  237. Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
  238. "#2 Incorrect Exception : " + e);
  239. #else
  240. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  241. "#2 Incorrect Exception : " + e);
  242. #endif
  243. }
  244. // Test Exception is thrown if Query is incorrect
  245. conn.Open ();
  246. cmd.CommandText = "Select id1 from numeric_family";
  247. try {
  248. cmd.ExecuteNonQuery ();
  249. Assert.Fail ("#1 invalid Query");
  250. }catch (AssertionException e) {
  251. throw e;
  252. }catch (Exception e) {
  253. Assert.AreEqual (typeof(SqlException), e.GetType(),
  254. "#2 Incorrect Exception : " + e);
  255. }
  256. // Test Select/Insert/Update/Delete Statements
  257. SqlTransaction trans = conn.BeginTransaction ();
  258. cmd.Transaction = trans;
  259. try {
  260. cmd.CommandText = "Select id from numeric_family where id=1";
  261. result = cmd.ExecuteNonQuery ();
  262. Assert.AreEqual (-1, result, "#1");
  263. cmd.CommandText = "Insert into numeric_family (id,type_int) values (100,200)";
  264. result = cmd.ExecuteNonQuery ();
  265. Assert.AreEqual (1, result, "#2 One row shud be inserted");
  266. cmd.CommandText = "Update numeric_family set type_int=300 where id=100";
  267. result = cmd.ExecuteNonQuery ();
  268. Assert.AreEqual (1, result, "#3 One row shud be updated");
  269. // Test Batch Commands
  270. cmd.CommandText = "Select id from numeric_family where id=1;";
  271. cmd.CommandText += "update numeric_family set type_int=10 where id=1000";
  272. cmd.CommandText += "update numeric_family set type_int=10 where id=100";
  273. result = cmd.ExecuteNonQuery ();
  274. Assert.AreEqual (1, result, "#4 One row shud be updated");
  275. cmd.CommandText = "Delete from numeric_family where id=100";
  276. result = cmd.ExecuteNonQuery ();
  277. Assert.AreEqual (1, result, "#5 One row shud be deleted");
  278. }finally {
  279. trans.Rollback ();
  280. }
  281. // Parameterized stored procedure calls
  282. int int_value = 20;
  283. string string_value = "output value changed";
  284. cmd.CommandText =
  285. "create procedure #tmp_executescalar_outparams " +
  286. " (@p1 int, @p2 int out, @p3 varchar(200) out) " +
  287. "as " +
  288. "select 'test' as 'col1', @p1 as 'col2' " +
  289. "set @p2 = @p2 * 2 " +
  290. "set @p3 = N'" + string_value + "' " +
  291. "select 'second rowset' as 'col1', 2 as 'col2' " +
  292. "return 1";
  293. cmd.CommandType = CommandType.Text;
  294. cmd.ExecuteNonQuery ();
  295. cmd.CommandText = "#tmp_executescalar_outparams";
  296. cmd.CommandType = CommandType.StoredProcedure;
  297. SqlParameter p1 = new SqlParameter ();
  298. p1.ParameterName = "@p1";
  299. p1.Direction = ParameterDirection.Input;
  300. p1.DbType = DbType.Int32;
  301. p1.Value = int_value;
  302. cmd.Parameters.Add (p1);
  303. SqlParameter p2 = new SqlParameter ();
  304. p2.ParameterName = "@p2";
  305. p2.Direction = ParameterDirection.InputOutput;
  306. p2.DbType = DbType.Int32;
  307. p2.Value = int_value;
  308. cmd.Parameters.Add (p2);
  309. SqlParameter p3 = new SqlParameter ();
  310. p3.ParameterName = "@p3";
  311. p3.Direction = ParameterDirection.Output;
  312. p3.DbType = DbType.String;
  313. p3.Size = 200;
  314. cmd.Parameters.Add (p3);
  315. cmd.ExecuteNonQuery ();
  316. Assert.AreEqual (int_value * 2, p2.Value, "#6 ExecuteNonQuery should fill the parameter collection with the outputted values");
  317. Assert.AreEqual (string_value, p3.Value, "#7 ExecuteNonQuery should fill the parameter collection with the outputted values");
  318. }
  319. [Test]
  320. public void ExecuteReaderTest ()
  321. {
  322. SqlDataReader reader = null;
  323. conn = new SqlConnection (connectionString);
  324. // Test exception is thrown if conn is closed
  325. cmd = new SqlCommand ("Select count(*) from numeric_family");
  326. try {
  327. reader = cmd.ExecuteReader ();
  328. }catch (AssertionException e) {
  329. throw e;
  330. }catch (Exception e) {
  331. #if NET_2_0
  332. Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
  333. "#1 Incorrect Exception");
  334. #else
  335. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  336. "#1 Incorrect Exception");
  337. #endif
  338. }
  339. conn.Open ();
  340. // Test exception is thrown for Invalid Query
  341. cmd = new SqlCommand ("InvalidQuery", conn);
  342. try {
  343. reader = cmd.ExecuteReader ();
  344. Assert.Fail ("#1 Exception shud be thrown");
  345. }catch (AssertionException e) {
  346. throw e;
  347. }catch (Exception e) {
  348. Assert.AreEqual (typeof(SqlException), e.GetType (),
  349. "#2 Incorrect Exception : " + e);
  350. }
  351. // NOTE
  352. // Test SqlException is thrown if a row is locked
  353. // should lock a particular row and then modify it
  354. /*
  355. */
  356. // Test Connection cannot be modified when reader is in use
  357. // NOTE : msdotnet contradicts documented behavior
  358. /*
  359. cmd.CommandText = "select * from numeric_family where id=1";
  360. reader = cmd.ExecuteReader ();
  361. reader.Read ();
  362. conn.Close (); // valid operation
  363. conn = new SqlConnection (connectionString);
  364. */
  365. /*
  366. // NOTE msdotnet contradcits documented behavior
  367. // If the above testcase fails, then this shud be tested
  368. // Test connection can be modified once reader is closed
  369. conn.Close ();
  370. reader.Close ();
  371. conn = new SqlConnection (connectionString); // valid operation
  372. */
  373. }
  374. [Test]
  375. public void ExecuteReaderCommandBehaviorTest ()
  376. {
  377. // Test for command behaviors
  378. DataTable schemaTable = null;
  379. SqlDataReader reader = null;
  380. conn = new SqlConnection (connectionString);
  381. conn.Open ();
  382. cmd = new SqlCommand ("", conn);
  383. cmd.CommandText = "Select id from numeric_family where id <=4 order by id asc;";
  384. cmd.CommandText += "Select type_bit from numeric_family where id <=4 order by id asc";
  385. // Test for default command behavior
  386. reader = cmd.ExecuteReader ();
  387. int rows = 0;
  388. int results = 0;
  389. do {
  390. while (reader.Read ())
  391. rows++ ;
  392. Assert.AreEqual (4, rows, "#1 Multiple rows shud be returned");
  393. results++;
  394. rows = 0;
  395. }while (reader.NextResult());
  396. Assert.AreEqual (2, results, "#2 Multiple result sets shud be returned");
  397. reader.Close ();
  398. // Test if closing reader, closes the connection
  399. reader = cmd.ExecuteReader (CommandBehavior.CloseConnection);
  400. reader.Close ();
  401. Assert.AreEqual (ConnectionState.Closed, conn.State,
  402. "#3 Command Behavior is not followed");
  403. conn.Open();
  404. // Test if row info and primary Key info is returned
  405. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  406. schemaTable = reader.GetSchemaTable ();
  407. Assert.IsTrue(reader.HasRows, "#4 Data Rows shud also be returned");
  408. Assert.IsTrue ((bool)schemaTable.Rows[0]["IsKey"],
  409. "#5 Primary Key info shud be returned");
  410. reader.Close ();
  411. // Test only column information is returned
  412. reader = cmd.ExecuteReader (CommandBehavior.SchemaOnly);
  413. schemaTable = reader.GetSchemaTable ();
  414. Assert.IsFalse (reader.HasRows, "#6 row data shud not be returned");
  415. Assert.AreEqual(DBNull.Value, schemaTable.Rows[0]["IsKey"],
  416. "#7 Primary Key info shud not be returned");
  417. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  418. "#8 Schema Data is Incorrect");
  419. reader.Close ();
  420. // Test only one result set (first) is returned
  421. reader = cmd.ExecuteReader (CommandBehavior.SingleResult);
  422. schemaTable = reader.GetSchemaTable ();
  423. Assert.IsFalse (reader.NextResult(),
  424. "#9 Only one result set shud be returned");
  425. Assert.AreEqual ("id", schemaTable.Rows[0]["ColumnName"],
  426. "#10 The result set returned shud be the first result set");
  427. reader.Close ();
  428. // Test only one row is returned for all result sets
  429. // msdotnet doesnt work correctly.. returns only one result set
  430. reader = cmd.ExecuteReader (CommandBehavior.SingleRow);
  431. rows=0;
  432. results=0;
  433. do {
  434. while (reader.Read ())
  435. rows++ ;
  436. Assert.AreEqual (1, rows, "#11 Only one row shud be returned");
  437. results++;
  438. rows = 0;
  439. }while (reader.NextResult());
  440. // NOTE msdotnet contradicts documented behavior.
  441. // Multiple result sets shud be returned , and in this case : 2
  442. //Assert.AreEqual (2, results, "# Multiple result sets shud be returned");
  443. Assert.AreEqual (2, results, "#12 Multiple result sets shud be returned");
  444. reader.Close ();
  445. }
  446. [Test]
  447. public void PrepareTest_CheckValidStatement ()
  448. {
  449. cmd = new SqlCommand ();
  450. conn = new SqlConnection (connectionString);
  451. conn.Open ();
  452. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  453. cmd.Connection = conn ;
  454. // Test if Parameters are correctly populated
  455. cmd.Parameters.Clear ();
  456. cmd.Parameters.Add ("@ID", SqlDbType.TinyInt);
  457. cmd.Parameters["@ID"].Value = 2 ;
  458. cmd.Prepare ();
  459. Assert.AreEqual (2, cmd.ExecuteScalar (), "#3 Prepared Stmt not working");
  460. cmd.Parameters[0].Value = 3;
  461. Assert.AreEqual (3, cmd.ExecuteScalar (), "#4 Prep Stmt not working");
  462. conn.Close ();
  463. }
  464. [Test]
  465. public void PrepareTest ()
  466. {
  467. cmd = new SqlCommand ();
  468. conn = new SqlConnection (connectionString);
  469. conn.Open ();
  470. cmd.CommandText = "Select id from numeric_family where id=@ID" ;
  471. cmd.Connection = conn ;
  472. // Test InvalidOperation Exception is thrown if Parameter Type
  473. // is not explicitly set
  474. cmd.Parameters.Add ("@ID", 2);
  475. try {
  476. cmd.Prepare ();
  477. Assert.Fail ("#1 Parameter Type shud be explicitly Set");
  478. }catch (AssertionException e) {
  479. throw e;
  480. }catch (Exception e) {
  481. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  482. "#2 Incorrect Exception : " + e.StackTrace);
  483. }
  484. // Test Exception is thrown for variable size data if precision/scale
  485. // is not set
  486. cmd.CommandText = "select type_varchar from string_family where type_varchar=@p1";
  487. cmd.Parameters.Clear ();
  488. cmd.Parameters.Add ("@p1", SqlDbType.VarChar);
  489. cmd.Parameters["@p1"].Value = "afasasadadada";
  490. try {
  491. cmd.Prepare ();
  492. Assert.Fail ("#5 Exception shud be thrown");
  493. }catch (AssertionException e) {
  494. throw e;
  495. }catch (Exception e) {
  496. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  497. "#6 Incorrect Exception " + e.StackTrace);
  498. }
  499. // Test Exception is not thrown for Stored Procs
  500. try {
  501. cmd.CommandType = CommandType.StoredProcedure;
  502. cmd.CommandText = "ABFSDSFSF" ;
  503. cmd.Prepare ();
  504. }catch (Exception e) {
  505. Assert.Fail ("#7 Exception shud not be thrown for Stored Procs");
  506. }
  507. cmd.CommandType = CommandType.Text;
  508. conn.Close ();
  509. //Test InvalidOperation Exception is thrown if connection is not set
  510. cmd.Connection = null;
  511. try {
  512. cmd.Prepare ();
  513. #if NET_2_0
  514. Assert.Fail ("#8 NullReferenceException should be thrown");
  515. #else
  516. Assert.Fail ("#8 InvalidOperation Exception should be thrown");
  517. #endif
  518. }
  519. catch (AssertionException e) {
  520. throw e;
  521. }catch (Exception e) {
  522. #if NET_2_0
  523. Assert.AreEqual (typeof (NullReferenceException), e.GetType (),
  524. "#9 Incorrect Exception : " + e.StackTrace);
  525. #else
  526. Assert.AreEqual (typeof (InvalidOperationException), e.GetType (),
  527. "#9 Incorrect Exception : " + e.StackTrace);
  528. #endif
  529. }
  530. //Test InvalidOperation Exception is thrown if connection is closed
  531. cmd.Connection = conn ;
  532. try{
  533. cmd.Prepare ();
  534. Assert.Fail ("#4 InvalidOperation Exception shud be thrown");
  535. }catch (AssertionException e) {
  536. throw e;
  537. }catch (Exception e) {
  538. #if NET_2_0
  539. Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
  540. "Incorrect Exception : " + e.StackTrace);
  541. #else
  542. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  543. "Incorrect Exception : " + e.StackTrace);
  544. #endif
  545. }
  546. }
  547. [Test]
  548. public void ResetTimeOut ()
  549. {
  550. SqlCommand cmd = new SqlCommand ();
  551. cmd.CommandTimeout = 50 ;
  552. Assert.AreEqual ( cmd.CommandTimeout, 50,
  553. "#1 CommandTimeout should be modfiable");
  554. cmd.ResetCommandTimeout ();
  555. Assert.AreEqual (cmd.CommandTimeout, 30,
  556. "#2 Reset Should set the Timeout to default value");
  557. }
  558. [Test]
  559. [ExpectedException (typeof(ArgumentException))]
  560. public void CommandTimeout ()
  561. {
  562. cmd = new SqlCommand ();
  563. cmd.CommandTimeout = 10;
  564. Assert.AreEqual (10, cmd.CommandTimeout, "#1");
  565. cmd.CommandTimeout = -1;
  566. }
  567. [Test]
  568. #if NET_2_0
  569. [ExpectedException (typeof(ArgumentOutOfRangeException))]
  570. #else
  571. [ExpectedException (typeof(ArgumentException))]
  572. #endif
  573. public void CommandTypeTest ()
  574. {
  575. cmd = new SqlCommand ();
  576. Assert.AreEqual (CommandType.Text ,cmd.CommandType,
  577. "Default CommandType is text");
  578. cmd.CommandType = (CommandType)(-1);
  579. }
  580. [Test]
  581. [Ignore ("msdotnet contradicts documented behavior")]
  582. [ExpectedException (typeof(InvalidOperationException))]
  583. public void ConnectionTest ()
  584. {
  585. SqlTransaction trans = null;
  586. try {
  587. conn = new SqlConnection (connectionString);
  588. conn.Open ();
  589. trans = conn.BeginTransaction ();
  590. cmd = new SqlCommand ("", conn,trans);
  591. cmd.CommandText = "Select id from numeric_family where id=1";
  592. cmd.Connection = new SqlConnection ();
  593. }finally {
  594. trans.Rollback();
  595. conn.Close ();
  596. }
  597. }
  598. [Test]
  599. public void TransactionTest ()
  600. {
  601. conn = new SqlConnection (connectionString);
  602. cmd = new SqlCommand ("", conn);
  603. Assert.IsNull (cmd.Transaction, "#1 Default value is null");
  604. SqlConnection conn1 = new SqlConnection (connectionString);
  605. conn1.Open ();
  606. SqlTransaction trans1 = conn1.BeginTransaction ();
  607. cmd.Transaction = trans1 ;
  608. try {
  609. cmd.ExecuteNonQuery ();
  610. Assert.Fail ("#2 Connection cannot be different");
  611. }catch (Exception e) {
  612. #if NET_2_0
  613. Assert.AreEqual (typeof(NullReferenceException), e.GetType(),
  614. "#3 Incorrect Exception : " + e);
  615. #else
  616. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  617. "#3 Incorrect Exception : " + e);
  618. #endif
  619. }finally {
  620. conn1.Close ();
  621. conn.Close ();
  622. }
  623. }
  624. // Need to add more tests
  625. [Test]
  626. #if NET_2_0
  627. [ExpectedException (typeof(ArgumentOutOfRangeException))]
  628. #else
  629. [ExpectedException (typeof(ArgumentException))]
  630. #endif
  631. public void UpdatedRowSourceTest ()
  632. {
  633. cmd = new SqlCommand ();
  634. Assert.AreEqual (UpdateRowSource.Both, cmd.UpdatedRowSource,
  635. "#1 Default value is both");
  636. cmd.UpdatedRowSource = UpdateRowSource.None;
  637. Assert.AreEqual (UpdateRowSource.None, cmd.UpdatedRowSource,
  638. "#2");
  639. cmd.UpdatedRowSource = (UpdateRowSource) (-1);
  640. }
  641. [Test]
  642. public void ExecuteNonQueryTempProcedureTest () {
  643. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  644. try {
  645. ConnectionManager.Singleton.OpenConnection ();
  646. // create temp sp here, should normally be created in Setup of test
  647. // case, but cannot be done right now because of ug #68978
  648. DBHelper.ExecuteNonQuery (conn, CREATE_TMP_SP_TEMP_INSERT_PERSON);
  649. SqlCommand cmd = new SqlCommand();
  650. cmd.Connection = conn;
  651. cmd.CommandText = "#sp_temp_insert_employee";
  652. cmd.CommandType = CommandType.StoredProcedure;
  653. Object TestPar = "test";
  654. cmd.Parameters.Add("@fname", SqlDbType.VarChar);
  655. cmd.Parameters ["@fname"].Value = TestPar;
  656. Assert.AreEqual(1,cmd.ExecuteNonQuery());
  657. } finally {
  658. DBHelper.ExecuteNonQuery (conn, DROP_TMP_SP_TEMP_INSERT_PERSON);
  659. DBHelper.ExecuteSimpleSP (conn, "sp_clean_person_table");
  660. ConnectionManager.Singleton.CloseConnection ();
  661. }
  662. }
  663. // Test for bug #76778
  664. // Test for a case, when query size is greater than the block size
  665. [Test]
  666. public void LongQueryTest ()
  667. {
  668. SqlConnection conn = new SqlConnection (
  669. connectionString + ";Pooling=false");
  670. using (conn) {
  671. conn.Open ();
  672. SqlCommand cmd = conn.CreateCommand ();
  673. String value = new String ('a', 10000);
  674. cmd.CommandText = String.Format ("Select '{0}'", value);
  675. cmd.ExecuteNonQuery ();
  676. }
  677. }
  678. // Test for bug #76778
  679. // To make sure RPC (when implemented) works ok..
  680. [Test]
  681. public void LongStoredProcTest()
  682. {
  683. SqlConnection conn = new SqlConnection (
  684. connectionString + ";Pooling=false");
  685. using (conn) {
  686. conn.Open ();
  687. int size = conn.PacketSize ;
  688. SqlCommand cmd = conn.CreateCommand ();
  689. // create a temp stored proc ..
  690. cmd.CommandText = "Create Procedure #sp_tmp_long_params ";
  691. cmd.CommandText += "@p1 nvarchar (4000), ";
  692. cmd.CommandText += "@p2 nvarchar (4000), ";
  693. cmd.CommandText += "@p3 nvarchar (4000), ";
  694. cmd.CommandText += "@p4 nvarchar (4000) out ";
  695. cmd.CommandText += "As ";
  696. cmd.CommandText += "Begin ";
  697. cmd.CommandText += "Set @p4 = N'Hello' ";
  698. cmd.CommandText += "Return 2 ";
  699. cmd.CommandText += "End";
  700. cmd.ExecuteNonQuery ();
  701. //execute the proc
  702. cmd.CommandType = CommandType.StoredProcedure;
  703. cmd.CommandText = "#sp_tmp_long_params";
  704. String value = new String ('a', 4000);
  705. SqlParameter p1 = new SqlParameter ("@p1",
  706. SqlDbType.NVarChar,4000);
  707. p1.Value = value;
  708. SqlParameter p2 = new SqlParameter ("@p2",
  709. SqlDbType.NVarChar,4000);
  710. p2.Value = value;
  711. SqlParameter p3 = new SqlParameter ("@p3",
  712. SqlDbType.NVarChar,4000);
  713. p3.Value = value;
  714. SqlParameter p4 = new SqlParameter ("@p4",
  715. SqlDbType.NVarChar,4000);
  716. p4.Direction = ParameterDirection.Output;
  717. // for now, name shud be @RETURN_VALUE
  718. // can be changed once RPC is implemented
  719. SqlParameter p5 = new SqlParameter ("@RETURN_VALUE", SqlDbType.Int);
  720. p5.Direction = ParameterDirection.ReturnValue ;
  721. cmd.Parameters.Add (p1);
  722. cmd.Parameters.Add (p2);
  723. cmd.Parameters.Add (p3);
  724. cmd.Parameters.Add (p4);
  725. cmd.Parameters.Add (p5);
  726. cmd.ExecuteNonQuery ();
  727. Assert.AreEqual ("Hello", p4.Value, "#1");
  728. Assert.AreEqual (2, p5.Value, "#2");
  729. }
  730. }
  731. // Test for bug #76880
  732. [Test]
  733. public void DateTimeParameterTest ()
  734. {
  735. SqlConnection conn = new SqlConnection (connectionString);
  736. using (conn) {
  737. conn.Open ();
  738. SqlCommand cmd = conn.CreateCommand ();
  739. cmd.CommandText = "select * from datetime_family where type_datetime=@p1";
  740. cmd.Parameters.Add ("@p1", SqlDbType.DateTime).Value= "10-10-2005";
  741. // shudnt cause and exception
  742. SqlDataReader rdr = cmd.ExecuteReader ();
  743. rdr.Close ();
  744. }
  745. }
  746. /**
  747. * Verifies whether an enum value is converted to a numeric value when
  748. * used as value for a numeric parameter (bug #66630)
  749. */
  750. [Test]
  751. public void EnumParameterTest() {
  752. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  753. try {
  754. ConnectionManager.Singleton.OpenConnection ();
  755. // create temp sp here, should normally be created in Setup of test
  756. // case, but cannot be done right now because of ug #68978
  757. DBHelper.ExecuteNonQuery (conn, "CREATE PROCEDURE #Bug66630 ("
  758. + "@Status smallint = 7"
  759. + ")"
  760. + "AS" + Environment.NewLine
  761. + "BEGIN" + Environment.NewLine
  762. + "SELECT CAST(5 AS int), @Status" + Environment.NewLine
  763. + "END");
  764. SqlCommand cmd = new SqlCommand("#Bug66630", conn);
  765. cmd.CommandType = CommandType.StoredProcedure;
  766. cmd.Parameters.Add("@Status", SqlDbType.Int).Value = Status.Error;
  767. using (SqlDataReader dr = cmd.ExecuteReader()) {
  768. // one record should be returned
  769. Assert.IsTrue(dr.Read(), "EnumParameterTest#1");
  770. // we should get two field in the result
  771. Assert.AreEqual(2, dr.FieldCount, "EnumParameterTest#2");
  772. // field 1
  773. Assert.AreEqual("int", dr.GetDataTypeName(0), "EnumParameterTest#3");
  774. Assert.AreEqual(5, dr.GetInt32(0), "EnumParameterTest#4");
  775. // field 2
  776. Assert.AreEqual("smallint", dr.GetDataTypeName(1), "EnumParameterTest#5");
  777. Assert.AreEqual((short) Status.Error, dr.GetInt16(1), "EnumParameterTest#6");
  778. // only one record should be returned
  779. Assert.IsFalse(dr.Read(), "EnumParameterTest#7");
  780. }
  781. } finally {
  782. DBHelper.ExecuteNonQuery (conn, "if exists (select name from sysobjects " +
  783. " where name like '#temp_Bug66630' and type like 'P') " +
  784. " drop procedure #temp_Bug66630; ");
  785. ConnectionManager.Singleton.CloseConnection ();
  786. }
  787. }
  788. /**
  789. * The below test does not need a connection but since the setup opens
  790. * the connection i will need to close it
  791. */
  792. [Test]
  793. public void CloneTest() {
  794. ConnectionManager.Singleton.OpenConnection ();
  795. SqlCommand cmd = new SqlCommand();
  796. cmd.Connection = null;
  797. cmd.CommandText = "sp_insert";
  798. cmd.CommandType = CommandType.StoredProcedure;
  799. Object TestPar = DBNull.Value;
  800. cmd.Parameters.Add("@TestPar1", SqlDbType.Int);
  801. cmd.Parameters["@TestPar1"].Value = TestPar;
  802. cmd.Parameters.Add("@BirthDate", DateTime.Now);
  803. cmd.DesignTimeVisible = true;
  804. cmd.CommandTimeout = 100;
  805. Object clone1 = ((ICloneable)(cmd)).Clone();
  806. SqlCommand cmd1 = (SqlCommand) clone1;
  807. Assert.AreEqual(2, cmd1.Parameters.Count);
  808. Assert.AreEqual(100, cmd1.CommandTimeout);
  809. cmd1.Parameters.Add("@test", DateTime.Now);
  810. // to check that it is deep copy and not a shallow copy of the
  811. // parameter collection
  812. Assert.AreEqual(3, cmd1.Parameters.Count);
  813. Assert.AreEqual(2, cmd.Parameters.Count);
  814. }
  815. [Test]
  816. public void StoredProc_NoParameterTest ()
  817. {
  818. string query = "create procedure #tmp_sp_proc as begin";
  819. query += " select 'data' end";
  820. SqlConnection conn = new SqlConnection (connectionString);
  821. SqlCommand cmd = conn.CreateCommand ();
  822. cmd.CommandText = query ;
  823. conn.Open ();
  824. cmd.ExecuteNonQuery ();
  825. cmd.CommandType = CommandType.StoredProcedure;
  826. cmd.CommandText = "#tmp_sp_proc";
  827. using (SqlDataReader reader = cmd.ExecuteReader()) {
  828. if (reader.Read ())
  829. Assert.AreEqual ("data", reader.GetString(0),"#1");
  830. else
  831. Assert.Fail ("#2 Select shud return data");
  832. }
  833. conn.Close ();
  834. }
  835. [Test]
  836. public void StoredProc_ParameterTest ()
  837. {
  838. string create_query = CREATE_TMP_SP_PARAM_TEST;
  839. string drop_query = DROP_TMP_SP_PARAM_TEST;
  840. SqlConnection conn = new SqlConnection (connectionString);
  841. conn.Open ();
  842. SqlCommand cmd = conn.CreateCommand ();
  843. int label = 0 ;
  844. string error = "";
  845. while (label != -1) {
  846. try {
  847. switch (label) {
  848. case 0 :
  849. // Test BigInt Param
  850. DBHelper.ExecuteNonQuery (conn,
  851. String.Format(create_query, "bigint"));
  852. rpc_helper_function (cmd, SqlDbType.BigInt, 0, Int64.MaxValue);
  853. rpc_helper_function (cmd, SqlDbType.BigInt, 0, Int64.MinValue);
  854. break;
  855. case 1 :
  856. // Test Binary Param
  857. DBHelper.ExecuteNonQuery (conn,
  858. String.Format(create_query, "binary(5)"));
  859. //rpc_helper_function (cmd, SqlDbType.Binary, 0, new byte[] {});
  860. rpc_helper_function (cmd, SqlDbType.Binary, 5, new byte[] {1,2,3,4,5});
  861. break;
  862. case 2 :
  863. // Test Bit Param
  864. DBHelper.ExecuteNonQuery (conn,
  865. String.Format(create_query, "bit"));
  866. rpc_helper_function (cmd, SqlDbType.Bit, 0, true);
  867. rpc_helper_function (cmd, SqlDbType.Bit, 0, false);
  868. break;
  869. case 3 :
  870. // Testing Char
  871. DBHelper.ExecuteNonQuery (conn,
  872. String.Format(create_query, "char(10)"));
  873. rpc_helper_function (cmd, SqlDbType.Char, 10, "characters");
  874. /*
  875. rpc_helper_function (cmd, SqlDbType.Char, 10, "");
  876. rpc_helper_function (cmd, SqlDbType.Char, 10, null);
  877. */
  878. break;
  879. case 4 :
  880. // Testing DateTime
  881. DBHelper.ExecuteNonQuery (conn,
  882. String.Format(create_query, "datetime"));
  883. rpc_helper_function (cmd, SqlDbType.DateTime, 0, "2079-06-06 23:59:00");
  884. /*
  885. rpc_helper_function (cmd, SqlDbType.DateTime, 10, "");
  886. rpc_helper_function (cmd, SqlDbType.DateTime, 10, null);
  887. */
  888. break;
  889. case 5 :
  890. // Test Decimal Param
  891. DBHelper.ExecuteNonQuery (conn,
  892. String.Format(create_query,"decimal(10,2)"));
  893. /*
  894. rpc_helper_function (cmd, SqlDbType.Decimal, 0, 10.01);
  895. rpc_helper_function (cmd, SqlDbType.Decimal, 0, -10.01);
  896. */
  897. break;
  898. case 6 :
  899. // Test Float Param
  900. DBHelper.ExecuteNonQuery (conn,
  901. String.Format(create_query,"float"));
  902. rpc_helper_function (cmd, SqlDbType.Float, 0, 10.0);
  903. rpc_helper_function (cmd, SqlDbType.Float, 0, 0);
  904. /*
  905. rpc_helper_function (cmd, SqlDbType.Float, 0, null);
  906. */
  907. break;
  908. case 7 :
  909. // Testing Image
  910. /* NOT WORKING
  911. DBHelper.ExecuteNonQuery (conn,
  912. String.Format(create_query, "image"));
  913. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  914. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  915. rpc_helper_function (cmd, SqlDbType.Image, 0, );
  916. /* NOT WORKING*/
  917. break;
  918. case 8 :
  919. // Test Integer Param
  920. DBHelper.ExecuteNonQuery (conn,
  921. String.Format(create_query,"int"));
  922. rpc_helper_function (cmd, SqlDbType.Int, 0, 10);
  923. /*
  924. rpc_helper_function (cmd, SqlDbType.Int, 0, null);
  925. */
  926. break;
  927. case 9 :
  928. // Test Money Param
  929. DBHelper.ExecuteNonQuery (conn,
  930. String.Format(create_query,"money"));
  931. /*
  932. rpc_helper_function (cmd, SqlDbType.Money, 0, 10.0);
  933. rpc_helper_function (cmd, SqlDbType.Money, 0, null);
  934. */
  935. break;
  936. case 23 :
  937. // Test NChar Param
  938. DBHelper.ExecuteNonQuery (conn,
  939. String.Format(create_query,"nchar(10)"));
  940. /*
  941. rpc_helper_function (cmd, SqlDbType.NChar, 10, "nchar");
  942. rpc_helper_function (cmd, SqlDbType.NChar, 10, "");
  943. rpc_helper_function (cmd, SqlDbType.NChar, 10, null);
  944. */
  945. break;
  946. case 10 :
  947. // Test NText Param
  948. DBHelper.ExecuteNonQuery (conn,
  949. String.Format(create_query,"ntext"));
  950. /*
  951. rpc_helper_function (cmd, SqlDbType.NText, 0, "ntext");
  952. rpc_helper_function (cmd, SqlDbType.NText, 0, "");
  953. rpc_helper_function (cmd, SqlDbType.NText, 0, null);
  954. */
  955. break;
  956. case 11 :
  957. // Test NVarChar Param
  958. DBHelper.ExecuteNonQuery (conn,
  959. String.Format(create_query,"nvarchar(10)"));
  960. rpc_helper_function (cmd, SqlDbType.NVarChar, 10, "nvarchar");
  961. rpc_helper_function (cmd, SqlDbType.NVarChar, 10, "");
  962. //rpc_helper_function (cmd, SqlDbType.NVarChar, 10, null);
  963. break;
  964. case 12 :
  965. // Test Real Param
  966. DBHelper.ExecuteNonQuery (conn,
  967. String.Format(create_query,"real"));
  968. rpc_helper_function (cmd, SqlDbType.Real, 0, 10.0);
  969. //rpc_helper_function (cmd, SqlDbType.Real, 0, null);
  970. break;
  971. case 13 :
  972. // Test SmallDateTime Param
  973. DBHelper.ExecuteNonQuery (conn,
  974. String.Format(create_query,"smalldatetime"));
  975. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, "6/6/2079 11:59:00 PM");
  976. /*
  977. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, "");
  978. rpc_helper_function (cmd, SqlDbType.SmallDateTime, 0, null);
  979. */
  980. break;
  981. case 14 :
  982. // Test SmallInt Param
  983. DBHelper.ExecuteNonQuery (conn,
  984. String.Format(create_query,"smallint"));
  985. rpc_helper_function (cmd, SqlDbType.SmallInt, 0, 10);
  986. rpc_helper_function (cmd, SqlDbType.SmallInt, 0, -10);
  987. //rpc_helper_function (cmd, SqlDbType.SmallInt, 0, null);
  988. break;
  989. case 15 :
  990. // Test SmallMoney Param
  991. DBHelper.ExecuteNonQuery (conn,
  992. String.Format(create_query,"smallmoney"));
  993. /*
  994. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, 10.0);
  995. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, -10.0);
  996. rpc_helper_function (cmd, SqlDbType.SmallMoney, 0, null);
  997. */
  998. break;
  999. case 16 :
  1000. // Test Text Param
  1001. DBHelper.ExecuteNonQuery (conn,
  1002. String.Format(create_query,"text"));
  1003. /*
  1004. rpc_helper_function (cmd, SqlDbType.Text, 0, "text");
  1005. rpc_helper_function (cmd, SqlDbType.Text, 0, "");
  1006. rpc_helper_function (cmd, SqlDbType.Text, 0, null);
  1007. */
  1008. break;
  1009. case 17 :
  1010. // Test TimeStamp Param
  1011. /* NOT WORKING
  1012. DBHelper.ExecuteNonQuery (conn,
  1013. String.Format(create_query,"timestamp"));
  1014. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  1015. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, "");
  1016. rpc_helper_function (cmd, SqlDbType.TimeStamp, 0, null);
  1017. */
  1018. break;
  1019. case 18 :
  1020. // Test TinyInt Param
  1021. DBHelper.ExecuteNonQuery (conn,
  1022. String.Format(create_query,"tinyint"));
  1023. /*
  1024. rpc_helper_function (cmd, SqlDbType.TinyInt, 0, 10);
  1025. rpc_helper_function (cmd, SqlDbType.TinyInt, 0, -10);
  1026. rpc_helper_function (cmd, SqlDbType.TinyInt, 0, null);
  1027. */
  1028. break;
  1029. case 19 :
  1030. // Test UniqueIdentifier Param
  1031. /*
  1032. DBHelper.ExecuteNonQuery (conn,
  1033. String.Format(create_query,"uniqueidentifier"));
  1034. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, "0f159bf395b1d04f8c2ef5c02c3add96");
  1035. rpc_helper_function (cmd, SqlDbType.UniqueIdentifier, 0, null);
  1036. */
  1037. break;
  1038. case 20 :
  1039. // Test VarBinary Param
  1040. /* NOT WORKING
  1041. DBHelper.ExecuteNonQuery (conn,
  1042. String.Format(create_query,"varbinary (10)"));
  1043. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  1044. rpc_helper_function (cmd, SqlDbType.VarBinary, 0,);
  1045. rpc_helper_function (cmd, SqlDbType.VarBinary, 0, null);
  1046. */
  1047. break;
  1048. case 21 :
  1049. // Test Varchar Param
  1050. DBHelper.ExecuteNonQuery (conn,
  1051. String.Format(create_query,"varchar(10)"));
  1052. rpc_helper_function (cmd, SqlDbType.VarChar, 10, "VarChar");
  1053. break;
  1054. case 22 :
  1055. // Test Variant Param
  1056. /* NOT WORKING
  1057. DBHelper.ExecuteNonQuery (conn,
  1058. String.Format(create_query,"variant"));
  1059. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  1060. rpc_helper_function (cmd, SqlDbType.Variant, 0, );
  1061. rpc_helper_function (cmd, SqlDbType.Variant, 0, null);
  1062. */
  1063. break;
  1064. default :
  1065. label = -2;
  1066. break;
  1067. }
  1068. }catch (AssertionException e) {
  1069. error += String.Format (" Case {0} INCORRECT VALUE : {1}\n",label, e.Message);
  1070. }catch (Exception e) {
  1071. error += String.Format (" Case {0} NOT WORKING : {1}\n",label, e.Message);
  1072. }
  1073. label++;
  1074. if (label != -1)
  1075. DBHelper.ExecuteNonQuery (conn, drop_query);
  1076. }
  1077. if (error != String.Empty)
  1078. Assert.Fail (error);
  1079. }
  1080. private void rpc_helper_function (SqlCommand cmd, SqlDbType type, int size, object val)
  1081. {
  1082. cmd.Parameters.Clear ();
  1083. SqlParameter param1 ;
  1084. SqlParameter param2 ;
  1085. if (size != 0) {
  1086. param1 = new SqlParameter ("@param1", type, size);
  1087. param2 = new SqlParameter ("@param2", type, size);
  1088. }
  1089. else {
  1090. param1 = new SqlParameter ("@param1", type);
  1091. param2 = new SqlParameter ("@param2", type);
  1092. }
  1093. SqlParameter retval = new SqlParameter ("retval", SqlDbType.Int);
  1094. param1.Value = val;
  1095. param1.Direction = ParameterDirection.Input;
  1096. param2.Direction = ParameterDirection.Output;
  1097. retval.Direction = ParameterDirection.ReturnValue;
  1098. cmd.Parameters.Add (param1);
  1099. cmd.Parameters.Add (param2);
  1100. cmd.Parameters.Add (retval);
  1101. cmd.CommandText = "#tmp_sp_param_test";
  1102. cmd.CommandType = CommandType.StoredProcedure;
  1103. using (SqlDataReader reader = cmd.ExecuteReader ()) {
  1104. while (reader.Read ()) {
  1105. if (param1.Value != null && param1.Value.GetType () == typeof (string))
  1106. Assert.AreEqual (param1.Value,
  1107. reader.GetValue(0).ToString (),"#1");
  1108. else
  1109. Assert.AreEqual (param1.Value,
  1110. reader.GetValue(0),"#1");
  1111. }
  1112. }
  1113. if (param1.Value != null && param1.Value.GetType () == typeof (string) && param2.Value != null)
  1114. Assert.AreEqual (param1.Value.ToString (), param2.Value.ToString (), "#2");
  1115. else
  1116. Assert.AreEqual (param1.Value, param2.Value, "#2");
  1117. Assert.AreEqual (5, retval.Value, "#3");
  1118. }
  1119. [Test]
  1120. [ExpectedException (typeof (InvalidOperationException))]
  1121. public void OutputParamSizeTest1 ()
  1122. {
  1123. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1124. ConnectionManager.Singleton.OpenConnection ();
  1125. cmd = new SqlCommand ();
  1126. cmd.Connection = conn;
  1127. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  1128. cmd.CommandType = CommandType.Text;
  1129. cmd.ExecuteNonQuery ();
  1130. cmd.CommandText = "#testsize";
  1131. cmd.CommandType = CommandType.StoredProcedure;
  1132. SqlParameter p1 = new SqlParameter ();
  1133. p1.ParameterName = "@p1";
  1134. p1.Direction = ParameterDirection.InputOutput;
  1135. p1.DbType = DbType.String;
  1136. p1.IsNullable = false;
  1137. cmd.Parameters.Add (p1);
  1138. cmd.ExecuteNonQuery ();
  1139. }
  1140. [Test]
  1141. [ExpectedException (typeof (InvalidOperationException))]
  1142. public void OutputParamSizeTest2 ()
  1143. {
  1144. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1145. ConnectionManager.Singleton.OpenConnection ();
  1146. cmd = new SqlCommand ();
  1147. cmd.Connection = conn;
  1148. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  1149. cmd.CommandType = CommandType.Text;
  1150. cmd.ExecuteNonQuery ();
  1151. cmd.CommandText = "#testsize";
  1152. cmd.CommandType = CommandType.StoredProcedure;
  1153. SqlParameter p1 = new SqlParameter ();
  1154. p1.ParameterName = "@p1";
  1155. p1.Direction = ParameterDirection.Output;
  1156. p1.DbType = DbType.String;
  1157. p1.IsNullable = false;
  1158. cmd.Parameters.Add (p1);
  1159. cmd.ExecuteNonQuery ();
  1160. }
  1161. [Test]
  1162. [ExpectedException (typeof (InvalidOperationException))]
  1163. public void OutputParamSizeTest3 ()
  1164. {
  1165. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1166. ConnectionManager.Singleton.OpenConnection ();
  1167. cmd = new SqlCommand ();
  1168. cmd.Connection = conn;
  1169. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  1170. cmd.CommandType = CommandType.Text;
  1171. cmd.ExecuteNonQuery ();
  1172. cmd.CommandText = "#testsize";
  1173. cmd.CommandType = CommandType.StoredProcedure;
  1174. SqlParameter p1 = new SqlParameter ();
  1175. p1.ParameterName = "@p1";
  1176. p1.Direction = ParameterDirection.InputOutput;
  1177. p1.DbType = DbType.String;
  1178. p1.IsNullable = true;
  1179. cmd.Parameters.Add (p1);
  1180. cmd.ExecuteNonQuery ();
  1181. }
  1182. [Test]
  1183. [ExpectedException (typeof (InvalidOperationException))]
  1184. public void OutputParamSizeTest4 ()
  1185. {
  1186. conn = (SqlConnection) ConnectionManager.Singleton.Connection;
  1187. ConnectionManager.Singleton.OpenConnection ();
  1188. cmd = new SqlCommand ();
  1189. cmd.Connection = conn;
  1190. cmd.CommandText = "create procedure #testsize (@p1 as varchar(10) output) as return";
  1191. cmd.CommandType = CommandType.Text;
  1192. cmd.ExecuteNonQuery ();
  1193. cmd.CommandText = "#testsize";
  1194. cmd.CommandType = CommandType.StoredProcedure;
  1195. SqlParameter p1 = new SqlParameter ();
  1196. p1.ParameterName = "@p1";
  1197. p1.Direction = ParameterDirection.Output;
  1198. p1.DbType = DbType.String;
  1199. p1.IsNullable = true;
  1200. cmd.Parameters.Add (p1);
  1201. cmd.ExecuteNonQuery ();
  1202. }
  1203. #if NET_2_0
  1204. [Test]
  1205. public void NotificationTest ()
  1206. {
  1207. cmd = new SqlCommand ();
  1208. SqlNotificationRequest notification = new SqlNotificationRequest("MyNotification","MyService",15);
  1209. Assert.AreEqual (null, cmd.Notification, "#1 The default value for this property should be null");
  1210. cmd.Notification = notification;
  1211. Assert.AreEqual ("MyService", cmd.Notification.Options, "#2 The value should be MyService as the constructor is initiated with this value");
  1212. Assert.AreEqual (15, cmd.Notification.Timeout, "#2 The value should be 15 as the constructor is initiated with this value");
  1213. }
  1214. [Test]
  1215. public void NotificationAutoEnlistTest ()
  1216. {
  1217. cmd = new SqlCommand ();
  1218. Assert.AreEqual (true, cmd.NotificationAutoEnlist, "#1 Default value of the property should be true");
  1219. cmd.NotificationAutoEnlist = false;
  1220. Assert.AreEqual (false, cmd.NotificationAutoEnlist, "#2 The value of the property should be false after setting it to false");
  1221. }
  1222. [Test]
  1223. public void BeginExecuteXmlReaderTest ()
  1224. {
  1225. cmd = new SqlCommand ();
  1226. string connectionString1 = null;
  1227. connectionString1 = ConnectionManager.Singleton.ConnectionString + "Asynchronous Processing=true";
  1228. try {
  1229. SqlConnection conn1 = new SqlConnection (connectionString1);
  1230. conn1.Open ();
  1231. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA" ;
  1232. cmd.Connection = conn1;
  1233. IAsyncResult result = cmd.BeginExecuteXmlReader ();
  1234. XmlReader reader = cmd.EndExecuteXmlReader (result);
  1235. while (reader.Read ())
  1236. {
  1237. if (reader.LocalName.ToString () == "employee")
  1238. {
  1239. Assert.AreEqual ("kumar", reader["lname"], "#1 ");
  1240. }
  1241. }
  1242. } finally {
  1243. ConnectionManager.Singleton.CloseConnection ();
  1244. }
  1245. }
  1246. [Test]
  1247. public void BeginExecuteXmlReaderExceptionTest ()
  1248. {
  1249. cmd = new SqlCommand ();
  1250. try {
  1251. SqlConnection conn = new SqlConnection (connectionString);
  1252. conn.Open ();
  1253. cmd.CommandText = "Select lname from employee where id<2 FOR XML AUTO, XMLDATA" ;
  1254. cmd.Connection = conn;
  1255. try {
  1256. IAsyncResult result = cmd.BeginExecuteXmlReader ();
  1257. } catch (InvalidOperationException) {
  1258. Assert.AreEqual (ConnectionManager.Singleton.ConnectionString, connectionString, "#1 Connection string has changed");
  1259. return;
  1260. }
  1261. Assert.Fail ("Expected Exception InvalidOperationException not thrown");
  1262. } finally {
  1263. ConnectionManager.Singleton.CloseConnection ();
  1264. }
  1265. }
  1266. [Test]
  1267. public void CloneObjTest ()
  1268. {
  1269. SqlCommand cmd = new SqlCommand();
  1270. cmd.CommandText = "sp_insert";
  1271. cmd.CommandType = CommandType.StoredProcedure;
  1272. Object TestPar = DBNull.Value;
  1273. cmd.Parameters.Add ("@TestPar1", SqlDbType.Int);
  1274. cmd.Parameters ["@TestPar1"].Value = TestPar;
  1275. cmd.Parameters.Add ("@BirthDate", DateTime.Now);
  1276. cmd.DesignTimeVisible = true;
  1277. cmd.CommandTimeout = 100;
  1278. SqlCommand cmd1 = cmd.Clone ();
  1279. Assert.AreEqual (2, cmd1.Parameters.Count);
  1280. Assert.AreEqual (100, cmd1.CommandTimeout);
  1281. cmd1.Parameters.Add ("@test", DateTime.Now);
  1282. Assert.AreEqual (3, cmd1.Parameters.Count);
  1283. Assert.AreEqual (2, cmd.Parameters.Count);
  1284. }
  1285. #endif
  1286. private enum Status {
  1287. OK = 0,
  1288. Error = 3
  1289. }
  1290. private readonly string CREATE_TMP_SP_PARAM_TEST = "create procedure #tmp_sp_param_test (@param1 {0}, @param2 {0} output) as begin select @param1 set @param2=@param1 return 5 end";
  1291. private readonly string DROP_TMP_SP_PARAM_TEST = "drop procedure #tmp_sp_param_test";
  1292. private readonly string CREATE_TMP_SP_TEMP_INSERT_PERSON = ("create procedure #sp_temp_insert_employee ( " + Environment.NewLine +
  1293. "@fname varchar (20)) " + Environment.NewLine +
  1294. "as " + Environment.NewLine +
  1295. "begin" + Environment.NewLine +
  1296. "declare @id int;" + Environment.NewLine +
  1297. "select @id = max (id) from employee;" + Environment.NewLine +
  1298. "set @id = @id + 6000 + 1;" + Environment.NewLine +
  1299. "insert into employee (id, fname, dob, doj) values (@id, @fname, '1980-02-11', getdate ());" + Environment.NewLine +
  1300. "return @id;" + Environment.NewLine +
  1301. "end");
  1302. private readonly string DROP_TMP_SP_TEMP_INSERT_PERSON = ("if exists (select name from sysobjects where " + Environment.NewLine +
  1303. "name = '#sp_temp_insert_employee' and type = 'P') " + Environment.NewLine +
  1304. "drop procedure #sp_temp_insert_employee; ");
  1305. }
  1306. }