SqlDataReaderTest.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224
  1. //
  2. // SqlDataReaderTest.cs - NUnit Test Cases for testing the
  3. // SqlDataReader class
  4. // Author:
  5. // Umadevi S ([email protected])
  6. // Kornél Pál <http://www.kornelpal.hu/>
  7. // Sureshkumar T ([email protected])
  8. // Senganal T ([email protected])
  9. //
  10. // Copyright (c) 2004 Novell Inc., and the individuals listed
  11. // on the ChangeLog entries.
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Data;
  34. using System.Text;
  35. using System.Data.SqlTypes;
  36. using System.Data.Common;
  37. using System.Data.SqlClient;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.Data.SqlClient
  40. {
  41. [TestFixture]
  42. [Category ("sqlserver")]
  43. public class SqlDataReaderTest
  44. {
  45. SqlConnection conn = null;
  46. SqlCommand cmd = null;
  47. SqlDataReader reader = null;
  48. String query = "Select type_{0},type_{1},convert({0},null) from numeric_family where id=1";
  49. DataSet sqlDataset = null;
  50. DataTable numericDataTable =null;
  51. DataTable stringDataTable =null;
  52. DataTable binaryDataTable =null;
  53. DataTable datetimeDataTable =null;
  54. DataRow numericRow = null;
  55. DataRow stringRow = null;
  56. DataRow binaryRow = null;
  57. DataRow datetimeRow = null;
  58. [TestFixtureSetUp]
  59. public void init ()
  60. {
  61. conn = new SqlConnection (ConnectionManager.Singleton.ConnectionString);
  62. cmd = conn.CreateCommand ();
  63. sqlDataset = (new DataProvider()).GetDataSet ();
  64. numericDataTable = sqlDataset.Tables["numeric_family"];
  65. stringDataTable = sqlDataset.Tables["string_family"];
  66. binaryDataTable = sqlDataset.Tables["binary_family"];
  67. datetimeDataTable = sqlDataset.Tables["datetime_family"];
  68. numericRow = numericDataTable.Select ("id=1")[0];
  69. stringRow = stringDataTable.Select ("id=1")[0];
  70. binaryRow = binaryDataTable.Select ("id=1")[0];
  71. datetimeRow = datetimeDataTable.Select ("id=1")[0];
  72. }
  73. [SetUp]
  74. public void Setup ()
  75. {
  76. conn.Open ();
  77. }
  78. [TearDown]
  79. public void TearDown ()
  80. {
  81. if (reader != null)
  82. reader.Close ();
  83. conn.Close ();
  84. }
  85. [Test]
  86. public void ReadEmptyNTextFieldTest () {
  87. try {
  88. DBHelper.ExecuteNonQuery (conn, "create table #tmp_monotest (name ntext)");
  89. DBHelper.ExecuteNonQuery (conn, "insert into #tmp_monotest values ('')");
  90. SqlCommand cmd = (SqlCommand) conn.CreateCommand ();
  91. cmd.CommandText = "select * from #tmp_monotest";
  92. SqlDataReader dr = cmd.ExecuteReader ();
  93. if (dr.Read()) {
  94. Assert.AreEqual("System.String",dr["NAME"].GetType().FullName);
  95. }
  96. Assert.AreEqual (false, dr.Read (), "#2");
  97. } finally {
  98. ConnectionManager.Singleton.CloseConnection ();
  99. }
  100. }
  101. [Test]
  102. public void ReadBingIntTest()
  103. {
  104. try {
  105. string query = "SELECT CAST(548967465189498 AS bigint) AS Value";
  106. SqlCommand cmd = new SqlCommand();
  107. cmd.Connection = conn;
  108. cmd.CommandText = query;
  109. SqlDataReader r = cmd.ExecuteReader();
  110. using (r) {
  111. Assert.AreEqual (true, r.Read(), "#1");
  112. long id = r.GetInt64(0);
  113. Assert.AreEqual(548967465189498, id, "#2");
  114. id = r.GetSqlInt64(0).Value;
  115. Assert.AreEqual(548967465189498, id, "#3");
  116. }
  117. } finally {
  118. ConnectionManager.Singleton.CloseConnection ();
  119. }
  120. }
  121. // This method just helps in Calling common tests among all the Get* Methods
  122. // without replicating code
  123. void CallGetMethod (string s, int i)
  124. {
  125. switch (s) {
  126. case "Boolean" : reader.GetBoolean (i) ; break;
  127. case "SqlBoolean": reader.GetSqlBoolean (i); break;
  128. case "Int16" : reader.GetInt16 (i); break;
  129. case "SqlInt16" : reader.GetSqlInt16 (i); break;
  130. case "Int32" : reader.GetInt32 (i);break;
  131. case "SqlInt32" : reader.GetSqlInt32(i);break;
  132. case "Int64" : reader.GetInt64 (i);break;
  133. case "SqlInt64" : reader.GetSqlInt64(i); break;
  134. case "Decimal" : reader.GetDecimal(i);break;
  135. case "SqlDecimal" : reader.GetSqlDecimal (i);break;
  136. case "SqlMoney" : reader.GetSqlMoney (i);break;
  137. case "Float" : reader.GetFloat (i);break;
  138. case "SqlSingle" : reader.GetSqlSingle(i);break;
  139. case "Double" : reader.GetDouble (i);break;
  140. case "SqlDouble" : reader.GetSqlDouble(i);break;
  141. case "Guid" : reader.GetGuid(i);break;
  142. case "SqlGuid" : reader.GetSqlGuid(i);break;
  143. case "String" : reader.GetString(i);break;
  144. case "SqlString" : reader.GetSqlString(i);break;
  145. case "Char" : reader.GetChar(i);break;
  146. case "Byte" : reader.GetByte (i);break;
  147. case "SqlByte" : reader.GetSqlByte(i); break;
  148. case "DateTime" : reader.GetDateTime(i); break;
  149. case "SqlDateTime" : reader.GetSqlDateTime(i); break;
  150. case "SqlBinary" : reader.GetSqlBinary(i); break;
  151. default : Console.WriteLine ("OOOOPSSSSSS {0}",s);break;
  152. }
  153. }
  154. // This method just helps in Calling common tests among all the Get* Methods
  155. // without replicating code
  156. void GetMethodTests (string s)
  157. {
  158. try {
  159. CallGetMethod (s, 1);
  160. Assert.Fail ("#1[Get"+s+"] InvalidCastException must be thrown");
  161. }catch (AssertionException e) {
  162. throw e;
  163. }catch (Exception e) {
  164. Assert.AreEqual (typeof(InvalidCastException), e.GetType(),
  165. "#2[Get"+s+"] Incorrect Exception : " + e);
  166. }
  167. // GetSql* Methods do not throw SqlNullValueException
  168. // So, Testimg only for Get* Methods
  169. if (!s.StartsWith("Sql")) {
  170. try {
  171. CallGetMethod (s, 2);
  172. Assert.Fail ("#3[Get"+s+"] Exception must be thrown");
  173. }catch (AssertionException e) {
  174. throw e;
  175. }catch (Exception e){
  176. Assert.AreEqual (typeof(SqlNullValueException),e.GetType(),
  177. "#4[Get"+s+"] Incorrect Exception : " + e);
  178. }
  179. }
  180. try {
  181. CallGetMethod (s, 3);
  182. Assert.Fail ("#5[Get"+s+"] IndexOutOfRangeException must be thrown");
  183. }catch (AssertionException e) {
  184. throw e;
  185. }catch (Exception e){
  186. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  187. "#6[Get"+s+"] Incorrect Exception : " + e);
  188. }
  189. }
  190. [Test]
  191. public void GetBooleanTest ()
  192. {
  193. cmd.CommandText = string.Format (query, "bit", "int");
  194. reader = cmd.ExecuteReader ();
  195. reader.Read ();
  196. // Test for standard exceptions
  197. GetMethodTests("Boolean");
  198. // Test if data is returned correctly
  199. Assert.AreEqual (numericRow["type_bit"], reader.GetBoolean(0),
  200. "#2 DataValidation Failed");
  201. // Test for standard exceptions
  202. GetMethodTests("SqlBoolean");
  203. // Test if data is returned correctly
  204. Assert.AreEqual (numericRow["type_bit"], reader.GetSqlBoolean(0).Value,
  205. "#4 DataValidation Failed");
  206. reader.Close ();
  207. }
  208. [Test]
  209. public void GetByteTest ()
  210. {
  211. cmd.CommandText = string.Format (query, "tinyint", "int");
  212. reader = cmd.ExecuteReader ();
  213. reader.Read ();
  214. // Test for standard exceptions
  215. GetMethodTests("Byte");
  216. // Test if data is returned correctly
  217. Assert.AreEqual (numericRow["type_tinyint"], reader.GetByte(0),
  218. "#2 DataValidation Failed");
  219. // Test for standard exceptions
  220. GetMethodTests("SqlByte");
  221. // Test if data is returned correctly
  222. Assert.AreEqual (numericRow["type_tinyint"], reader.GetSqlByte(0).Value,
  223. "#4 DataValidation Failed");
  224. reader.Close ();
  225. }
  226. [Test]
  227. public void GetInt16Test ()
  228. {
  229. cmd.CommandText = string.Format (query, "smallint", "int");
  230. reader = cmd.ExecuteReader();
  231. reader.Read ();
  232. // Test for standard exceptions
  233. GetMethodTests("Int16");
  234. // Test if data is returned correctly
  235. Assert.AreEqual (numericRow["type_smallint"], reader.GetInt16(0),
  236. "#2 DataValidation Failed");
  237. // Test for standard exceptions
  238. GetMethodTests("SqlInt16");
  239. // Test if data is returned correctly
  240. Assert.AreEqual (numericRow["type_smallint"], reader.GetSqlInt16(0).Value,
  241. "#4 DataValidation Failed");
  242. reader.Close ();
  243. }
  244. [Test]
  245. public void GetInt32Test ()
  246. {
  247. cmd.CommandText = string.Format (query, "int", "bigint");
  248. reader = cmd.ExecuteReader ();
  249. reader.Read ();
  250. // Test for standard exceptions
  251. GetMethodTests("Int32");
  252. // Test if data is returned correctly
  253. Assert.AreEqual (numericRow["type_int"], reader.GetInt32(0),
  254. "#2 DataValidation Failed");
  255. // Test for standard exceptions
  256. GetMethodTests("SqlInt32");
  257. // Test if data is returned correctly
  258. Assert.AreEqual (numericRow["type_int"], reader.GetSqlInt32(0).Value,
  259. "#4 DataValidation Failed");
  260. reader.Close ();
  261. }
  262. [Test]
  263. public void GetInt64Test ()
  264. {
  265. cmd.CommandText = string.Format (query, "bigint", "int");
  266. reader = cmd.ExecuteReader ();
  267. reader.Read ();
  268. // Test for standard exceptions
  269. GetMethodTests("Int64");
  270. // Test if data is returned correctly
  271. Assert.AreEqual (numericRow["type_bigint"], reader.GetInt64(0),
  272. "#2 DataValidation Failed");
  273. // Test for standard exceptions
  274. GetMethodTests("SqlInt64");
  275. // Test if data is returned correctly
  276. Assert.AreEqual (numericRow["type_bigint"], reader.GetSqlInt64(0).Value,
  277. "#4 DataValidation Failed");
  278. reader.Close ();
  279. }
  280. [Test]
  281. public void GetDecimalTest ()
  282. {
  283. cmd.CommandText = string.Format (query, "decimal", "int");
  284. reader = cmd.ExecuteReader ();
  285. reader.Read ();
  286. // Test for standard exceptions
  287. GetMethodTests("Decimal");
  288. // Test if data is returned correctly
  289. Assert.AreEqual (numericRow["type_decimal"], reader.GetDecimal(0),
  290. "#2 DataValidation Failed");
  291. // Test for standard exceptions
  292. GetMethodTests("SqlDecimal");
  293. // Test if data is returned correctly
  294. Assert.AreEqual (numericRow["type_decimal"], reader.GetSqlDecimal(0).Value,
  295. "#4 DataValidation Failed");
  296. reader.Close ();
  297. }
  298. [Test]
  299. public void GetSqlMoneyTest ()
  300. {
  301. cmd.CommandText = string.Format (query, "money", "int");
  302. reader = cmd.ExecuteReader ();
  303. reader.Read ();
  304. // Test for standard exceptions
  305. GetMethodTests("SqlMoney");
  306. // Test if data is returned correctly
  307. Assert.AreEqual (numericRow["type_money"], reader.GetSqlMoney(0).Value,
  308. "#2 DataValidation Failed");
  309. reader.Close ();
  310. }
  311. [Test]
  312. public void GetFloatTest ()
  313. {
  314. cmd.CommandText = "select type_float,type_double,convert(real,null)";
  315. cmd.CommandText += "from numeric_family where id=1";
  316. reader = cmd.ExecuteReader ();
  317. reader.Read ();
  318. // Test for standard exceptions
  319. GetMethodTests("Float");
  320. // Test if data is returned correctly
  321. Assert.AreEqual (numericRow["type_float"], reader.GetFloat(0),
  322. "#2 DataValidation Failed");
  323. // Test for standard exceptions
  324. GetMethodTests("SqlSingle");
  325. // Test if data is returned correctly
  326. Assert.AreEqual (numericRow["type_float"], reader.GetSqlSingle(0).Value,
  327. "#2 DataValidation Failed");
  328. reader.Close ();
  329. }
  330. [Test]
  331. public void GetDoubleTest ()
  332. {
  333. cmd.CommandText = "select type_double,type_float,convert(float,null)";
  334. cmd.CommandText += " from numeric_family where id=1";
  335. reader = cmd.ExecuteReader ();
  336. reader.Read ();
  337. // Test for standard exceptions
  338. GetMethodTests("Double");
  339. // Test if data is returned correctly
  340. Assert.AreEqual (numericRow["type_double"], reader.GetDouble(0),
  341. "#2 DataValidation Failed");
  342. // Test for standard exceptions
  343. GetMethodTests("SqlDouble");
  344. // Test if data is returned correctly
  345. Assert.AreEqual (numericRow["type_double"], reader.GetSqlDouble(0).Value,
  346. "#4 DataValidation Failed");
  347. reader.Close ();
  348. }
  349. [Test]
  350. public void GetBytesTest ()
  351. {
  352. cmd.CommandText = "Select type_text,type_ntext,convert(text,null) ";
  353. cmd.CommandText += "from string_family where id=1";
  354. reader = cmd.ExecuteReader ();
  355. reader.Read ();
  356. try {
  357. reader.GetBytes (0,0,null,0,0);
  358. Assert.Fail ("#1 GetBytes shud be used only wth Sequential Access");
  359. }catch (AssertionException e) {
  360. throw e;
  361. }catch (Exception e) {
  362. Assert.AreEqual (typeof(InvalidCastException), e.GetType (),
  363. "#2 Incorrect Exception : " + e);
  364. }
  365. reader.Close ();
  366. byte[] asciiArray = (new ASCIIEncoding ()).GetBytes ("text");
  367. byte[] unicodeArray = (new UnicodeEncoding ()).GetBytes ("ntext");
  368. byte[] buffer = null ;
  369. long size = 0;
  370. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  371. reader.Read ();
  372. size = reader.GetBytes (0,0,null,0,0);
  373. Assert.AreEqual (asciiArray.Length, size, "#3 Data Incorrect");
  374. buffer = new byte[size];
  375. size = reader.GetBytes (0,0,buffer,0,(int)size);
  376. for (int i=0;i<size; i++)
  377. Assert.AreEqual (asciiArray[i], buffer[i], "#4 Data Incorrect");
  378. size = reader.GetBytes (1,0,null,0,0);
  379. Assert.AreEqual (unicodeArray.Length, size, "#5 Data Incorrect");
  380. buffer = new byte[size];
  381. size = reader.GetBytes (1,0,buffer,0,(int)size);
  382. for (int i=0;i<size; i++)
  383. Assert.AreEqual (unicodeArray[i], buffer[i], "#6 Data Incorrect");
  384. // Test if msdotnet behavior s followed when null value
  385. // is read using GetBytes
  386. Assert.AreEqual (0, reader.GetBytes (2,0,null,0,0), "#7");
  387. reader.GetBytes (2,0,buffer,0,10);
  388. reader.Close ();
  389. // do i need to test for image/binary values also ???
  390. }
  391. [Test]
  392. public void GetBytes_Binary ()
  393. {
  394. cmd.CommandText = "Select type_binary,type_varbinary,type_blob ";
  395. cmd.CommandText += "from binary_family where id=1";
  396. reader = cmd.ExecuteReader ();
  397. reader.Read ();
  398. byte[] binary = (byte[])reader.GetValue (0);
  399. byte[] varbinary = (byte[])reader.GetValue (1);
  400. byte[] image = (byte[])reader.GetValue (2);
  401. reader.Close ();
  402. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  403. reader.Read ();
  404. int len = 0;
  405. byte[] arr ;
  406. len = (int)reader.GetBytes (0,0,null,0,0);
  407. Assert.AreEqual (binary.Length, len, "#1");
  408. arr = new byte [len];
  409. reader.GetBytes (0,0,arr,0,len);
  410. for (int i=0; i<len; ++i)
  411. Assert.AreEqual (binary[i], arr[i], "#2");
  412. len = (int)reader.GetBytes (1,0,null,0,0);
  413. Assert.AreEqual (varbinary.Length, len, "#1");
  414. arr = new byte [len];
  415. reader.GetBytes (1,0,arr,0,len);
  416. for (int i=0; i<len; ++i)
  417. Assert.AreEqual (varbinary[i], arr[i], "#2");
  418. len = (int)reader.GetBytes (2,0,null,0,0);
  419. Assert.AreEqual (image.Length, len, "#1");
  420. arr = new byte [len];
  421. reader.GetBytes (2,0,arr,0,len);
  422. for (int i=0; i<len; ++i)
  423. Assert.AreEqual (image[i], arr[i], "#2");
  424. reader.Close ();
  425. cmd.CommandText = "Select type_binary,type_varbinary,type_blob ";
  426. cmd.CommandText += "from binary_family where id=1";
  427. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  428. reader.Read ();
  429. len = (int)reader.GetBytes (0,0,null,0,0);
  430. arr = new byte [100];
  431. for (int i=0; i<len; ++i) {
  432. Assert.AreEqual (len-i, reader.GetBytes (0, i, null, 0, 0), "#1_"+i);
  433. Assert.AreEqual (1, reader.GetBytes (0, i, arr, 0, 1), "#2_"+i);
  434. Assert.AreEqual (binary [i], arr [0], "#3_"+i);
  435. }
  436. Assert.AreEqual (0, reader.GetBytes (0, len+10, null, 0, 0));
  437. reader.Close ();
  438. }
  439. [Test]
  440. public void GetChars ()
  441. {
  442. cmd.CommandText = "Select type_char, type_varchar,type_text, type_ntext ";
  443. cmd.CommandText += "from string_family where id=1";
  444. reader = cmd.ExecuteReader ();
  445. reader.Read ();
  446. string charstring = reader.GetString (0);
  447. //string ncharstring = reader.GetString (1);
  448. string varcharstring = reader.GetString (1);
  449. //string nvarcharstring = reader.GetString (2);
  450. string textstring = reader.GetString (2);
  451. string ntextstring = reader.GetString (3);
  452. reader.Close ();
  453. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  454. reader.Read ();
  455. int len = 0;
  456. char[] arr;
  457. len = (int)reader.GetChars (0,0,null,0,0);
  458. Assert.AreEqual (charstring.Length, len, "#1");
  459. arr = new char [len];
  460. reader.GetChars (0,0,arr,0,len);
  461. Assert.AreEqual (0, charstring.CompareTo (new String (arr)), "#2");
  462. len = (int)reader.GetChars (1,0,null,0,0);
  463. Assert.AreEqual (varcharstring.Length, len, "#3");
  464. arr = new char [len];
  465. reader.GetChars (1,0,arr,0,len);
  466. Assert.AreEqual (0, varcharstring.CompareTo (new String (arr)), "#4");
  467. len = (int)reader.GetChars (2,0,null,0,0);
  468. Assert.AreEqual (textstring.Length, len, "#5");
  469. arr = new char [len];
  470. reader.GetChars (2,0,arr,0,len);
  471. Assert.AreEqual (0, textstring.CompareTo (new String (arr)), "#6");
  472. len = (int)reader.GetChars (3,0,null,0,0);
  473. Assert.AreEqual (ntextstring.Length, len, "#7");
  474. arr = new char [len];
  475. reader.GetChars (3,0,arr,0,len);
  476. Assert.AreEqual (0, ntextstring.CompareTo (new String (arr)), "#8");
  477. reader.Close ();
  478. reader = cmd.ExecuteReader (CommandBehavior.SequentialAccess);
  479. reader.Read ();
  480. len = (int)reader.GetChars (0,0,null,0,0);
  481. arr = new char [10];
  482. for (int i=0; i<len; ++i) {
  483. Assert.AreEqual (len-i, reader.GetChars (0, i, null, 0, 0), "#9_"+i);
  484. Assert.AreEqual (1, reader.GetChars (0, i, arr, 0, 1), "#10_"+i);
  485. Assert.AreEqual (charstring [i], arr [0], "#11_"+i);
  486. }
  487. Assert.AreEqual (0, reader.GetChars (0, len+10, null, 0, 0));
  488. reader.Close ();
  489. }
  490. [Test]
  491. public void GetStringTest ()
  492. {
  493. cmd.CommandText = "Select type_varchar,10,convert(varchar,null)";
  494. cmd.CommandText += "from string_family where id=1";
  495. reader = cmd.ExecuteReader ();
  496. reader.Read ();
  497. // Test for standard exceptions
  498. GetMethodTests("String");
  499. // Test if data is returned correctly
  500. Assert.AreEqual (stringRow["type_varchar"], reader.GetString(0),
  501. "#2 DataValidation Failed");
  502. // Test for standard exceptions
  503. GetMethodTests("SqlString");
  504. // Test if data is returned correctly
  505. Assert.AreEqual (stringRow["type_varchar"], reader.GetSqlString(0).Value,
  506. "#4 DataValidation Failed");
  507. reader.Close();
  508. }
  509. [Test]
  510. public void GetSqlBinaryTest ()
  511. {
  512. cmd.CommandText = "Select type_binary ,10 ,convert(binary,null)";
  513. cmd.CommandText += "from binary_family where id=1";
  514. reader = cmd.ExecuteReader ();
  515. reader.Read ();
  516. // Test for standard exceptions
  517. GetMethodTests ("SqlBinary");
  518. // Test if data is returned correctly
  519. Assert.AreEqual (binaryRow["type_binary"], reader.GetSqlBinary(0).Value,
  520. "#2 DataValidation Failed");
  521. reader.Close ();
  522. }
  523. [Test]
  524. public void GetGuidTest ()
  525. {
  526. cmd.CommandText = "Select type_guid,id,convert(uniqueidentifier,null)";
  527. cmd.CommandText += "from string_family where id=1";
  528. reader = cmd.ExecuteReader ();
  529. reader.Read ();
  530. // Test for standard exceptions
  531. GetMethodTests("Guid");
  532. // Test if data is returned correctly
  533. Assert.AreEqual (stringRow["type_guid"], reader.GetGuid(0),
  534. "#2 DataValidation Failed");
  535. // Test for standard exceptions
  536. GetMethodTests("SqlGuid");
  537. // Test if data is returned correctly
  538. Assert.AreEqual (stringRow["type_guid"], reader.GetSqlGuid(0).Value,
  539. "#4 DataValidation Failed");
  540. reader.Close ();
  541. }
  542. [Test]
  543. public void GetDateTimeTest ()
  544. {
  545. cmd.CommandText = "Select type_datetime,10,convert(datetime,null)";
  546. cmd.CommandText += "from datetime_family where id=1";
  547. reader = cmd.ExecuteReader ();
  548. reader.Read ();
  549. // Test for standard exceptions
  550. GetMethodTests("DateTime");
  551. // Test if data is returned correctly
  552. Assert.AreEqual (datetimeRow["type_datetime"], reader.GetDateTime(0),
  553. "#2 DataValidation Failed");
  554. // Test for standard exceptions
  555. GetMethodTests("SqlDateTime");
  556. // Test if data is returned correctly
  557. Assert.AreEqual (datetimeRow["type_datetime"], reader.GetSqlDateTime(0).Value,
  558. "#2 DataValidation Failed");
  559. reader.Close ();
  560. }
  561. [Test]
  562. [Ignore ("Not Supported by msdotnet")]
  563. public void GetCharTest ()
  564. {
  565. cmd.CommandText = "Select type_char,type_guid,convert(char,null)";
  566. cmd.CommandText += "from string_family where id=1";
  567. reader = cmd.ExecuteReader ();
  568. reader.Read ();
  569. // Test for standard exceptions
  570. GetMethodTests ("Char");
  571. reader.Close ();
  572. }
  573. [Test]
  574. public void GetValueTest ()
  575. {
  576. cmd.CommandText = "Select id, null from numeric_family where id=1";
  577. reader = cmd.ExecuteReader ();
  578. reader.Read ();
  579. object obj = null;
  580. obj = reader.GetValue (0);
  581. Assert.AreEqual ((byte)1, obj, "#1 Shud return the value of id");
  582. obj = reader.GetValue (1);
  583. Assert.AreEqual (DBNull.Value, obj, "#2 shud return DBNull");
  584. reader.Close ();
  585. }
  586. [Test]
  587. public void GetSqlValueTest ()
  588. {
  589. cmd.CommandText = "Select id, type_tinyint, null from numeric_family where id=1";
  590. reader = cmd.ExecuteReader ();
  591. reader.Read ();
  592. Assert.AreEqual ((byte)255, ((SqlByte) reader.GetSqlValue(1)).Value, "#1");
  593. //Assert.AreEqual (DBNull.Value, reader.GetSqlValue(2), "#2");
  594. reader.Close ();
  595. }
  596. [Test]
  597. public void GetValuesTest ()
  598. {
  599. cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
  600. reader = cmd.ExecuteReader ();
  601. reader.Read ();
  602. object[] arr = null;
  603. int count = 0;
  604. arr = new object[1];
  605. count = reader.GetValues (arr);
  606. Assert.AreEqual (10, (int)arr[0], "#1 Only first object shud be copied");
  607. Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
  608. arr = new object[3];
  609. count = reader.GetValues (arr);
  610. Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
  611. arr = new object [5];
  612. count = reader.GetValues (arr);
  613. Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
  614. Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
  615. reader.Close ();
  616. }
  617. [Test]
  618. public void GetSqlValuesTest ()
  619. {
  620. cmd.CommandText = "Select 10,20,30 from numeric_family where id=1";
  621. reader = cmd.ExecuteReader ();
  622. reader.Read ();
  623. object[] arr = null;
  624. int count = 0;
  625. arr = new object[1];
  626. count = reader.GetSqlValues (arr);
  627. // Something is wrong with types ... gotta figure it out
  628. //Assert.AreEqual (10, arr[0], "#1 Only first object shud be copied");
  629. Assert.AreEqual (1, count, "#1 return value shud equal objects copied");
  630. arr = new object[3];
  631. count = reader.GetSqlValues (arr);
  632. Assert.AreEqual (3, count, "#2 return value shud equal objects copied");
  633. arr = new object[5];
  634. count = reader.GetSqlValues (arr);
  635. Assert.AreEqual (3, count, "#3 return value shud equal objects copied");
  636. Assert.IsNull (arr[3], "#4 Only 3 objects shud be copied");
  637. reader.Close ();
  638. }
  639. [Test]
  640. public void isDBNullTest ()
  641. {
  642. cmd.CommandText = "select id , null from numeric_family where id=1";
  643. reader = cmd.ExecuteReader ();
  644. reader.Read ();
  645. Assert.IsFalse (reader.IsDBNull (0), "#1");
  646. Assert.IsTrue (reader.IsDBNull (1) , "#2");
  647. try {
  648. reader.IsDBNull (10);
  649. Assert.Fail ("#1 Invalid Argument");
  650. }catch (AssertionException e) {
  651. throw e;
  652. }catch (Exception e) {
  653. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  654. "#1 Incorrect Exception : " + e);
  655. }
  656. }
  657. [Test]
  658. public void ReadTest ()
  659. {
  660. cmd.CommandText = "select id, type_bit from numeric_family where id=1" ;
  661. reader = cmd.ExecuteReader ();
  662. Assert.IsTrue (reader.Read () , "#1");
  663. Assert.IsFalse (reader.Read (), "#2");
  664. reader.Close ();
  665. try {
  666. reader.Read ();
  667. Assert.Fail ("#3 Exception shud be thrown : Reader is closed");
  668. }catch (AssertionException e) {
  669. throw e;
  670. }catch (Exception e) {
  671. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  672. "#4 Incorrect Exception : " + e);
  673. }
  674. }
  675. [Test]
  676. public void NextResultTest ()
  677. {
  678. cmd.CommandText = "Select id from numeric_family where id=1";
  679. reader = cmd.ExecuteReader ();
  680. Assert.IsFalse (reader.NextResult (), "#1");
  681. reader.Close ();
  682. cmd.CommandText = "select id from numeric_family where id=1;";
  683. cmd.CommandText += "select type_bit from numeric_family where id=2;";
  684. reader = cmd.ExecuteReader ();
  685. Assert.IsTrue (reader.NextResult (), "#2");
  686. Assert.IsFalse (reader.NextResult (), "#3");
  687. reader.Close ();
  688. try {
  689. reader.NextResult ();
  690. Assert.Fail ("#4 Exception shud be thrown : Reader is closed");
  691. }catch (AssertionException e) {
  692. throw e;
  693. }catch (Exception e) {
  694. Assert.AreEqual (typeof(InvalidOperationException), e.GetType (),
  695. "#5 Incorrect Exception : " + e);
  696. }
  697. }
  698. [Test]
  699. public void GetNameTest ()
  700. {
  701. cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
  702. reader = cmd.ExecuteReader ();
  703. Assert.AreEqual ("id" , reader.GetName(0) , "#1");
  704. Assert.AreEqual ("gen" , reader.GetName(1) , "#2");
  705. try {
  706. reader.GetName (3);
  707. Assert.Fail ("#4 Exception shud be thrown");
  708. }catch (AssertionException e) {
  709. throw e;
  710. }catch (Exception e) {
  711. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  712. "#5 Incorrect Exception : " + e);
  713. }
  714. }
  715. [Test]
  716. public void GetOrdinalTest ()
  717. {
  718. //what is kana-width insensitive ?????
  719. cmd.CommandText = "Select id,10 as gen,20 from numeric_family where id=1";
  720. reader = cmd.ExecuteReader ();
  721. Assert.AreEqual (0, reader.GetOrdinal ("id"), "#1");
  722. Assert.AreEqual (0, reader.GetOrdinal ("ID"), "#2");
  723. Assert.AreEqual (1, reader.GetOrdinal ("gen"), "#3");
  724. // Would expect column1,columnn2 etc for unnamed columns,
  725. // but msdotnet return empty string for unnamed columns
  726. Assert.AreEqual (2, reader.GetOrdinal (""), "#4");
  727. try {
  728. reader.GetOrdinal ("invalidname");
  729. }catch (AssertionException e) {
  730. throw e;
  731. }catch (Exception e) {
  732. Assert.AreEqual (typeof (IndexOutOfRangeException),
  733. e.GetType(), "#4 Incorrect Exception : " + e);
  734. }
  735. }
  736. [Test]
  737. public void GetSchemaTableTest ()
  738. {
  739. cmd.CommandText = "Select type_decimal as decimal,id,10 ";
  740. cmd.CommandText += "from numeric_family where id=1";
  741. reader = cmd.ExecuteReader (CommandBehavior.KeyInfo);
  742. DataTable schemaTable = reader.GetSchemaTable ();
  743. DataRow row0 = schemaTable.Rows[0];
  744. DataRow row1 = schemaTable.Rows[1];
  745. Assert.AreEqual ("decimal", row0["ColumnName"], "#1");
  746. Assert.AreEqual ("", schemaTable.Rows[2]["ColumnName"], "#2");
  747. Assert.AreEqual (0, row0["ColumnOrdinal"], "#2");
  748. Assert.AreEqual (17, row0["ColumnSize"], "#3");
  749. Assert.AreEqual (38, row0["NumericPrecision"], "#4");
  750. Assert.AreEqual (0, row0["NumericScale"], "#5");
  751. Assert.AreEqual (false, row0["IsUnique"], "#6");
  752. // msdotnet returns IsUnique as false for Primary key
  753. // even though table consists of a single Primary Key
  754. //Assert.AreEqual (true, row1["IsUnique"], "#7");
  755. Assert.AreEqual (false, row0["IsKey"], "#8");
  756. Assert.AreEqual (true, row1["IsKey"], "#9");
  757. //Assert.AreEqual ("servername", row0["BaseServerName"], "#10");
  758. //Assert.AreEqual ("monotest", row0["BaseCatalogName"], "#11");
  759. Assert.AreEqual ("type_decimal", row0["BaseColumnName"], "#12");
  760. //Assert.IsNull(row0["BaseSchemaName"], "#13");
  761. Assert.AreEqual ("numeric_family", row0["BaseTableName"], "#14");
  762. Assert.AreEqual (typeof (Decimal), row0["DataType"], "#15");
  763. Assert.AreEqual (true, row0["AllowDBNull"], "#16");
  764. Assert.AreEqual (false, row1["AllowDBNull"], "#17");
  765. //Assert.IsNull(row0["ProviderType"], "#18");
  766. Assert.AreEqual (true, row0["IsAliased"], "#19");
  767. Assert.AreEqual (false, row1["IsAliased"], "#20");
  768. Assert.AreEqual (false, row0["IsExpression"], "#21");
  769. Assert.AreEqual (false, row0["IsIdentity"], "#22");
  770. Assert.AreEqual (false, row0["IsAutoIncrement"], "#23");
  771. Assert.AreEqual (false, row0["IsRowVersion"], "#24");
  772. Assert.AreEqual (false, row0["IsHidden"], "#25");
  773. Assert.AreEqual (false, row0["IsLong"], "#26");
  774. Assert.AreEqual (false, row0["IsReadOnly"], "#27");
  775. Assert.AreEqual (true, schemaTable.Rows[2]["IsReadOnly"], "#27");
  776. // Test Exception is thrown when reader is closed
  777. reader.Close ();
  778. try {
  779. reader.GetSchemaTable ();
  780. Assert.Fail ("#28 Exception shud be thrown" );
  781. }catch (AssertionException e) {
  782. throw e;
  783. }catch (Exception e) {
  784. Assert.AreEqual (typeof(InvalidOperationException), e.GetType(),
  785. "#29 Incorrect Exception");
  786. }
  787. }
  788. [Test]
  789. public void GetDataTypeNameTest ()
  790. {
  791. cmd.CommandText = "Select id, type_tinyint, 10,null from numeric_family where id=1";
  792. reader = cmd.ExecuteReader ();
  793. Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
  794. Assert.AreEqual ("int", reader.GetDataTypeName(2), "#2");
  795. //need check on windows
  796. Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
  797. try {
  798. reader.GetDataTypeName (10);
  799. Assert.Fail ("#4 Exception shud be thrown");
  800. }catch (AssertionException e) {
  801. throw e;
  802. }catch (Exception e) {
  803. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  804. "#5 Incorrect Exception : " + e);
  805. }
  806. }
  807. [Test]
  808. public void GetFieldTypeTest ()
  809. {
  810. cmd.CommandText = "Select id , type_tinyint, 10 , null from numeric_family where id=1";
  811. reader = cmd.ExecuteReader ();
  812. Assert.AreEqual ("tinyint", reader.GetDataTypeName(1), "#1");
  813. Assert.AreEqual ("int", reader.GetDataTypeName(2) , "#2");
  814. Assert.AreEqual ("int", reader.GetDataTypeName(3), "#3");
  815. try {
  816. reader.GetDataTypeName (10);
  817. Assert.Fail ("#4 Exception shud be thrown");
  818. }catch (AssertionException e) {
  819. throw e;
  820. }catch (Exception e) {
  821. Assert.AreEqual (typeof(IndexOutOfRangeException), e.GetType(),
  822. "#5 Incorrect Exception : " + e);
  823. }
  824. }
  825. // Need to populate the data from a config file
  826. // Will be replaced later
  827. void validateData(string sqlQuery, DataTable table)
  828. {
  829. string fmt = "#TAB[{0}] ROW[{1}] COL[{2}] Data Mismatch";
  830. int noOfColumns = table.Columns.Count ;
  831. int i=0;
  832. cmd.CommandText = sqlQuery ;
  833. reader = cmd.ExecuteReader ();
  834. while (reader.Read ()){
  835. for (int j=1; j< noOfColumns ; ++j)
  836. Assert.AreEqual (table.Rows[i][j], reader[j],
  837. String.Format (fmt, table.TableName, i+1, j));
  838. i++;
  839. }
  840. reader.Close ();
  841. }
  842. [Test]
  843. public void NumericDataValidation ()
  844. {
  845. validateData ("select * from numeric_family order by id ASC",
  846. numericDataTable);
  847. }
  848. [Test]
  849. public void StringDataValidation ()
  850. {
  851. validateData ("select * from string_family order by id ASC",
  852. stringDataTable);
  853. }
  854. [Test]
  855. public void BinaryDataValidation ()
  856. {
  857. validateData ("select * from binary_family order by id ASC",
  858. binaryDataTable);
  859. }
  860. [Test]
  861. public void DatetimeDataValidation ()
  862. {
  863. validateData ("select * from datetime_family order by id ASC",
  864. datetimeDataTable);
  865. }
  866. #if NET_2_0
  867. string connectionString = ConnectionManager.Singleton.ConnectionString;
  868. //FIXME : Add more test cases
  869. [Test]
  870. public void GetProviderSpecificFieldTypeTest ()
  871. {
  872. using (SqlConnection conn = new SqlConnection (connectionString)) {
  873. conn.Open ();
  874. SqlCommand cmd = conn.CreateCommand ();
  875. cmd.CommandText = "SELECT * FROM employee";
  876. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  877. rdr.Read();
  878. Assert.AreEqual (6, rdr.FieldCount, "#1");
  879. Assert.AreEqual(typeof(SqlInt32),rdr.GetProviderSpecificFieldType(0),"#2 The column at index 0 must have FieldType as SqlString");
  880. Assert.AreEqual(typeof(SqlString),rdr.GetProviderSpecificFieldType(1),"#3 The column at index 1 must have FieldType as SqlString");
  881. Assert.AreEqual(typeof(SqlDateTime),rdr.GetProviderSpecificFieldType(3),"#4 The column at index 3 must have FieldType as SqlString");
  882. }
  883. cmd.CommandText = "SELECT * FROM numeric_family";
  884. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  885. rdr.Read();
  886. Assert.AreEqual (12, rdr.FieldCount, "#5");
  887. Assert.AreEqual(typeof(SqlBoolean),rdr.GetProviderSpecificFieldType(1),"#6 The column at index 0 must have FieldType as SqlString");
  888. Assert.AreEqual(typeof(SqlByte),rdr.GetProviderSpecificFieldType(2),"#7 The column at index 1 must have FieldType as SqlString");
  889. Assert.AreEqual(typeof(SqlDecimal),rdr.GetProviderSpecificFieldType(6),"#8 The column at index 3 must have FieldType as SqlString");
  890. Assert.AreEqual(typeof(SqlMoney),rdr.GetProviderSpecificFieldType(8),"#9 The column at index 3 must have FieldType as SqlString");
  891. Assert.AreEqual(typeof(SqlSingle),rdr.GetProviderSpecificFieldType(10),"#10 The column at index 3 must have FieldType as SqlString");
  892. Assert.AreEqual(typeof(SqlDouble),rdr.GetProviderSpecificFieldType(11),"#11 The column at index 3 must have FieldType as SqlString");
  893. }
  894. }
  895. }
  896. //FIXME : Add more test cases
  897. [Test]
  898. public void GetProviderSpecificValueTest ()
  899. {
  900. using (SqlConnection conn = new SqlConnection (connectionString)) {
  901. conn.Open ();
  902. SqlCommand cmd = conn.CreateCommand ();
  903. cmd.CommandText = "SELECT * FROM employee";
  904. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  905. rdr.Read();
  906. Assert.AreEqual (6, rdr.FieldCount, "#1");
  907. Assert.AreEqual((SqlInt32)1,rdr.GetProviderSpecificValue(0),"#2 The column at index 0 must have FieldType as SqlString");
  908. Assert.AreEqual((SqlString)"suresh",rdr.GetProviderSpecificValue(1),"#3 The column at index 1 must have FieldType as SqlString");
  909. Assert.AreEqual((SqlDateTime)DateTime.Parse("8/22/1978"),rdr.GetProviderSpecificValue(3),"#4 The column at index 3 must have FieldType as SqlString");
  910. }
  911. }
  912. }
  913. [Test]
  914. public void GetProviderSpecificValueLowerBoundaryTest ()
  915. {
  916. using (SqlConnection conn = new SqlConnection (connectionString)) {
  917. conn.Open ();
  918. SqlCommand cmd = conn.CreateCommand ();
  919. cmd.CommandText = "SELECT * FROM employee";
  920. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  921. rdr.Read();
  922. Assert.AreEqual (6, rdr.FieldCount, "#1");
  923. try {
  924. Assert.AreEqual((SqlInt32)1,rdr.GetProviderSpecificValue(-1),"#2 The column at index 0 must have FieldType as SqlString");
  925. } catch (IndexOutOfRangeException) {
  926. Assert.AreEqual((SqlInt32)1,rdr.GetProviderSpecificValue(0),"#2 The column at index 0 must have FieldType as SqlString");
  927. Assert.AreEqual((SqlString)"suresh",rdr.GetProviderSpecificValue(1),"#3 The column at index 1 must have FieldType as SqlString");
  928. Assert.AreEqual((SqlDateTime)DateTime.Parse("8/22/1978"),rdr.GetProviderSpecificValue(3),"#4 The column at index 3 must have FieldType as SqlString");
  929. return;
  930. }
  931. Assert.Fail ("Expected exception IndexOutOfRangeException was not thrown");
  932. }
  933. }
  934. }
  935. [Test]
  936. public void GetProviderSpecificValueUpperBoundaryTest ()
  937. {
  938. using (SqlConnection conn = new SqlConnection (connectionString)) {
  939. conn.Open ();
  940. SqlCommand cmd = conn.CreateCommand ();
  941. cmd.CommandText = "SELECT * FROM employee";
  942. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  943. rdr.Read();
  944. Assert.AreEqual (6, rdr.FieldCount, "#1");
  945. try {
  946. Assert.AreEqual((SqlInt32)1,rdr.GetProviderSpecificValue(rdr.FieldCount),"#2 The column at index 0 must have FieldType as SqlString");
  947. } catch (IndexOutOfRangeException) {
  948. Assert.AreEqual((SqlInt32)1,rdr.GetProviderSpecificValue(0),"#2 The column at index 0 must have FieldType as SqlString");
  949. Assert.AreEqual((SqlString)"suresh",rdr.GetProviderSpecificValue(1),"#3 The column at index 1 must have FieldType as SqlString");
  950. Assert.AreEqual((SqlDateTime)DateTime.Parse("8/22/1978"),rdr.GetProviderSpecificValue(3),"#4 The column at index 3 must have FieldType as SqlString");
  951. return;
  952. }
  953. Assert.Fail ("Expected exception IndexOutOfRangeException was not thrown");
  954. }
  955. }
  956. }
  957. //FIXME : Add more test cases
  958. [Test]
  959. public void GetProviderSpecificValuesTest ()
  960. {
  961. using (SqlConnection conn = new SqlConnection (connectionString)) {
  962. conn.Open ();
  963. SqlCommand cmd = conn.CreateCommand ();
  964. cmd.CommandText = "SELECT * FROM employee";
  965. Object [] psValues = new Object[6];
  966. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  967. rdr.Read();
  968. rdr.GetProviderSpecificValues (psValues);
  969. Assert.AreEqual ((SqlInt32)1, psValues[0], "#");
  970. Assert.AreEqual ((SqlString)"suresh", psValues[1], "#");
  971. Assert.AreEqual ((SqlDateTime)DateTime.Parse("8/22/1978"), psValues[3], "#");
  972. }
  973. }
  974. }
  975. [Test]
  976. public void GetProviderSpecificValuesSmallArrayTest ()
  977. {
  978. using (SqlConnection conn = new SqlConnection (connectionString)) {
  979. conn.Open ();
  980. SqlCommand cmd = conn.CreateCommand ();
  981. cmd.CommandText = "SELECT * FROM employee";
  982. Object [] psValues = new Object[2];
  983. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  984. rdr.Read();
  985. rdr.GetProviderSpecificValues (psValues);
  986. Assert.AreEqual ((SqlInt32)1, psValues[0], "#");
  987. Assert.AreEqual ((SqlString)"suresh", psValues[1], "#");
  988. }
  989. }
  990. }
  991. [Test]
  992. public void GetProviderSpecificValuesLargeArrayTest ()
  993. {
  994. using (SqlConnection conn = new SqlConnection (connectionString)) {
  995. conn.Open ();
  996. SqlCommand cmd = conn.CreateCommand ();
  997. cmd.CommandText = "SELECT * FROM employee";
  998. Object [] psValues = new Object[10];
  999. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  1000. rdr.Read();
  1001. rdr.GetProviderSpecificValues (psValues);
  1002. Assert.AreEqual ((SqlInt32)1, psValues[0], "#");
  1003. Assert.AreEqual ((SqlString)"suresh", psValues[1], "#");
  1004. Assert.AreEqual ((SqlDateTime)DateTime.Parse("8/22/1978"), psValues[3], "#");
  1005. Assert.AreEqual (null, psValues[6], "#");
  1006. Assert.AreEqual (null, psValues[9], "#");
  1007. }
  1008. }
  1009. }
  1010. [Test]
  1011. public void GetProviderSpecificValuesNullTest ()
  1012. {
  1013. using (SqlConnection conn = new SqlConnection (connectionString)) {
  1014. conn.Open ();
  1015. SqlCommand cmd = conn.CreateCommand ();
  1016. cmd.CommandText = "SELECT * FROM employee";
  1017. Object [] psValues = new Object[2];
  1018. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  1019. rdr.Read();
  1020. try {
  1021. rdr.GetProviderSpecificValues (null);
  1022. } catch (NullReferenceException) {
  1023. Assert.AreEqual (null, psValues[0], "#");
  1024. Assert.AreEqual (null, psValues[1], "#");
  1025. return;
  1026. }
  1027. Assert.Fail ("Expected exception NullReferenceException was not thrown");
  1028. }
  1029. }
  1030. }
  1031. [Test]
  1032. public void GetSqlBytesTest ()
  1033. {
  1034. using (SqlConnection conn = new SqlConnection (connectionString)) {
  1035. conn.Open ();
  1036. SqlCommand cmd = conn.CreateCommand ();
  1037. cmd.CommandText = "SELECT * FROM binary_family";
  1038. using (SqlDataReader rdr = cmd.ExecuteReader ()) {
  1039. rdr.Read();
  1040. Assert.AreEqual (7, rdr.FieldCount, "#1");
  1041. SqlBytes sb = rdr.GetSqlBytes (1);
  1042. long byteCount = sb.Length;
  1043. Assert.AreEqual (1, byteCount, "#");
  1044. Assert.AreEqual (53, sb[0], "#");
  1045. sb = rdr.GetSqlBytes (2);
  1046. Assert.AreEqual (typeof(SqlBinary), rdr.GetSqlValue(2).GetType(), "#");
  1047. byteCount = sb.Length;
  1048. Assert.AreEqual (30, byteCount, "#");
  1049. Assert.AreEqual (48, sb[0], "#");
  1050. Assert.AreEqual (49, sb[1], "#");
  1051. Assert.AreEqual (50, sb[2], "#");
  1052. Assert.AreEqual (53, sb[15], "#");
  1053. Assert.AreEqual (57, sb[29], "#");
  1054. }
  1055. }
  1056. }
  1057. #endif // NET_2_0
  1058. }
  1059. }