SqlDataReader.cs 21 KB

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