SqlDataReader.cs 23 KB

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