SqlDataReader.cs 29 KB

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