SqlDataReader.cs 23 KB

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