SqlDataReader.cs 22 KB

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