SqlDataReader.cs 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  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.Text;
  38. using System.Collections;
  39. using System.ComponentModel;
  40. using System.Data;
  41. using System.Data.Common;
  42. using System.Data.SqlTypes;
  43. namespace System.Data.SqlClient {
  44. #if NET_2_0
  45. public class SqlDataReader : DbDataReader, IDataReader, IDisposable, IDataRecord
  46. #else
  47. public sealed class SqlDataReader : MarshalByRefObject, IEnumerable, IDataReader, IDisposable, IDataRecord
  48. #endif // NET_2_0
  49. {
  50. #region Fields
  51. SqlCommand command;
  52. ArrayList dataTypeNames;
  53. bool disposed = false;
  54. int fieldCount;
  55. bool isClosed;
  56. bool moreResults;
  57. int resultsRead;
  58. int rowsRead;
  59. DataTable schemaTable;
  60. bool hasRows;
  61. bool haveRead;
  62. bool readResult;
  63. bool readResultUsed;
  64. #if NET_2_0
  65. int visibleFieldCount;
  66. #endif
  67. #endregion // Fields
  68. #region Constructors
  69. internal SqlDataReader (SqlCommand command)
  70. {
  71. readResult = false;
  72. haveRead = false;
  73. readResultUsed = false;
  74. this.command = command;
  75. schemaTable = ConstructSchemaTable ();
  76. resultsRead = 0;
  77. fieldCount = 0;
  78. isClosed = false;
  79. command.Tds.RecordsAffected = -1;
  80. #if NET_2_0
  81. visibleFieldCount = 0;
  82. #endif
  83. NextResult ();
  84. }
  85. #endregion // Constructors
  86. #region Properties
  87. public
  88. #if NET_2_0
  89. override
  90. #endif // NET_2_0
  91. int Depth {
  92. get { return 0; }
  93. }
  94. public
  95. #if NET_2_0
  96. override
  97. #endif // NET_2_0
  98. int FieldCount {
  99. get { return fieldCount; }
  100. }
  101. public
  102. #if NET_2_0
  103. override
  104. #endif // NET_2_0
  105. bool IsClosed {
  106. get { return isClosed; }
  107. }
  108. public
  109. #if NET_2_0
  110. override
  111. #endif // NET_2_0
  112. object this [int i] {
  113. get { return GetValue (i); }
  114. }
  115. public
  116. #if NET_2_0
  117. override
  118. #endif // NET_2_0
  119. object this [string name] {
  120. get { return GetValue (GetOrdinal (name)); }
  121. }
  122. public
  123. #if NET_2_0
  124. override
  125. #endif // NET_2_0
  126. int RecordsAffected {
  127. get {
  128. return command.Tds.RecordsAffected;
  129. }
  130. }
  131. public
  132. #if NET_2_0
  133. override
  134. #endif // NET_2_0
  135. bool HasRows {
  136. get {
  137. if (haveRead)
  138. return readResult;
  139. haveRead = true;
  140. readResult = ReadRecord ();
  141. return readResult;
  142. }
  143. }
  144. #if NET_2_0
  145. public override int VisibleFieldCount {
  146. get { return visibleFieldCount; }
  147. }
  148. #endif
  149. #endregion // Properties
  150. #region Methods
  151. public
  152. #if NET_2_0
  153. override
  154. #endif // NET_2_0
  155. void Close ()
  156. {
  157. if (IsClosed)
  158. return;
  159. // skip to end & read output parameters.
  160. while (NextResult ())
  161. ;
  162. isClosed = true;
  163. command.Connection.DataReader = null;
  164. command.CloseDataReader (moreResults);
  165. }
  166. private static DataTable ConstructSchemaTable ()
  167. {
  168. Type booleanType = Type.GetType ("System.Boolean");
  169. Type stringType = Type.GetType ("System.String");
  170. Type intType = Type.GetType ("System.Int32");
  171. Type typeType = Type.GetType ("System.Type");
  172. Type shortType = Type.GetType ("System.Int16");
  173. DataTable schemaTable = new DataTable ("SchemaTable");
  174. schemaTable.Columns.Add ("ColumnName", stringType);
  175. schemaTable.Columns.Add ("ColumnOrdinal", intType);
  176. schemaTable.Columns.Add ("ColumnSize", intType);
  177. schemaTable.Columns.Add ("NumericPrecision", shortType);
  178. schemaTable.Columns.Add ("NumericScale", shortType);
  179. schemaTable.Columns.Add ("IsUnique", booleanType);
  180. schemaTable.Columns.Add ("IsKey", booleanType);
  181. schemaTable.Columns.Add ("BaseServerName", stringType);
  182. schemaTable.Columns.Add ("BaseCatalogName", stringType);
  183. schemaTable.Columns.Add ("BaseColumnName", stringType);
  184. schemaTable.Columns.Add ("BaseSchemaName", stringType);
  185. schemaTable.Columns.Add ("BaseTableName", stringType);
  186. schemaTable.Columns.Add ("DataType", typeType);
  187. schemaTable.Columns.Add ("AllowDBNull", booleanType);
  188. schemaTable.Columns.Add ("ProviderType", intType);
  189. schemaTable.Columns.Add ("IsAliased", booleanType);
  190. schemaTable.Columns.Add ("IsExpression", booleanType);
  191. schemaTable.Columns.Add ("IsIdentity", booleanType);
  192. schemaTable.Columns.Add ("IsAutoIncrement", booleanType);
  193. schemaTable.Columns.Add ("IsRowVersion", booleanType);
  194. schemaTable.Columns.Add ("IsHidden", booleanType);
  195. schemaTable.Columns.Add ("IsLong", booleanType);
  196. schemaTable.Columns.Add ("IsReadOnly", booleanType);
  197. return schemaTable;
  198. }
  199. #if NET_2_0
  200. protected override
  201. #endif
  202. void Dispose (bool disposing)
  203. {
  204. if (!disposed) {
  205. if (disposing) {
  206. schemaTable.Dispose ();
  207. Close ();
  208. command = null;
  209. }
  210. disposed = true;
  211. }
  212. }
  213. public
  214. #if NET_2_0
  215. override
  216. #endif // NET_2_0
  217. bool GetBoolean (int i)
  218. {
  219. object value = GetValue (i);
  220. if (!(value is bool)) {
  221. if (value is DBNull) throw new SqlNullValueException ();
  222. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  223. }
  224. return (bool) value;
  225. }
  226. public
  227. #if NET_2_0
  228. override
  229. #endif // NET_2_0
  230. byte GetByte (int i)
  231. {
  232. object value = GetValue (i);
  233. if (!(value is byte)) {
  234. if (value is DBNull) throw new SqlNullValueException ();
  235. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  236. }
  237. return (byte) value;
  238. }
  239. public
  240. #if NET_2_0
  241. override
  242. #endif // NET_2_0
  243. long GetBytes (int i, long dataIndex, byte[] buffer, int bufferIndex, int length)
  244. {
  245. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  246. long len = ((Tds)command.Tds).GetSequentialColumnValue (i, dataIndex, buffer, bufferIndex, length);
  247. if (len == -1)
  248. throw new InvalidCastException ("Invalid attempt to GetBytes on column "
  249. + "'" + command.Tds.Columns[i]["ColumnName"] + "'." + "The GetBytes function"
  250. + " can only be used on columns of type Text, NText, or Image");
  251. return len;
  252. }
  253. object value = GetValue (i);
  254. if (!(value is byte [])) {
  255. if (value is DBNull) throw new SqlNullValueException ();
  256. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  257. }
  258. if ( buffer == null )
  259. return ((byte []) value).Length; // Return length of data
  260. // Copy data into buffer
  261. int availLen = (int) ( ( (byte []) value).Length - dataIndex);
  262. if (availLen < length)
  263. length = availLen;
  264. Array.Copy ((byte []) value, (int) dataIndex, buffer, bufferIndex, length);
  265. return length; // return actual read count
  266. }
  267. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  268. public
  269. #if NET_2_0
  270. override
  271. #endif // NET_2_0
  272. char GetChar (int i)
  273. {
  274. object value = GetValue (i);
  275. if (!(value is char)) {
  276. if (value is DBNull) throw new SqlNullValueException ();
  277. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  278. }
  279. return (char) value;
  280. }
  281. public
  282. #if NET_2_0
  283. override
  284. #endif // NET_2_0
  285. long GetChars (int i, long dataIndex, char[] buffer, int bufferIndex, int length)
  286. {
  287. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  288. Encoding encoding = null;
  289. byte mul = 1;
  290. TdsColumnType colType = (TdsColumnType) command.Tds.Columns[i]["ColumnType"];
  291. switch (colType) {
  292. case TdsColumnType.Text :
  293. case TdsColumnType.VarChar:
  294. case TdsColumnType.Char:
  295. case TdsColumnType.BigVarChar:
  296. encoding = Encoding.ASCII;
  297. break;
  298. case TdsColumnType.NText :
  299. case TdsColumnType.NVarChar:
  300. case TdsColumnType.NChar:
  301. encoding = Encoding.Unicode;
  302. mul = 2 ;
  303. break;
  304. default :
  305. return -1;
  306. }
  307. long count = 0;
  308. if (buffer == null) {
  309. count = GetBytes (i,0,(byte[]) null,0,0);
  310. return (count/mul);
  311. }
  312. length *= mul;
  313. byte[] arr = new byte [length];
  314. count = GetBytes (i, dataIndex, arr, 0, length);
  315. if (count == -1)
  316. throw new InvalidCastException ("Specified cast is not valid");
  317. Char[] val = encoding.GetChars (arr, 0, (int)count);
  318. val.CopyTo (buffer, bufferIndex);
  319. return val.Length;
  320. }
  321. char [] valueBuffer;
  322. object value = GetValue (i);
  323. if (value is char[])
  324. valueBuffer = (char[])value;
  325. else if (value is string)
  326. valueBuffer = ((string)value).ToCharArray();
  327. else {
  328. if (value is DBNull) throw new SqlNullValueException ();
  329. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  330. }
  331. if ( buffer == null ) {
  332. // Return length of data
  333. return valueBuffer.Length;
  334. }
  335. else {
  336. // Copy data into buffer
  337. Array.Copy (valueBuffer, (int) dataIndex, buffer, bufferIndex, length);
  338. return valueBuffer.Length - dataIndex;
  339. }
  340. }
  341. #if !NET_2_0
  342. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  343. public new IDataReader GetData (int i)
  344. {
  345. return ((IDataReader) this [i]);
  346. }
  347. #endif
  348. public
  349. #if NET_2_0
  350. override
  351. #endif // NET_2_0
  352. string GetDataTypeName (int i)
  353. {
  354. if (i < 0 || i >= dataTypeNames.Count)
  355. throw new IndexOutOfRangeException ();
  356. return (string) dataTypeNames [i];
  357. }
  358. public
  359. #if NET_2_0
  360. override
  361. #endif // NET_2_0
  362. DateTime GetDateTime (int i)
  363. {
  364. object value = GetValue (i);
  365. if (!(value is DateTime)) {
  366. if (value is DBNull) throw new SqlNullValueException ();
  367. else throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  368. }
  369. return (DateTime) value;
  370. }
  371. public
  372. #if NET_2_0
  373. override
  374. #endif // NET_2_0
  375. decimal GetDecimal (int i)
  376. {
  377. object value = GetValue (i);
  378. if (!(value is decimal)) {
  379. if (value is DBNull) throw new SqlNullValueException ();
  380. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  381. }
  382. return (decimal) value;
  383. }
  384. public
  385. #if NET_2_0
  386. override
  387. #endif // NET_2_0
  388. double GetDouble (int i)
  389. {
  390. object value = GetValue (i);
  391. if (!(value is double)) {
  392. if (value is DBNull) throw new SqlNullValueException ();
  393. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  394. }
  395. return (double) value;
  396. }
  397. public
  398. #if NET_2_0
  399. override
  400. #endif // NET_2_0
  401. Type GetFieldType (int i)
  402. {
  403. if (i < 0 || i >= schemaTable.Rows.Count)
  404. throw new IndexOutOfRangeException ();
  405. return (Type) schemaTable.Rows[i]["DataType"];
  406. }
  407. public
  408. #if NET_2_0
  409. override
  410. #endif // NET_2_0
  411. float GetFloat (int i)
  412. {
  413. object value = GetValue (i);
  414. if (!(value is float)) {
  415. if (value is DBNull) throw new SqlNullValueException ();
  416. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  417. }
  418. return (float) value;
  419. }
  420. public
  421. #if NET_2_0
  422. override
  423. #endif // NET_2_0
  424. Guid GetGuid (int i)
  425. {
  426. object value = GetValue (i);
  427. if (!(value is Guid)) {
  428. if (value is DBNull) throw new SqlNullValueException ();
  429. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  430. }
  431. return (Guid) value;
  432. }
  433. public
  434. #if NET_2_0
  435. override
  436. #endif // NET_2_0
  437. short GetInt16 (int i)
  438. {
  439. object value = GetValue (i);
  440. if (!(value is short)) {
  441. if (value is DBNull) throw new SqlNullValueException ();
  442. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  443. }
  444. return (short) value;
  445. }
  446. public
  447. #if NET_2_0
  448. override
  449. #endif // NET_2_0
  450. int GetInt32 (int i)
  451. {
  452. object value = GetValue (i);
  453. if (!(value is int)) {
  454. if (value is DBNull) throw new SqlNullValueException ();
  455. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  456. }
  457. return (int) value;
  458. }
  459. public
  460. #if NET_2_0
  461. override
  462. #endif // NET_2_0
  463. long GetInt64 (int i)
  464. {
  465. object value = GetValue (i);
  466. // TDS 7.0 returns bigint as decimal(19,0)
  467. if (value is decimal) {
  468. TdsDataColumn schema = command.Tds.Columns[i];
  469. if ((byte)schema["NumericPrecision"] == 19 && (byte)schema["NumericScale"] == 0)
  470. value = (long) (decimal) value;
  471. }
  472. if (!(value is long)) {
  473. if (value is DBNull) throw new SqlNullValueException ();
  474. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  475. }
  476. return (long) value;
  477. }
  478. public
  479. #if NET_2_0
  480. override
  481. #endif // NET_2_0
  482. string GetName (int i)
  483. {
  484. return (string) schemaTable.Rows[i]["ColumnName"];
  485. }
  486. public
  487. #if NET_2_0
  488. override
  489. #endif // NET_2_0
  490. int GetOrdinal (string name)
  491. {
  492. foreach (DataRow schemaRow in schemaTable.Rows)
  493. if (((string) schemaRow ["ColumnName"]).Equals (name))
  494. return (int) schemaRow ["ColumnOrdinal"];
  495. foreach (DataRow schemaRow in schemaTable.Rows)
  496. if (String.Compare (((string) schemaRow ["ColumnName"]), name, true) == 0)
  497. return (int) schemaRow ["ColumnOrdinal"];
  498. throw new IndexOutOfRangeException ();
  499. }
  500. public
  501. #if NET_2_0
  502. override
  503. #endif // NET_2_0
  504. DataTable GetSchemaTable ()
  505. {
  506. ValidateState ();
  507. if (schemaTable.Rows != null && schemaTable.Rows.Count > 0)
  508. return schemaTable;
  509. if (!moreResults)
  510. return null;
  511. fieldCount = 0;
  512. dataTypeNames = new ArrayList ();
  513. foreach (TdsDataColumn schema in command.Tds.Columns) {
  514. DataRow row = schemaTable.NewRow ();
  515. row ["ColumnName"] = GetSchemaValue (schema, "ColumnName");
  516. row ["ColumnSize"] = GetSchemaValue (schema, "ColumnSize");
  517. row ["ColumnOrdinal"] = GetSchemaValue (schema, "ColumnOrdinal");
  518. row ["NumericPrecision"] = GetSchemaValue (schema, "NumericPrecision");
  519. row ["NumericScale"] = GetSchemaValue (schema, "NumericScale");
  520. row ["IsUnique"] = GetSchemaValue (schema, "IsUnique");
  521. row ["IsKey"] = GetSchemaValue (schema, "IsKey");
  522. row ["BaseServerName"] = GetSchemaValue (schema, "BaseServerName");
  523. row ["BaseCatalogName"] = GetSchemaValue (schema, "BaseCatalogName");
  524. row ["BaseColumnName"] = GetSchemaValue (schema, "BaseColumnName");
  525. row ["BaseSchemaName"] = GetSchemaValue (schema, "BaseSchemaName");
  526. row ["BaseTableName"] = GetSchemaValue (schema, "BaseTableName");
  527. row ["AllowDBNull"] = GetSchemaValue (schema, "AllowDBNull");
  528. row ["IsAliased"] = GetSchemaValue (schema, "IsAliased");
  529. row ["IsExpression"] = GetSchemaValue (schema, "IsExpression");
  530. row ["IsIdentity"] = GetSchemaValue (schema, "IsIdentity");
  531. row ["IsAutoIncrement"] = GetSchemaValue (schema, "IsAutoIncrement");
  532. row ["IsRowVersion"] = GetSchemaValue (schema, "IsRowVersion");
  533. row ["IsHidden"] = GetSchemaValue (schema, "IsHidden");
  534. row ["IsReadOnly"] = GetSchemaValue (schema, "IsReadOnly");
  535. // We don't always get the base column name.
  536. if (row ["BaseColumnName"] == DBNull.Value)
  537. row ["BaseColumnName"] = row ["ColumnName"];
  538. switch ((TdsColumnType) schema ["ColumnType"]) {
  539. case TdsColumnType.Int1:
  540. case TdsColumnType.Int2:
  541. case TdsColumnType.Int4:
  542. case TdsColumnType.IntN:
  543. switch ((int) schema ["ColumnSize"]) {
  544. case 1:
  545. dataTypeNames.Add ("tinyint");
  546. row ["ProviderType"] = (int) SqlDbType.TinyInt;
  547. row ["DataType"] = typeof (byte);
  548. row ["IsLong"] = false;
  549. break;
  550. case 2:
  551. dataTypeNames.Add ("smallint");
  552. row ["ProviderType"] = (int) SqlDbType.SmallInt;
  553. row ["DataType"] = typeof (short);
  554. row ["IsLong"] = false;
  555. break;
  556. case 4:
  557. dataTypeNames.Add ("int");
  558. row ["ProviderType"] = (int) SqlDbType.Int;
  559. row ["DataType"] = typeof (int);
  560. row ["IsLong"] = false;
  561. break;
  562. case 8:
  563. dataTypeNames.Add ("bigint");
  564. row ["ProviderType"] = (int) SqlDbType.BigInt;
  565. row ["DataType"] = typeof (long);
  566. row ["IsLong"] = false;
  567. break;
  568. }
  569. break;
  570. case TdsColumnType.Real:
  571. case TdsColumnType.Float8:
  572. case TdsColumnType.FloatN:
  573. switch ((int) schema ["ColumnSize"]) {
  574. case 4:
  575. dataTypeNames.Add ("real");
  576. row ["ProviderType"] = (int) SqlDbType.Real;
  577. row ["DataType"] = typeof (float);
  578. row ["IsLong"] = false;
  579. break;
  580. case 8:
  581. dataTypeNames.Add ("float");
  582. row ["ProviderType"] = (int) SqlDbType.Float;
  583. row ["DataType"] = typeof (double);
  584. row ["IsLong"] = false;
  585. break;
  586. }
  587. break;
  588. case TdsColumnType.Image :
  589. dataTypeNames.Add ("image");
  590. row ["ProviderType"] = (int) SqlDbType.Image;
  591. row ["DataType"] = typeof (byte[]);
  592. row ["IsLong"] = true;
  593. break;
  594. case TdsColumnType.Text :
  595. dataTypeNames.Add ("text");
  596. row ["ProviderType"] = (int) SqlDbType.Text;
  597. row ["DataType"] = typeof (string);
  598. row ["IsLong"] = true;
  599. break;
  600. case TdsColumnType.UniqueIdentifier :
  601. dataTypeNames.Add ("uniqueidentifier");
  602. row ["ProviderType"] = (int) SqlDbType.UniqueIdentifier;
  603. row ["DataType"] = typeof (Guid);
  604. row ["IsLong"] = false;
  605. break;
  606. case TdsColumnType.VarBinary :
  607. case TdsColumnType.BigVarBinary :
  608. dataTypeNames.Add ("varbinary");
  609. row ["ProviderType"] = (int) SqlDbType.VarBinary;
  610. row ["DataType"] = typeof (byte[]);
  611. row ["IsLong"] = true;
  612. break;
  613. case TdsColumnType.VarChar :
  614. case TdsColumnType.BigVarChar :
  615. dataTypeNames.Add ("varchar");
  616. row ["ProviderType"] = (int) SqlDbType.VarChar;
  617. row ["DataType"] = typeof (string);
  618. row ["IsLong"] = false;
  619. break;
  620. case TdsColumnType.Binary :
  621. case TdsColumnType.BigBinary :
  622. dataTypeNames.Add ("binary");
  623. row ["ProviderType"] = (int) SqlDbType.Binary;
  624. row ["DataType"] = typeof (byte[]);
  625. row ["IsLong"] = true;
  626. break;
  627. case TdsColumnType.Char :
  628. case TdsColumnType.BigChar :
  629. dataTypeNames.Add ("char");
  630. row ["ProviderType"] = (int) SqlDbType.Char;
  631. row ["DataType"] = typeof (string);
  632. row ["IsLong"] = false;
  633. break;
  634. case TdsColumnType.Bit :
  635. case TdsColumnType.BitN :
  636. dataTypeNames.Add ("bit");
  637. row ["ProviderType"] = (int) SqlDbType.Bit;
  638. row ["DataType"] = typeof (bool);
  639. row ["IsLong"] = false;
  640. break;
  641. case TdsColumnType.DateTime4 :
  642. case TdsColumnType.DateTime :
  643. case TdsColumnType.DateTimeN :
  644. dataTypeNames.Add ("datetime");
  645. row ["ProviderType"] = (int) SqlDbType.DateTime;
  646. row ["DataType"] = typeof (DateTime);
  647. row ["IsLong"] = false;
  648. break;
  649. case TdsColumnType.Money :
  650. case TdsColumnType.MoneyN :
  651. case TdsColumnType.Money4 :
  652. dataTypeNames.Add ("money");
  653. row ["ProviderType"] = (int) SqlDbType.Money;
  654. row ["DataType"] = typeof (decimal);
  655. row ["IsLong"] = false;
  656. break;
  657. case TdsColumnType.NText :
  658. dataTypeNames.Add ("ntext");
  659. row ["ProviderType"] = (int) SqlDbType.NText;
  660. row ["DataType"] = typeof (string);
  661. row ["IsLong"] = true;
  662. break;
  663. case TdsColumnType.NVarChar :
  664. dataTypeNames.Add ("nvarchar");
  665. row ["ProviderType"] = (int) SqlDbType.NVarChar;
  666. row ["DataType"] = typeof (string);
  667. row ["IsLong"] = false;
  668. break;
  669. case TdsColumnType.Decimal :
  670. case TdsColumnType.Numeric :
  671. dataTypeNames.Add ("decimal");
  672. row ["ProviderType"] = (int) SqlDbType.Decimal;
  673. row ["DataType"] = typeof (decimal);
  674. row ["IsLong"] = false;
  675. break;
  676. case TdsColumnType.NChar :
  677. dataTypeNames.Add ("nchar");
  678. row ["ProviderType"] = (int) SqlDbType.NChar;
  679. row ["DataType"] = typeof (string);
  680. row ["IsLong"] = false;
  681. break;
  682. case TdsColumnType.SmallMoney :
  683. dataTypeNames.Add ("smallmoney");
  684. row ["ProviderType"] = (int) SqlDbType.SmallMoney;
  685. row ["DataType"] = typeof (decimal);
  686. row ["IsLong"] = false;
  687. break;
  688. default :
  689. dataTypeNames.Add ("variant");
  690. row ["ProviderType"] = (int) SqlDbType.Variant;
  691. row ["DataType"] = typeof (object);
  692. row ["IsLong"] = false;
  693. break;
  694. }
  695. #if NET_2_0
  696. if ((bool)row ["IsHidden"] == false)
  697. visibleFieldCount += 1;
  698. #endif
  699. schemaTable.Rows.Add (row);
  700. fieldCount += 1;
  701. }
  702. return schemaTable;
  703. }
  704. private static object GetSchemaValue (TdsDataColumn schema, object key)
  705. {
  706. if (schema.ContainsKey (key) && schema [key] != null)
  707. return schema [key];
  708. return DBNull.Value;
  709. }
  710. public
  711. #if NET_2_0
  712. virtual
  713. #endif
  714. SqlBinary GetSqlBinary (int i)
  715. {
  716. object value = GetSqlValue (i);
  717. if (!(value is SqlBinary))
  718. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  719. return (SqlBinary) value;
  720. }
  721. public
  722. #if NET_2_0
  723. virtual
  724. #endif
  725. SqlBoolean GetSqlBoolean (int i)
  726. {
  727. object value = GetSqlValue (i);
  728. if (!(value is SqlBoolean))
  729. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  730. return (SqlBoolean) value;
  731. }
  732. public
  733. #if NET_2_0
  734. virtual
  735. #endif
  736. SqlByte GetSqlByte (int i)
  737. {
  738. object value = GetSqlValue (i);
  739. if (!(value is SqlByte))
  740. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  741. return (SqlByte) value;
  742. }
  743. public
  744. #if NET_2_0
  745. virtual
  746. #endif
  747. SqlDateTime GetSqlDateTime (int i)
  748. {
  749. object value = GetSqlValue (i);
  750. if (!(value is SqlDateTime))
  751. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  752. return (SqlDateTime) value;
  753. }
  754. public
  755. #if NET_2_0
  756. virtual
  757. #endif
  758. SqlDecimal GetSqlDecimal (int i)
  759. {
  760. object value = GetSqlValue (i);
  761. if (!(value is SqlDecimal))
  762. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  763. return (SqlDecimal) value;
  764. }
  765. public
  766. #if NET_2_0
  767. virtual
  768. #endif
  769. SqlDouble GetSqlDouble (int i)
  770. {
  771. object value = GetSqlValue (i);
  772. if (!(value is SqlDouble))
  773. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  774. return (SqlDouble) value;
  775. }
  776. public
  777. #if NET_2_0
  778. virtual
  779. #endif
  780. SqlGuid GetSqlGuid (int i)
  781. {
  782. object value = GetSqlValue (i);
  783. if (!(value is SqlGuid))
  784. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  785. return (SqlGuid) value;
  786. }
  787. public
  788. #if NET_2_0
  789. virtual
  790. #endif
  791. SqlInt16 GetSqlInt16 (int i)
  792. {
  793. object value = GetSqlValue (i);
  794. if (!(value is SqlInt16))
  795. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  796. return (SqlInt16) value;
  797. }
  798. public
  799. #if NET_2_0
  800. virtual
  801. #endif
  802. SqlInt32 GetSqlInt32 (int i)
  803. {
  804. object value = GetSqlValue (i);
  805. if (!(value is SqlInt32))
  806. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  807. return (SqlInt32) value;
  808. }
  809. public
  810. #if NET_2_0
  811. virtual
  812. #endif
  813. SqlInt64 GetSqlInt64 (int i)
  814. {
  815. object value = GetSqlValue (i);
  816. // TDS 7.0 returns bigint as decimal(19,0)
  817. if (value is SqlDecimal) {
  818. TdsDataColumn schema = command.Tds.Columns[i];
  819. if ((byte)schema["NumericPrecision"] == 19 && (byte)schema["NumericScale"] == 0)
  820. value = (SqlInt64) (SqlDecimal) value;
  821. }
  822. if (!(value is SqlInt64))
  823. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  824. return (SqlInt64) value;
  825. }
  826. public
  827. #if NET_2_0
  828. virtual
  829. #endif
  830. SqlMoney GetSqlMoney (int i)
  831. {
  832. object value = GetSqlValue (i);
  833. if (!(value is SqlMoney))
  834. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  835. return (SqlMoney) value;
  836. }
  837. public
  838. #if NET_2_0
  839. virtual
  840. #endif
  841. SqlSingle GetSqlSingle (int i)
  842. {
  843. object value = GetSqlValue (i);
  844. if (!(value is SqlSingle))
  845. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  846. return (SqlSingle) value;
  847. }
  848. public
  849. #if NET_2_0
  850. virtual
  851. #endif
  852. SqlString GetSqlString (int i)
  853. {
  854. object value = GetSqlValue (i);
  855. if (!(value is SqlString))
  856. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  857. return (SqlString) value;
  858. }
  859. public
  860. #if NET_2_0
  861. virtual
  862. #endif
  863. object GetSqlValue (int i)
  864. {
  865. SqlDbType type = (SqlDbType) (schemaTable.Rows [i]["ProviderType"]);
  866. object value = GetValue (i);
  867. switch (type) {
  868. case SqlDbType.BigInt:
  869. if (value == DBNull.Value)
  870. return SqlInt64.Null;
  871. return (SqlInt64) ((long) value);
  872. case SqlDbType.Binary:
  873. case SqlDbType.Image:
  874. case SqlDbType.VarBinary:
  875. case SqlDbType.Timestamp:
  876. if (value == DBNull.Value)
  877. return SqlBinary.Null;
  878. return (SqlBinary) ((byte[]) value);
  879. case SqlDbType.Bit:
  880. if (value == DBNull.Value)
  881. return SqlBoolean.Null;
  882. return (SqlBoolean) ((bool) value);
  883. case SqlDbType.Char:
  884. case SqlDbType.NChar:
  885. case SqlDbType.NText:
  886. case SqlDbType.NVarChar:
  887. case SqlDbType.Text:
  888. case SqlDbType.VarChar:
  889. if (value == DBNull.Value)
  890. return SqlString.Null;
  891. return (SqlString) ((string) value);
  892. case SqlDbType.DateTime:
  893. case SqlDbType.SmallDateTime:
  894. if (value == DBNull.Value)
  895. return SqlDateTime.Null;
  896. return (SqlDateTime) ((DateTime) value);
  897. case SqlDbType.Decimal:
  898. if (value == DBNull.Value)
  899. return SqlDecimal.Null;
  900. if (value is TdsBigDecimal)
  901. return SqlDecimal.FromTdsBigDecimal ((TdsBigDecimal) value);
  902. return (SqlDecimal) ((decimal) value);
  903. case SqlDbType.Float:
  904. if (value == DBNull.Value)
  905. return SqlDouble.Null;
  906. return (SqlDouble) ((double) value);
  907. case SqlDbType.Int:
  908. if (value == DBNull.Value)
  909. return SqlInt32.Null;
  910. return (SqlInt32) ((int) value);
  911. case SqlDbType.Money:
  912. case SqlDbType.SmallMoney:
  913. if (value == DBNull.Value)
  914. return SqlMoney.Null;
  915. return (SqlMoney) ((decimal) value);
  916. case SqlDbType.Real:
  917. if (value == DBNull.Value)
  918. return SqlSingle.Null;
  919. return (SqlSingle) ((float) value);
  920. case SqlDbType.UniqueIdentifier:
  921. if (value == DBNull.Value)
  922. return SqlGuid.Null;
  923. return (SqlGuid) ((Guid) value);
  924. case SqlDbType.SmallInt:
  925. if (value == DBNull.Value)
  926. return SqlInt16.Null;
  927. return (SqlInt16) ((short) value);
  928. case SqlDbType.TinyInt:
  929. if (value == DBNull.Value)
  930. return SqlByte.Null;
  931. return (SqlByte) ((byte) value);
  932. }
  933. throw new InvalidOperationException ("The type of this column is unknown.");
  934. }
  935. public
  936. #if NET_2_0
  937. virtual
  938. #endif
  939. int GetSqlValues (object[] values)
  940. {
  941. int count = 0;
  942. int columnCount = schemaTable.Rows.Count;
  943. int arrayCount = values.Length;
  944. if (arrayCount > columnCount)
  945. count = columnCount;
  946. else
  947. count = arrayCount;
  948. for (int i = 0; i < count; i += 1)
  949. values [i] = GetSqlValue (i);
  950. return count;
  951. }
  952. public
  953. #if NET_2_0
  954. override
  955. #endif // NET_2_0
  956. string GetString (int i)
  957. {
  958. object value = GetValue (i);
  959. if (!(value is string)) {
  960. if (value is DBNull) throw new SqlNullValueException ();
  961. throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  962. }
  963. return (string) value;
  964. }
  965. public
  966. #if NET_2_0
  967. override
  968. #endif // NET_2_0
  969. object GetValue (int i)
  970. {
  971. if (i < 0 || i >= command.Tds.Columns.Count)
  972. throw new IndexOutOfRangeException ();
  973. if ((command.CommandBehavior & CommandBehavior.SequentialAccess) != 0) {
  974. return ((Tds)command.Tds).GetSequentialColumnValue (i);
  975. }
  976. return command.Tds.ColumnValues [i];
  977. }
  978. public
  979. #if NET_2_0
  980. override
  981. #endif // NET_2_0
  982. int GetValues (object[] values)
  983. {
  984. int len = values.Length;
  985. int bigDecimalIndex = command.Tds.ColumnValues.BigDecimalIndex;
  986. // If a four-byte decimal is stored, then we can't convert to
  987. // a native type. Throw an OverflowException.
  988. if (bigDecimalIndex >= 0 && bigDecimalIndex < len)
  989. throw new OverflowException ();
  990. command.Tds.ColumnValues.CopyTo (0, values, 0,
  991. len > command.Tds.ColumnValues.Count ? command.Tds.ColumnValues.Count : len);
  992. return (len < FieldCount ? len : FieldCount);
  993. }
  994. void IDisposable.Dispose ()
  995. {
  996. Dispose (true);
  997. GC.SuppressFinalize (this);
  998. }
  999. public
  1000. #if NET_2_0
  1001. override
  1002. #endif
  1003. IEnumerator GetEnumerator ()
  1004. {
  1005. return new DbEnumerator (this);
  1006. }
  1007. public
  1008. #if NET_2_0
  1009. override
  1010. #endif // NET_2_0
  1011. bool IsDBNull (int i)
  1012. {
  1013. return GetValue (i) == DBNull.Value;
  1014. }
  1015. public
  1016. #if NET_2_0
  1017. override
  1018. #endif // NET_2_0
  1019. bool NextResult ()
  1020. {
  1021. ValidateState ();
  1022. if ((command.CommandBehavior & CommandBehavior.SingleResult) != 0 && resultsRead > 0)
  1023. return false;
  1024. moreResults = command.Tds.NextResult ();
  1025. if (!moreResults)
  1026. command.GetOutputParameters ();
  1027. else {
  1028. //new schema
  1029. schemaTable = ConstructSchemaTable ();
  1030. GetSchemaTable ();
  1031. }
  1032. rowsRead = 0;
  1033. resultsRead += 1;
  1034. return moreResults;
  1035. }
  1036. public
  1037. #if NET_2_0
  1038. override
  1039. #endif // NET_2_0
  1040. bool Read ()
  1041. {
  1042. ValidateState ();
  1043. if ((command.CommandBehavior & CommandBehavior.SingleRow) != 0 && rowsRead > 0)
  1044. return false;
  1045. if ((command.CommandBehavior & CommandBehavior.SchemaOnly) != 0)
  1046. return false;
  1047. if (!moreResults)
  1048. return false;
  1049. if ((haveRead) && (!readResultUsed))
  1050. {
  1051. readResultUsed = true;
  1052. return true;
  1053. }
  1054. return (ReadRecord ());
  1055. }
  1056. internal bool ReadRecord ()
  1057. {
  1058. bool result = command.Tds.NextRow ();
  1059. rowsRead += 1;
  1060. return result;
  1061. }
  1062. void ValidateState ()
  1063. {
  1064. if (IsClosed)
  1065. throw new InvalidOperationException ("Invalid attempt to read data when reader is closed");
  1066. }
  1067. #if NET_2_0
  1068. public override Type GetProviderSpecificFieldType (int position)
  1069. {
  1070. return (GetSqlValue (position).GetType());
  1071. }
  1072. public override object GetProviderSpecificValue (int position)
  1073. {
  1074. return (GetSqlValue (position));
  1075. }
  1076. public override int GetProviderSpecificValues (object [] values)
  1077. {
  1078. return (GetSqlValues (values));
  1079. }
  1080. public virtual SqlBytes GetSqlBytes (int i)
  1081. {
  1082. //object value = GetSqlValue (i);
  1083. //if (!(value is SqlBinary))
  1084. // throw new InvalidCastException ("Type is " + value.GetType ().ToString ());
  1085. Byte[] val = (byte[])GetValue(i);
  1086. SqlBytes sb = new SqlBytes (val);
  1087. return (sb);
  1088. }
  1089. #endif // NET_2_0
  1090. #endregion // Methods
  1091. }
  1092. }