SqlDataReader.cs 22 KB

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