SqlDataReader.cs 29 KB

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