SqlDataReader.cs 23 KB

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