SqlDataReader.cs 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. //
  2. // System.Data.SqlClient.SqlDataReader.cs
  3. //
  4. // Author:
  5. // Rodrigo Moya ([email protected])
  6. // Daniel Morgan ([email protected])
  7. // Tim Coleman ([email protected])
  8. //
  9. // (C) Ximian, Inc 2002
  10. // (C) Daniel Morgan 2002
  11. // Copyright (C) Tim Coleman, 2002
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. using Mono.Data.Tds.Protocol;
  36. using System;
  37. using System.IO;
  38. using System.Text;
  39. using System.Collections;
  40. using System.ComponentModel;
  41. using System.Data;
  42. using System.Data.Common;
  43. using System.Data.SqlTypes;
  44. using System.Globalization;
  45. using System.Xml;
  46. namespace System.Data.SqlClient
  47. {
  48. #if NET_2_0
  49. public class SqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord
  50. #else
  51. public sealed class SqlDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  52. #endif // NET_2_0
  53. {
  54. #region Fields
  55. SqlCommand command;
  56. bool disposed;
  57. bool isClosed;
  58. bool moreResults;
  59. int resultsRead;
  60. int rowsRead;
  61. DataTable schemaTable;
  62. bool haveRead;
  63. bool readResult;
  64. bool readResultUsed;
  65. #if NET_2_0
  66. int visibleFieldCount;
  67. #endif
  68. #endregion // Fields
  69. const int COLUMN_NAME_IDX = 0;
  70. const int COLUMN_ORDINAL_IDX = 1;
  71. const int COLUMN_SIZE_IDX = 2;
  72. const int NUMERIC_PRECISION_IDX = 3;
  73. const int NUMERIC_SCALE_IDX = 4;
  74. const int IS_UNIQUE_IDX = 5;
  75. const int IS_KEY_IDX = 6;
  76. const int BASE_SERVER_NAME_IDX = 7;
  77. const int BASE_CATALOG_NAME_IDX = 8;
  78. const int BASE_COLUMN_NAME_IDX = 9;
  79. const int BASE_SCHEMA_NAME_IDX = 10;
  80. const int BASE_TABLE_NAME_IDX = 11;
  81. const int DATA_TYPE_IDX = 12;
  82. const int ALLOW_DBNULL_IDX = 13;
  83. const int PROVIDER_TYPE_IDX = 14;
  84. const int IS_ALIASED_IDX = 15;
  85. const int IS_EXPRESSION_IDX = 16;
  86. const int IS_IDENTITY_IDX = 17;
  87. const int IS_AUTO_INCREMENT_IDX = 18;
  88. const int IS_ROW_VERSION_IDX = 19;
  89. const int IS_HIDDEN_IDX = 20;
  90. const int IS_LONG_IDX = 21;
  91. const int IS_READ_ONLY_IDX = 22;
  92. #region Constructors
  93. internal SqlDataReader (SqlCommand command)
  94. {
  95. this.command = command;
  96. command.Tds.RecordsAffected = -1;
  97. NextResult ();
  98. }
  99. #endregion // Constructors
  100. #region Properties
  101. public
  102. #if NET_2_0
  103. override
  104. #endif // NET_2_0
  105. int Depth {
  106. get { return 0; }
  107. }
  108. public
  109. #if NET_2_0
  110. override
  111. #endif // NET_2_0
  112. int FieldCount {
  113. get {
  114. ValidateState ();
  115. return command.Tds.Columns.Count;
  116. }
  117. }
  118. public
  119. #if NET_2_0
  120. override
  121. #endif // NET_2_0
  122. bool IsClosed {
  123. get { return isClosed; }
  124. }
  125. public
  126. #if NET_2_0
  127. override
  128. #endif // NET_2_0
  129. object this [int i] {
  130. get { return GetValue (i); }
  131. }
  132. public
  133. #if NET_2_0
  134. override
  135. #endif // NET_2_0
  136. object this [string name] {
  137. get { return GetValue (GetOrdinal (name)); }
  138. }
  139. public
  140. #if NET_2_0
  141. override
  142. #endif // NET_2_0
  143. int RecordsAffected {
  144. get {
  145. return command.Tds.RecordsAffected;
  146. }
  147. }
  148. public
  149. #if NET_2_0
  150. override
  151. #endif // NET_2_0
  152. bool HasRows {
  153. get {
  154. ValidateState ();
  155. if (rowsRead > 0)
  156. return true;
  157. if (!haveRead)
  158. readResult = ReadRecord ();
  159. return readResult;
  160. }
  161. }
  162. #if NET_2_0
  163. public override int VisibleFieldCount {
  164. get { return visibleFieldCount; }
  165. }
  166. protected SqlConnection Connection {
  167. get { return command.Connection; }
  168. }
  169. protected bool IsCommandBehavior (CommandBehavior condition) {
  170. return condition == command.CommandBehavior;
  171. }
  172. #endif
  173. #endregion // Properties
  174. #region Methods
  175. public
  176. #if NET_2_0
  177. override
  178. #endif // NET_2_0
  179. void Close ()
  180. {
  181. if (IsClosed)
  182. return;
  183. // skip to end & read output parameters.
  184. while (NextResult ())
  185. ;
  186. isClosed = true;
  187. command.CloseDataReader ();
  188. }
  189. private static DataTable ConstructSchemaTable ()
  190. {
  191. Type booleanType = typeof (bool);
  192. Type stringType = typeof (string);
  193. Type intType = typeof (int);
  194. #if NET_2_0
  195. Type typeType = typeof (Type);
  196. #endif
  197. Type shortType = typeof (short);
  198. DataTable schemaTable = new DataTable ("SchemaTable");
  199. schemaTable.Columns.Add ("ColumnName", stringType);
  200. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  201. schemaTable.Columns.Add ("ColumnSize", intType);
  202. schemaTable.Columns.Add ("NumericPrecision", shortType);
  203. schemaTable.Columns.Add ("NumericScale", shortType);
  204. schemaTable.Columns.Add ("IsUnique", booleanType);
  205. schemaTable.Columns.Add ("IsKey", booleanType);
  206. schemaTable.Columns.Add ("BaseServerName", stringType);
  207. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  208. schemaTable.Columns.Add ("BaseColumnName", stringType);
  209. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  210. schemaTable.Columns.Add ("BaseTableName", stringType);
  211. #if NET_2_0
  212. schemaTable.Columns.Add ("DataType", typeType);
  213. #else
  214. schemaTable.Columns.Add ("DataType", typeof (object));
  215. #endif
  216. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  217. schemaTable.Columns.Add ("ProviderType", intType);
  218. schemaTable.Columns.Add ("IsAliased", booleanType);
  219. schemaTable.Columns.Add ("IsExpression", booleanType);
  220. schemaTable.Columns.Add ("IsIdentity", booleanType);
  221. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  222. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  223. schemaTable.Columns.Add ("IsHidden", booleanType);
  224. schemaTable.Columns.Add ("IsLong", booleanType);
  225. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  226. return schemaTable;
  227. }
  228. private string GetSchemaRowTypeName (TdsColumnType ctype, int csize, short precision, short scale)
  229. {
  230. int dbType;
  231. bool isLong;
  232. Type fieldType;
  233. string typeName;
  234. GetSchemaRowType (ctype, csize, precision, scale,
  235. out dbType, out fieldType, out isLong,
  236. out typeName);
  237. return typeName;
  238. }
  239. private Type GetSchemaRowFieldType (TdsColumnType ctype, int csize, short precision, short scale)
  240. {
  241. int dbType;
  242. bool isLong;
  243. Type fieldType;
  244. string typeName;
  245. GetSchemaRowType (ctype, csize, precision, scale,
  246. out dbType, out fieldType, out isLong,
  247. out typeName);
  248. return fieldType;
  249. }
  250. SqlDbType GetSchemaRowDbType (int ordinal)
  251. {
  252. int csize;
  253. short precision, scale;
  254. TdsColumnType ctype;
  255. TdsDataColumn column;
  256. if (ordinal < 0 || ordinal >= command.Tds.Columns.Count)
  257. throw new IndexOutOfRangeException ();
  258. column = command.Tds.Columns [ordinal];
  259. #if NET_2_0
  260. ctype = (TdsColumnType) column.ColumnType;
  261. csize = (int) column.ColumnSize;
  262. precision = (short) (column.NumericPrecision ?? 0);
  263. scale = (short) (column.NumericScale ?? 0);
  264. #else
  265. ctype = (TdsColumnType) column ["ColumnType"];
  266. csize = (int) column ["ColumnSize"];
  267. precision = (short) ((byte) column ["NumericPrecision"]);
  268. scale = (short) ((byte) column ["NumericScale"]);
  269. #endif
  270. return GetSchemaRowDbType (ctype, csize, precision, scale);
  271. }
  272. private SqlDbType GetSchemaRowDbType (TdsColumnType ctype, int csize, short precision, short scale)
  273. {
  274. Type fieldType;
  275. bool isLong;
  276. string typeName;
  277. int dbType;
  278. GetSchemaRowType (ctype, csize, precision, scale,
  279. out dbType, out fieldType, out isLong,
  280. out typeName);
  281. return (SqlDbType) dbType;
  282. }
  283. private void GetSchemaRowType (TdsColumnType ctype, int csize,
  284. short precision, short scale,
  285. out int dbType, out Type fieldType,
  286. out bool isLong, out string typeName)
  287. {
  288. dbType = -1;
  289. typeName = string.Empty;
  290. isLong = false;
  291. fieldType = typeof (Type);
  292. switch (ctype) {
  293. case TdsColumnType.Int1:
  294. case TdsColumnType.Int2:
  295. case TdsColumnType.Int4:
  296. case TdsColumnType.IntN:
  297. case TdsColumnType.BigInt:
  298. switch (csize) {
  299. case 1:
  300. typeName = "tinyint";
  301. dbType = (int) SqlDbType.TinyInt;
  302. fieldType = typeof (byte);
  303. isLong = false;
  304. break;
  305. case 2:
  306. typeName = "smallint";
  307. dbType = (int) SqlDbType.SmallInt;
  308. fieldType = typeof (short);
  309. isLong = false;
  310. break;
  311. case 4:
  312. typeName = "int";
  313. dbType = (int) SqlDbType.Int;
  314. fieldType = typeof (int);
  315. isLong = false;
  316. break;
  317. case 8:
  318. typeName = "bigint";
  319. dbType = (int) SqlDbType.BigInt;
  320. fieldType = typeof (long);
  321. isLong = false;
  322. break;
  323. }
  324. break;
  325. case TdsColumnType.Real:
  326. case TdsColumnType.Float8:
  327. case TdsColumnType.FloatN:
  328. switch (csize) {
  329. case 4:
  330. typeName = "real";
  331. dbType = (int) SqlDbType.Real;
  332. fieldType = typeof (float);
  333. isLong = false;
  334. break;
  335. case 8:
  336. typeName = "float";
  337. dbType = (int) SqlDbType.Float;
  338. fieldType = typeof (double);
  339. isLong = false;
  340. break;
  341. }
  342. break;
  343. case TdsColumnType.Image :
  344. typeName = "image";
  345. dbType = (int) SqlDbType.Image;
  346. fieldType = typeof (byte[]);
  347. isLong = true;
  348. break;
  349. case TdsColumnType.Text :
  350. typeName = "text";
  351. dbType = (int) SqlDbType.Text;
  352. fieldType = typeof (string);
  353. isLong = true;
  354. break;
  355. case TdsColumnType.UniqueIdentifier :
  356. typeName = "uniqueidentifier";
  357. dbType = (int) SqlDbType.UniqueIdentifier;
  358. fieldType = typeof (Guid);
  359. isLong = false;
  360. break;
  361. case TdsColumnType.VarBinary :
  362. case TdsColumnType.BigVarBinary :
  363. typeName = "varbinary";
  364. dbType = (int) SqlDbType.VarBinary;
  365. fieldType = typeof (byte[]);
  366. isLong = false;
  367. break;
  368. case TdsColumnType.VarChar :
  369. case TdsColumnType.BigVarChar :
  370. typeName = "varchar";
  371. dbType = (int) SqlDbType.VarChar;
  372. fieldType = typeof (string);
  373. isLong = false;
  374. break;
  375. case TdsColumnType.Binary :
  376. case TdsColumnType.BigBinary :
  377. typeName = "binary";
  378. dbType = (int) SqlDbType.Binary;
  379. fieldType = typeof (byte[]);
  380. isLong = false;
  381. break;
  382. case TdsColumnType.Char :
  383. case TdsColumnType.BigChar :
  384. typeName = "char";
  385. dbType = (int) SqlDbType.Char;
  386. fieldType = typeof (string);
  387. isLong = false;
  388. break;
  389. case TdsColumnType.Bit :
  390. case TdsColumnType.BitN :
  391. typeName = "bit";
  392. dbType = (int) SqlDbType.Bit;
  393. fieldType = typeof (bool);
  394. isLong = false;
  395. break;
  396. case TdsColumnType.DateTime4 :
  397. case TdsColumnType.DateTime :
  398. case TdsColumnType.DateTimeN :
  399. switch (csize) {
  400. case 4:
  401. typeName = "smalldatetime";
  402. dbType = (int) SqlDbType.SmallDateTime;
  403. fieldType = typeof (DateTime);
  404. isLong = false;
  405. break;
  406. case 8:
  407. typeName = "datetime";
  408. dbType = (int) SqlDbType.DateTime;
  409. fieldType = typeof (DateTime);
  410. isLong = false;
  411. break;
  412. }
  413. break;
  414. case TdsColumnType.Money :
  415. case TdsColumnType.MoneyN :
  416. case TdsColumnType.Money4 :
  417. switch (csize) {
  418. case 4:
  419. typeName = "smallmoney";
  420. dbType = (int) SqlDbType.SmallMoney;
  421. fieldType = typeof (decimal);
  422. isLong = false;
  423. break;
  424. case 8:
  425. typeName = "money";
  426. dbType = (int) SqlDbType.Money;
  427. fieldType = typeof (decimal);
  428. isLong = false;
  429. break;
  430. }
  431. break;
  432. case TdsColumnType.NText :
  433. typeName = "ntext";
  434. dbType = (int) SqlDbType.NText;
  435. fieldType = typeof (string);
  436. isLong = true;
  437. break;
  438. case TdsColumnType.NVarChar :
  439. typeName = "nvarchar";
  440. dbType = (int) SqlDbType.NVarChar;
  441. fieldType = typeof (string);
  442. isLong = false;
  443. break;
  444. case TdsColumnType.Decimal :
  445. case TdsColumnType.Numeric :
  446. // TDS 7.0 returns bigint as decimal(19,0)
  447. if (precision == 19 && scale == 0) {
  448. typeName = "bigint";
  449. dbType = (int) SqlDbType.BigInt;
  450. fieldType = typeof (long);
  451. } else {
  452. typeName = "decimal";
  453. dbType = (int) SqlDbType.Decimal;
  454. fieldType = typeof (decimal);
  455. }
  456. isLong = false;
  457. break;
  458. case TdsColumnType.NChar :
  459. typeName = "nchar";
  460. dbType = (int) SqlDbType.NChar;
  461. fieldType = typeof (string);
  462. isLong = false;
  463. break;
  464. case TdsColumnType.SmallMoney :
  465. typeName = "smallmoney";
  466. dbType = (int) SqlDbType.SmallMoney;
  467. fieldType = typeof (decimal);
  468. isLong = false;
  469. break;
  470. default :
  471. typeName = "variant";
  472. dbType = (int) SqlDbType.Variant;
  473. fieldType = typeof (object);
  474. isLong = false;
  475. break;
  476. }
  477. }
  478. #if NET_2_0
  479. new
  480. #endif
  481. void Dispose (bool disposing)
  482. {
  483. if (!disposed) {
  484. if (disposing) {
  485. if (schemaTable != null)
  486. schemaTable.Dispose ();
  487. Close ();
  488. command = null;
  489. }
  490. disposed = true;
  491. }
  492. }
  493. public
  494. #if NET_2_0
  495. override
  496. #endif // NET_2_0
  497. bool GetBoolean (int i)
  498. {
  499. object value = GetValue (i);
  500. if (!(value is bool)) {
  501. if (value is DBNull) throw new SqlNullValueException ();
  502. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  503. }
  504. return (bool) value;
  505. }
  506. public
  507. #if NET_2_0
  508. override
  509. #endif // NET_2_0
  510. byte GetByte (int i)
  511. {
  512. object value = GetValue (i);
  513. if (!(value is byte)) {
  514. if (value is DBNull) throw new SqlNullValueException ();
  515. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  516. }
  517. return (byte) value;
  518. }
  519. public
  520. #if NET_2_0
  521. override
  522. #endif // NET_2_0
  523. long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  524. {
  525. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  526. ValidateState ();
  527. EnsureDataAvailable ();
  528. try {
  529. long len = ((Tds)command.Tds).GetSequentialColumnValue (i, dataIndex, buffer, bufferIndex, length);
  530. if (len == -1)
  531. throw CreateGetBytesOnInvalidColumnTypeException (i);
  532. if (len == -2)
  533. #if NET_2_0
  534. throw new SqlNullValueException ();
  535. #else
  536. return 0;
  537. #endif
  538. return len;
  539. } catch (TdsInternalException ex) {
  540. command.Connection.Close ();
  541. throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
  542. }
  543. }
  544. object value = GetValue (i);
  545. if (!(value is byte [])) {
  546. SqlDbType type = GetSchemaRowDbType (i);
  547. switch (type) {
  548. case SqlDbType.Image:
  549. if (value is DBNull)
  550. throw new SqlNullValueException ();
  551. break;
  552. case SqlDbType.Text:
  553. #if NET_2_0
  554. string text = value as string;
  555. if (text != null)
  556. value = Encoding.Default.GetBytes (text);
  557. else
  558. value = null;
  559. break;
  560. #else
  561. throw new InvalidCastException ();
  562. #endif
  563. case SqlDbType.NText:
  564. #if NET_2_0
  565. string ntext = value as string;
  566. if (ntext != null)
  567. value = Encoding.Unicode.GetBytes (ntext);
  568. else
  569. value = null;
  570. break;
  571. #else
  572. throw new InvalidCastException ();
  573. #endif
  574. default:
  575. throw CreateGetBytesOnInvalidColumnTypeException (i);
  576. }
  577. }
  578. if (buffer == null)
  579. return ((byte []) value).Length; // Return length of data
  580. // Copy data into buffer
  581. int availLen = (int) ( ( (byte []) value).Length - dataIndex);
  582. if (availLen < length)
  583. length = availLen;
  584. if (dataIndex < 0)
  585. return 0;
  586. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  587. return length; // return actual read count
  588. }
  589. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  590. public
  591. #if NET_2_0
  592. override
  593. #endif // NET_2_0
  594. char GetChar (int i)
  595. {
  596. throw new NotSupportedException ();
  597. }
  598. public
  599. #if NET_2_0
  600. override
  601. #endif // NET_2_0
  602. long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  603. {
  604. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  605. ValidateState ();
  606. EnsureDataAvailable ();
  607. if (i < 0 || i >= command.Tds.Columns.Count)
  608. throw new IndexOutOfRangeException ();
  609. Encoding encoding = null;
  610. byte mul = 1;
  611. TdsColumnType colType = (TdsColumnType) command.Tds.Columns[i]["ColumnType"];
  612. switch (colType) {
  613. case TdsColumnType.Text :
  614. case TdsColumnType.VarChar:
  615. case TdsColumnType.Char:
  616. case TdsColumnType.BigVarChar:
  617. encoding = Encoding.ASCII;
  618. break;
  619. case TdsColumnType.NText :
  620. case TdsColumnType.NVarChar:
  621. case TdsColumnType.NChar:
  622. encoding = Encoding.Unicode;
  623. mul = 2;
  624. break;
  625. default :
  626. return -1;
  627. }
  628. long count = 0;
  629. if (buffer == null) {
  630. count = GetBytes (i,0,(byte[]) null,0,0);
  631. return (count/mul);
  632. }
  633. length *= mul;
  634. byte[] arr = new byte [length];
  635. count = GetBytes (i, dataIndex, arr, 0, length);
  636. if (count == -1)
  637. throw new InvalidCastException ("Specified cast is not valid");
  638. Char[] val = encoding.GetChars (arr, 0, (int)count);
  639. val.CopyTo (buffer, bufferIndex);
  640. return val.Length;
  641. }
  642. char [] valueBuffer;
  643. object value = GetValue (i);
  644. if (value is char[])
  645. valueBuffer = (char[])value;
  646. else if (value is string)
  647. valueBuffer = ((string)value).ToCharArray();
  648. else {
  649. if (value is DBNull) throw new SqlNullValueException ();
  650. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  651. }
  652. if ( buffer == null ) {
  653. // Return length of data
  654. return valueBuffer.Length;
  655. }
  656. else {
  657. // Copy data into buffer
  658. Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
  659. return valueBuffer.Length - dataIndex;
  660. }
  661. }
  662. #if !NET_2_0
  663. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  664. public new IDataReader GetData (int i)
  665. {
  666. return ((IDataReader) this [i]);
  667. }
  668. #endif
  669. public
  670. #if NET_2_0
  671. override
  672. #endif // NET_2_0
  673. string GetDataTypeName (int i)
  674. {
  675. TdsDataColumn column;
  676. TdsColumnType ctype;
  677. string datatypeName;
  678. int csize;
  679. short precision;
  680. short scale;
  681. ValidateState ();
  682. if (i < 0 || i >= command.Tds.Columns.Count)
  683. throw new IndexOutOfRangeException ();
  684. column = command.Tds.Columns [i];
  685. #if NET_2_0
  686. ctype = (TdsColumnType) column.ColumnType;
  687. csize = (int) column.ColumnSize;
  688. precision = (short) (column.NumericPrecision ?? 0);
  689. scale = (short) (column.NumericScale ?? 0);
  690. #else
  691. ctype = (TdsColumnType) column ["ColumnType"];
  692. csize = (int) column ["ColumnSize"];
  693. precision = (short) ((byte) column ["NumericPrecision"]);
  694. scale = (short) ((byte) column ["NumericScale"]);
  695. #endif
  696. return GetSchemaRowTypeName (ctype, csize, precision, scale);
  697. }
  698. public
  699. #if NET_2_0
  700. override
  701. #endif // NET_2_0
  702. DateTime GetDateTime (int i)
  703. {
  704. object value = GetValue (i);
  705. if (!(value is DateTime)) {
  706. if (value is DBNull) throw new SqlNullValueException ();
  707. else throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  708. }
  709. return (DateTime) value;
  710. }
  711. public
  712. #if NET_2_0
  713. override
  714. #endif // NET_2_0
  715. decimal GetDecimal (int i)
  716. {
  717. object value = GetValue (i);
  718. if (!(value is decimal)) {
  719. if (value is DBNull) throw new SqlNullValueException ();
  720. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  721. }
  722. return (decimal) value;
  723. }
  724. public
  725. #if NET_2_0
  726. override
  727. #endif // NET_2_0
  728. double GetDouble (int i)
  729. {
  730. object value = GetValue (i);
  731. if (!(value is double)) {
  732. if (value is DBNull) throw new SqlNullValueException ();
  733. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  734. }
  735. return (double) value;
  736. }
  737. public
  738. #if NET_2_0
  739. override
  740. #endif // NET_2_0
  741. Type GetFieldType (int i)
  742. {
  743. TdsDataColumn column;
  744. TdsColumnType ctype;
  745. int csize;
  746. short precision;
  747. short scale;
  748. ValidateState ();
  749. if (i < 0 || i >= command.Tds.Columns.Count)
  750. throw new IndexOutOfRangeException ();
  751. column = command.Tds.Columns [i];
  752. #if NET_2_0
  753. ctype = (TdsColumnType) column.ColumnType;
  754. csize = (int) column.ColumnSize;
  755. precision = (short) (column.NumericPrecision ?? 0);
  756. scale = (short) (column.NumericScale ?? 0);
  757. #else
  758. ctype = (TdsColumnType) column ["ColumnType"];
  759. csize = (int) column ["ColumnSize"];
  760. precision = (short) ((byte) column ["NumericPrecision"]);
  761. scale = (short) ((byte) column ["NumericScale"]);
  762. #endif
  763. return GetSchemaRowFieldType (ctype, csize, precision,
  764. scale);
  765. }
  766. public
  767. #if NET_2_0
  768. override
  769. #endif // NET_2_0
  770. float GetFloat (int i)
  771. {
  772. object value = GetValue (i);
  773. if (!(value is float)) {
  774. if (value is DBNull) throw new SqlNullValueException ();
  775. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  776. }
  777. return (float) value;
  778. }
  779. public
  780. #if NET_2_0
  781. override
  782. #endif // NET_2_0
  783. Guid GetGuid (int i)
  784. {
  785. object value = GetValue (i);
  786. if (!(value is Guid)) {
  787. if (value is DBNull) throw new SqlNullValueException ();
  788. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  789. }
  790. return (Guid) value;
  791. }
  792. public
  793. #if NET_2_0
  794. override
  795. #endif // NET_2_0
  796. short GetInt16 (int i)
  797. {
  798. object value = GetValue (i);
  799. if (!(value is short)) {
  800. if (value is DBNull) throw new SqlNullValueException ();
  801. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  802. }
  803. return (short) value;
  804. }
  805. public
  806. #if NET_2_0
  807. override
  808. #endif // NET_2_0
  809. int GetInt32 (int i)
  810. {
  811. object value = GetValue (i);
  812. if (!(value is int)) {
  813. if (value is DBNull) throw new SqlNullValueException ();
  814. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  815. }
  816. return (int) value;
  817. }
  818. public
  819. #if NET_2_0
  820. override
  821. #endif // NET_2_0
  822. long GetInt64 (int i)
  823. {
  824. object value = GetValue (i);
  825. if (!(value is long)) {
  826. if (value is DBNull) throw new SqlNullValueException ();
  827. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  828. }
  829. return (long) value;
  830. }
  831. public
  832. #if NET_2_0
  833. override
  834. #endif // NET_2_0
  835. string GetName (int i)
  836. {
  837. ValidateState ();
  838. if (i < 0 || i >= command.Tds.Columns.Count)
  839. throw new IndexOutOfRangeException ();
  840. #if NET_2_0
  841. return (string) command.Tds.Columns[i].ColumnName;
  842. #else
  843. return (string) command.Tds.Columns[i]["ColumnName"];
  844. #endif
  845. }
  846. public
  847. #if NET_2_0
  848. override
  849. #endif // NET_2_0
  850. int GetOrdinal (string name)
  851. {
  852. ValidateState ();
  853. if (name == null)
  854. throw new ArgumentNullException ("fieldName");
  855. string colName;
  856. foreach (TdsDataColumn schema in command.Tds.Columns) {
  857. #if NET_2_0
  858. colName = schema.ColumnName;
  859. if (colName.Equals (name) || String.Compare (colName, name, true) == 0)
  860. return (int) schema.ColumnOrdinal;
  861. #else
  862. colName = (string) schema["ColumnName"];
  863. if (colName.Equals (name) || String.Compare (colName, name, true) == 0)
  864. return (int) schema["ColumnOrdinal"];
  865. #endif
  866. }
  867. throw new IndexOutOfRangeException ();
  868. }
  869. public
  870. #if NET_2_0
  871. override
  872. #endif // NET_2_0
  873. DataTable GetSchemaTable ()
  874. {
  875. ValidateState ();
  876. if (schemaTable == null)
  877. schemaTable = ConstructSchemaTable ();
  878. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  879. return schemaTable;
  880. if (!moreResults)
  881. return null;
  882. foreach (TdsDataColumn schema in command.Tds.Columns) {
  883. DataRow row = schemaTable.NewRow ();
  884. #if NET_2_0
  885. row [COLUMN_NAME_IDX] = GetSchemaValue (schema.ColumnName);
  886. row [COLUMN_ORDINAL_IDX] = GetSchemaValue (schema.ColumnOrdinal);
  887. row [IS_UNIQUE_IDX] = GetSchemaValue (schema.IsUnique);
  888. row [IS_AUTO_INCREMENT_IDX] = GetSchemaValue (schema.IsAutoIncrement);
  889. row [IS_ROW_VERSION_IDX] = GetSchemaValue (schema.IsRowVersion);
  890. row [IS_HIDDEN_IDX] = GetSchemaValue (schema.IsHidden);
  891. row [IS_IDENTITY_IDX] = GetSchemaValue (schema.IsIdentity);
  892. row [NUMERIC_PRECISION_IDX] = GetSchemaValue (schema.NumericPrecision);
  893. row [IS_KEY_IDX] = GetSchemaValue (schema.IsKey);
  894. row [IS_ALIASED_IDX] = GetSchemaValue (schema.IsAliased);
  895. row [IS_EXPRESSION_IDX] = GetSchemaValue (schema.IsExpression);
  896. row [IS_READ_ONLY_IDX] = GetSchemaValue (schema.IsReadOnly);
  897. row [BASE_SERVER_NAME_IDX] = GetSchemaValue (schema.BaseServerName);
  898. row [BASE_CATALOG_NAME_IDX] = GetSchemaValue (schema.BaseCatalogName);
  899. row [BASE_COLUMN_NAME_IDX] = GetSchemaValue (schema.BaseColumnName);
  900. row [BASE_SCHEMA_NAME_IDX] = GetSchemaValue (schema.BaseSchemaName);
  901. row [BASE_TABLE_NAME_IDX] = GetSchemaValue (schema.BaseTableName);
  902. row [ALLOW_DBNULL_IDX] = GetSchemaValue (schema.AllowDBNull);
  903. #else
  904. row [COLUMN_NAME_IDX] = GetSchemaValue (schema, "ColumnName");
  905. row [COLUMN_ORDINAL_IDX] = GetSchemaValue (schema, "ColumnOrdinal");
  906. row [IS_UNIQUE_IDX] = GetSchemaValue (schema, "IsUnique");
  907. row [IS_AUTO_INCREMENT_IDX] = GetSchemaValue (schema, "IsAutoIncrement");
  908. row [IS_ROW_VERSION_IDX] = GetSchemaValue (schema, "IsRowVersion");
  909. row [IS_HIDDEN_IDX] = GetSchemaValue (schema, "IsHidden");
  910. row [IS_IDENTITY_IDX] = GetSchemaValue (schema, "IsIdentity");
  911. row [IS_KEY_IDX] = GetSchemaValue (schema, "IsKey");
  912. row [IS_ALIASED_IDX] = GetSchemaValue (schema, "IsAliased");
  913. row [IS_EXPRESSION_IDX] = GetSchemaValue (schema, "IsExpression");
  914. row [IS_READ_ONLY_IDX] = GetSchemaValue (schema, "IsReadOnly");
  915. row [BASE_SERVER_NAME_IDX] = GetSchemaValue (schema, "BaseServerName");
  916. row [BASE_CATALOG_NAME_IDX] = GetSchemaValue (schema, "BaseCatalogName");
  917. row [BASE_COLUMN_NAME_IDX] = GetSchemaValue (schema, "BaseColumnName");
  918. row [BASE_SCHEMA_NAME_IDX] = GetSchemaValue (schema, "BaseSchemaName");
  919. row [BASE_TABLE_NAME_IDX] = GetSchemaValue (schema, "BaseTableName");
  920. row [ALLOW_DBNULL_IDX] = GetSchemaValue (schema, "AllowDBNull");
  921. #endif
  922. // We don't always get the base column name.
  923. if (row [BASE_COLUMN_NAME_IDX] == DBNull.Value)
  924. row [BASE_COLUMN_NAME_IDX] = row [COLUMN_NAME_IDX];
  925. TdsColumnType ctype;
  926. int csize, dbType;
  927. Type fieldType;
  928. bool isLong;
  929. string typeName;
  930. short precision;
  931. short scale;
  932. #if NET_2_0
  933. ctype = (TdsColumnType) schema.ColumnType;
  934. csize = (int) schema.ColumnSize;
  935. precision = (short) GetSchemaValue (schema.NumericPrecision);
  936. scale = (short) GetSchemaValue (schema.NumericScale);
  937. #else
  938. ctype = (TdsColumnType) schema ["ColumnType"];
  939. csize = (int) schema ["ColumnSize"];
  940. precision = (short) ((byte) GetSchemaValue (schema, "NumericPrecision"));
  941. scale = (short) ((byte) GetSchemaValue (schema, "NumericScale"));
  942. #endif
  943. GetSchemaRowType (ctype, csize, precision, scale,
  944. out dbType, out fieldType, out isLong,
  945. out typeName);
  946. row [COLUMN_SIZE_IDX] = csize;
  947. row [NUMERIC_PRECISION_IDX] = precision;
  948. row [NUMERIC_SCALE_IDX] = scale;
  949. row [PROVIDER_TYPE_IDX] = dbType;
  950. row [DATA_TYPE_IDX] = fieldType;
  951. row [IS_LONG_IDX] = isLong;
  952. #if NET_2_0
  953. if ((bool)row [IS_HIDDEN_IDX] == false)
  954. visibleFieldCount += 1;
  955. #endif
  956. schemaTable.Rows.Add (row);
  957. }
  958. return schemaTable;
  959. }
  960. private static object GetSchemaValue (TdsDataColumn schema, string key)
  961. {
  962. object val = schema [key];
  963. if (val != null)
  964. return val;
  965. else
  966. return DBNull.Value;
  967. }
  968. #if NET_2_0
  969. static object GetSchemaValue (object value)
  970. {
  971. if (value == null)
  972. return DBNull.Value;
  973. return value;
  974. }
  975. #endif
  976. public
  977. #if NET_2_0
  978. virtual
  979. #endif
  980. SqlBinary GetSqlBinary (int i)
  981. {
  982. object value = GetSqlValue (i);
  983. if (!(value is SqlBinary))
  984. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  985. return (SqlBinary) value;
  986. }
  987. public
  988. #if NET_2_0
  989. virtual
  990. #endif
  991. SqlBoolean GetSqlBoolean (int i)
  992. {
  993. object value = GetSqlValue (i);
  994. if (!(value is SqlBoolean))
  995. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  996. return (SqlBoolean) value;
  997. }
  998. public
  999. #if NET_2_0
  1000. virtual
  1001. #endif
  1002. SqlByte GetSqlByte (int i)
  1003. {
  1004. object value = GetSqlValue (i);
  1005. if (!(value is SqlByte))
  1006. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1007. return (SqlByte) value;
  1008. }
  1009. public
  1010. #if NET_2_0
  1011. virtual
  1012. #endif
  1013. SqlDateTime GetSqlDateTime (int i)
  1014. {
  1015. object value = GetSqlValue (i);
  1016. if (!(value is SqlDateTime))
  1017. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1018. return (SqlDateTime) value;
  1019. }
  1020. public
  1021. #if NET_2_0
  1022. virtual
  1023. #endif
  1024. SqlDecimal GetSqlDecimal (int i)
  1025. {
  1026. object value = GetSqlValue (i);
  1027. if (!(value is SqlDecimal))
  1028. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1029. return (SqlDecimal) value;
  1030. }
  1031. public
  1032. #if NET_2_0
  1033. virtual
  1034. #endif
  1035. SqlDouble GetSqlDouble (int i)
  1036. {
  1037. object value = GetSqlValue (i);
  1038. if (!(value is SqlDouble))
  1039. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1040. return (SqlDouble) value;
  1041. }
  1042. public
  1043. #if NET_2_0
  1044. virtual
  1045. #endif
  1046. SqlGuid GetSqlGuid (int i)
  1047. {
  1048. object value = GetSqlValue (i);
  1049. if (!(value is SqlGuid))
  1050. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1051. return (SqlGuid) value;
  1052. }
  1053. public
  1054. #if NET_2_0
  1055. virtual
  1056. #endif
  1057. SqlInt16 GetSqlInt16 (int i)
  1058. {
  1059. object value = GetSqlValue (i);
  1060. if (!(value is SqlInt16))
  1061. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1062. return (SqlInt16) value;
  1063. }
  1064. public
  1065. #if NET_2_0
  1066. virtual
  1067. #endif
  1068. SqlInt32 GetSqlInt32 (int i)
  1069. {
  1070. object value = GetSqlValue (i);
  1071. if (!(value is SqlInt32))
  1072. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1073. return (SqlInt32) value;
  1074. }
  1075. public
  1076. #if NET_2_0
  1077. virtual
  1078. #endif
  1079. SqlInt64 GetSqlInt64 (int i)
  1080. {
  1081. object value = GetSqlValue (i);
  1082. if (!(value is SqlInt64))
  1083. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1084. return (SqlInt64) value;
  1085. }
  1086. public
  1087. #if NET_2_0
  1088. virtual
  1089. #endif
  1090. SqlMoney GetSqlMoney (int i)
  1091. {
  1092. object value = GetSqlValue (i);
  1093. if (!(value is SqlMoney))
  1094. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1095. return (SqlMoney) value;
  1096. }
  1097. public
  1098. #if NET_2_0
  1099. virtual
  1100. #endif
  1101. SqlSingle GetSqlSingle (int i)
  1102. {
  1103. object value = GetSqlValue (i);
  1104. if (!(value is SqlSingle))
  1105. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1106. return (SqlSingle) value;
  1107. }
  1108. public
  1109. #if NET_2_0
  1110. virtual
  1111. #endif
  1112. SqlString GetSqlString (int i)
  1113. {
  1114. object value = GetSqlValue (i);
  1115. if (!(value is SqlString))
  1116. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1117. return (SqlString) value;
  1118. }
  1119. #if NET_2_0
  1120. public virtual SqlXml GetSqlXml (int i)
  1121. {
  1122. object value = GetSqlValue (i);
  1123. if (!(value is SqlXml)) {
  1124. if (value is DBNull) {
  1125. throw new SqlNullValueException ();
  1126. } else if (command.Tds.TdsVersion == TdsVersion.tds70 && value is SqlString) {
  1127. // Workaround for TDS 7 clients
  1128. // Xml column types are supported only from Sql Server 2005 / TDS 8, however
  1129. // when a TDS 7 client requests for Xml column data, Sql Server 2005 returns
  1130. // it as NTEXT
  1131. MemoryStream stream = null;
  1132. if (!((SqlString) value).IsNull)
  1133. stream = new MemoryStream (Encoding.Unicode.GetBytes (value.ToString()));
  1134. value = new SqlXml (stream);
  1135. } else {
  1136. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1137. }
  1138. }
  1139. return (SqlXml) value;
  1140. }
  1141. #endif // NET_2_0
  1142. public
  1143. #if NET_2_0
  1144. virtual
  1145. #endif
  1146. object GetSqlValue (int i)
  1147. {
  1148. int csize;
  1149. short precision, scale;
  1150. TdsColumnType ctype;
  1151. TdsDataColumn column;
  1152. object value = GetValue (i);
  1153. SqlDbType type = GetSchemaRowDbType (i);
  1154. switch (type) {
  1155. case SqlDbType.BigInt:
  1156. if (value == DBNull.Value)
  1157. return SqlInt64.Null;
  1158. return (SqlInt64) ((long) value);
  1159. case SqlDbType.Binary:
  1160. case SqlDbType.Image:
  1161. case SqlDbType.VarBinary:
  1162. case SqlDbType.Timestamp:
  1163. if (value == DBNull.Value)
  1164. return SqlBinary.Null;
  1165. return (SqlBinary) ((byte[]) value);
  1166. case SqlDbType.Bit:
  1167. if (value == DBNull.Value)
  1168. return SqlBoolean.Null;
  1169. return (SqlBoolean) ((bool) value);
  1170. case SqlDbType.Char:
  1171. case SqlDbType.NChar:
  1172. case SqlDbType.NText:
  1173. case SqlDbType.NVarChar:
  1174. case SqlDbType.Text:
  1175. case SqlDbType.VarChar:
  1176. if (value == DBNull.Value)
  1177. return SqlString.Null;
  1178. return (SqlString) ((string) value);
  1179. case SqlDbType.DateTime:
  1180. case SqlDbType.SmallDateTime:
  1181. if (value == DBNull.Value)
  1182. return SqlDateTime.Null;
  1183. return (SqlDateTime) ((DateTime) value);
  1184. case SqlDbType.Decimal:
  1185. if (value == DBNull.Value)
  1186. return SqlDecimal.Null;
  1187. if (value is TdsBigDecimal)
  1188. return SqlDecimal.FromTdsBigDecimal ((TdsBigDecimal) value);
  1189. return (SqlDecimal) ((decimal) value);
  1190. case SqlDbType.Float:
  1191. if (value == DBNull.Value)
  1192. return SqlDouble.Null;
  1193. return (SqlDouble) ((double) value);
  1194. case SqlDbType.Int:
  1195. if (value == DBNull.Value)
  1196. return SqlInt32.Null;
  1197. return (SqlInt32) ((int) value);
  1198. case SqlDbType.Money:
  1199. case SqlDbType.SmallMoney:
  1200. if (value == DBNull.Value)
  1201. return SqlMoney.Null;
  1202. return (SqlMoney) ((decimal) value);
  1203. case SqlDbType.Real:
  1204. if (value == DBNull.Value)
  1205. return SqlSingle.Null;
  1206. return (SqlSingle) ((float) value);
  1207. case SqlDbType.UniqueIdentifier:
  1208. if (value == DBNull.Value)
  1209. return SqlGuid.Null;
  1210. return (SqlGuid) ((Guid) value);
  1211. case SqlDbType.SmallInt:
  1212. if (value == DBNull.Value)
  1213. return SqlInt16.Null;
  1214. return (SqlInt16) ((short) value);
  1215. case SqlDbType.TinyInt:
  1216. if (value == DBNull.Value)
  1217. return SqlByte.Null;
  1218. return (SqlByte) ((byte) value);
  1219. #if NET_2_0
  1220. case SqlDbType.Xml:
  1221. if (value == DBNull.Value)
  1222. return SqlByte.Null;
  1223. return (SqlXml) value;
  1224. #endif
  1225. }
  1226. throw new InvalidOperationException ("The type of this column is unknown.");
  1227. }
  1228. public
  1229. #if NET_2_0
  1230. virtual
  1231. #endif
  1232. int GetSqlValues (object[] values)
  1233. {
  1234. ValidateState ();
  1235. EnsureDataAvailable ();
  1236. if (values == null)
  1237. throw new ArgumentNullException ("values");
  1238. int count = 0;
  1239. int columnCount = command.Tds.Columns.Count;
  1240. int arrayCount = values.Length;
  1241. if (arrayCount > columnCount)
  1242. count = columnCount;
  1243. else
  1244. count = arrayCount;
  1245. for (int i = 0; i < count; i += 1)
  1246. values [i] = GetSqlValue (i);
  1247. return count;
  1248. }
  1249. public
  1250. #if NET_2_0
  1251. override
  1252. #endif // NET_2_0
  1253. string GetString (int i)
  1254. {
  1255. object value = GetValue (i);
  1256. if (!(value is string)) {
  1257. if (value is DBNull) throw new SqlNullValueException ();
  1258. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1259. }
  1260. return (string) value;
  1261. }
  1262. public
  1263. #if NET_2_0
  1264. override
  1265. #endif // NET_2_0
  1266. object GetValue (int i)
  1267. {
  1268. ValidateState ();
  1269. EnsureDataAvailable ();
  1270. if (i < 0 || i >= command.Tds.Columns.Count)
  1271. throw new IndexOutOfRangeException ();
  1272. try {
  1273. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  1274. return ((Tds)command.Tds).GetSequentialColumnValue (i);
  1275. }
  1276. } catch (TdsInternalException ex) {
  1277. command.Connection.Close ();
  1278. throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
  1279. }
  1280. return command.Tds.ColumnValues [i];
  1281. }
  1282. public
  1283. #if NET_2_0
  1284. override
  1285. #endif // NET_2_0
  1286. int GetValues (object[] values)
  1287. {
  1288. ValidateState ();
  1289. EnsureDataAvailable ();
  1290. if (values == null)
  1291. throw new ArgumentNullException ("values");
  1292. int len = values.Length;
  1293. int bigDecimalIndex = command.Tds.ColumnValues.BigDecimalIndex;
  1294. // If a four-byte decimal is stored, then we can't convert to
  1295. // a native type. Throw an OverflowException.
  1296. if (bigDecimalIndex >= 0 && bigDecimalIndex < len)
  1297. throw new OverflowException ();
  1298. try {
  1299. command.Tds.ColumnValues.CopyTo (0, values, 0,
  1300. len > command.Tds.ColumnValues.Count ? command.Tds.ColumnValues.Count : len);
  1301. } catch (TdsInternalException ex) {
  1302. command.Connection.Close ();
  1303. throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
  1304. }
  1305. return (len < FieldCount ? len : FieldCount);
  1306. }
  1307. #if !NET_2_0
  1308. void IDisposable.Dispose ()
  1309. {
  1310. Dispose (true);
  1311. GC.SuppressFinalize (this);
  1312. }
  1313. #endif
  1314. #if NET_2_0
  1315. public override IEnumerator GetEnumerator ()
  1316. #else
  1317. IEnumerator IEnumerable.GetEnumerator ()
  1318. #endif
  1319. {
  1320. return new DbEnumerator (this);
  1321. }
  1322. public
  1323. #if NET_2_0
  1324. override
  1325. #endif // NET_2_0
  1326. bool IsDBNull (int i)
  1327. {
  1328. return GetValue (i) == DBNull.Value;
  1329. }
  1330. public
  1331. #if NET_2_0
  1332. override
  1333. #endif // NET_2_0
  1334. bool NextResult ()
  1335. {
  1336. ValidateState ();
  1337. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0) {
  1338. moreResults = false;
  1339. rowsRead = 0;
  1340. haveRead = false;
  1341. return false;
  1342. }
  1343. try {
  1344. moreResults = command.Tds.NextResult ();
  1345. } catch (TdsInternalException ex) {
  1346. command.Connection.Close ();
  1347. throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
  1348. }
  1349. if (!moreResults)
  1350. command.GetOutputParameters ();
  1351. else {
  1352. // new schema - don't do anything except reset schemaTable as command.Tds.Columns is already updated
  1353. schemaTable = null;
  1354. }
  1355. rowsRead = 0;
  1356. haveRead = false;
  1357. resultsRead += 1;
  1358. return moreResults;
  1359. }
  1360. public
  1361. #if NET_2_0
  1362. override
  1363. #endif // NET_2_0
  1364. bool Read ()
  1365. {
  1366. ValidateState ();
  1367. if (!haveRead || readResultUsed)
  1368. readResult = ReadRecord ();
  1369. readResultUsed = true;
  1370. return readResult;
  1371. }
  1372. internal bool ReadRecord ()
  1373. {
  1374. readResultUsed = false;
  1375. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && haveRead)
  1376. return false;
  1377. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  1378. return false;
  1379. if (!moreResults)
  1380. return false;
  1381. try {
  1382. bool result = command.Tds.NextRow ();
  1383. if (result)
  1384. rowsRead++;
  1385. haveRead = true;
  1386. return result;
  1387. } catch (TdsInternalException ex) {
  1388. command.Connection.Close ();
  1389. throw SqlException.FromTdsInternalException ((TdsInternalException) ex);
  1390. }
  1391. }
  1392. void ValidateState ()
  1393. {
  1394. if (IsClosed)
  1395. throw new InvalidOperationException ("Invalid attempt to read data when reader is closed");
  1396. }
  1397. void EnsureDataAvailable ()
  1398. {
  1399. if (!readResult || !haveRead || !readResultUsed)
  1400. throw new InvalidOperationException ("No data available.");
  1401. }
  1402. InvalidCastException CreateGetBytesOnInvalidColumnTypeException (int ordinal)
  1403. {
  1404. string message = string.Format (CultureInfo.InvariantCulture,
  1405. "Invalid attempt to GetBytes on column '{0}'." +
  1406. "The GetBytes function can only be used on " +
  1407. "columns of type Text, NText, or Image.",
  1408. GetName (ordinal));
  1409. return new InvalidCastException (message);
  1410. }
  1411. #if NET_2_0
  1412. public override Type GetProviderSpecificFieldType (int i)
  1413. {
  1414. return (GetSqlValue (i).GetType());
  1415. }
  1416. public override object GetProviderSpecificValue (int i)
  1417. {
  1418. return (GetSqlValue (i));
  1419. }
  1420. public override int GetProviderSpecificValues (object [] values)
  1421. {
  1422. return (GetSqlValues (values));
  1423. }
  1424. public virtual SqlBytes GetSqlBytes (int i)
  1425. {
  1426. //object value = GetSqlValue (i);
  1427. //if (!(value is SqlBinary))
  1428. // throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1429. Byte[] val = (byte[])GetValue(i);
  1430. SqlBytes sb = new SqlBytes (val);
  1431. return (sb);
  1432. }
  1433. #endif // NET_2_0
  1434. #endregion // Methods
  1435. }
  1436. }