SqlDataReader.cs 25 KB

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