OdbcDataReader.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. //
  2. // System.Data.Odbc.OdbcDataReader
  3. //
  4. // Author:
  5. // Brian Ritchie ([email protected])
  6. // Daniel Morgan <[email protected]>
  7. // Sureshkumar T <[email protected]> (2004)
  8. //
  9. // Copyright (C) Brian Ritchie, 2002
  10. // Copyright (C) Daniel Morgan, 2002
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.Data;
  37. using System.Data.Common;
  38. #if NET_2_0
  39. using System.Data.ProviderBase;
  40. #endif // NET_2_0
  41. using System.Text;
  42. namespace System.Data.Odbc
  43. {
  44. #if NET_2_0
  45. public sealed class OdbcDataReader : DbDataReaderBase
  46. #else
  47. public sealed class OdbcDataReader : MarshalByRefObject, IDataReader, IDisposable, IDataRecord, IEnumerable
  48. #endif
  49. {
  50. #region Fields
  51. private OdbcCommand command;
  52. private bool open;
  53. private int currentRow;
  54. private OdbcColumn[] cols;
  55. private IntPtr hstmt;
  56. #if ONLY_1_1
  57. private CommandBehavior behavior;
  58. #endif // ONLY_1_1
  59. #endregion
  60. #region Constructors
  61. internal OdbcDataReader (OdbcCommand command, CommandBehavior behavior)
  62. #if NET_2_0
  63. : base (behavior)
  64. #endif // NET_2_0
  65. {
  66. this.command = command;
  67. #if ONLY_1_1
  68. this.CommandBehavior=behavior;
  69. #endif // ONLY_1_1
  70. open = true;
  71. currentRow = -1;
  72. hstmt=command.hStmt;
  73. // Init columns array;
  74. short colcount=0;
  75. libodbc.SQLNumResultCols(hstmt, ref colcount);
  76. cols=new OdbcColumn[colcount];
  77. GetSchemaTable ();
  78. }
  79. #endregion
  80. #region Properties
  81. #if ONLY_1_1
  82. private CommandBehavior CommandBehavior
  83. {
  84. get { return behavior; }
  85. set { value = behavior; }
  86. }
  87. #endif // ONLY_1_1
  88. #if NET_2_0
  89. [MonoTODO]
  90. public override int VisibleFieldCount
  91. {
  92. get { throw new NotImplementedException (); }
  93. }
  94. [MonoTODO]
  95. protected override bool IsValidRow
  96. {
  97. get { throw new NotImplementedException (); }
  98. }
  99. #endif // NET_2_0
  100. public
  101. #if NET_2_0
  102. override
  103. #endif // NET_2_0
  104. int Depth {
  105. get {
  106. return 0; // no nested selects supported
  107. }
  108. }
  109. public
  110. #if NET_2_0
  111. override
  112. #endif // NET_2_0
  113. int FieldCount {
  114. get {
  115. return cols.Length;
  116. }
  117. }
  118. public
  119. #if NET_2_0
  120. override
  121. #endif // NET_2_0
  122. bool IsClosed {
  123. get {
  124. return !open;
  125. }
  126. }
  127. public
  128. #if NET_2_0
  129. override
  130. #endif // NET_2_0
  131. object this[string name] {
  132. get {
  133. int pos;
  134. if (currentRow == -1)
  135. throw new InvalidOperationException ();
  136. pos = ColIndex(name);
  137. if (pos == -1)
  138. throw new IndexOutOfRangeException ();
  139. return this[pos];
  140. }
  141. }
  142. public
  143. #if NET_2_0
  144. override
  145. #endif // NET_2_0
  146. object this[int index] {
  147. get {
  148. return (object) GetValue (index);
  149. }
  150. }
  151. [MonoTODO]
  152. public
  153. #if NET_2_0
  154. override
  155. #endif // NET_2_0
  156. int RecordsAffected {
  157. get {
  158. return -1;
  159. }
  160. }
  161. [MonoTODO]
  162. public
  163. #if NET_2_0
  164. override
  165. #endif // NET_2_0
  166. bool HasRows {
  167. get { throw new NotImplementedException(); }
  168. }
  169. #endregion
  170. #region Methods
  171. private int ColIndex(string colname)
  172. {
  173. int i=0;
  174. foreach (OdbcColumn col in cols)
  175. {
  176. if (col != null && col.ColumnName==colname)
  177. return i;
  178. i++;
  179. }
  180. return -1;
  181. }
  182. // Dynamically load column descriptions as needed.
  183. private OdbcColumn GetColumn(int ordinal)
  184. {
  185. if (cols[ordinal]==null)
  186. {
  187. short bufsize=255;
  188. byte[] colname_buffer=new byte[bufsize];
  189. string colname;
  190. short colname_size=0;
  191. uint ColSize=0;
  192. short DecDigits=0, Nullable=0, dt=0;
  193. OdbcReturn ret=libodbc.SQLDescribeCol(hstmt, Convert.ToUInt16(ordinal+1),
  194. colname_buffer, bufsize, ref colname_size, ref dt, ref ColSize,
  195. ref DecDigits, ref Nullable);
  196. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  197. throw new OdbcException(new OdbcError("SQLDescribeCol",OdbcHandleType.Stmt,hstmt));
  198. colname=System.Text.Encoding.Default.GetString(colname_buffer);
  199. colname=colname.Replace((char) 0,' ').Trim();
  200. OdbcColumn c=new OdbcColumn(colname, (SQL_TYPE) dt);
  201. c.AllowDBNull=(Nullable!=0);
  202. c.Digits=DecDigits;
  203. if (c.IsStringType)
  204. c.MaxLength=(int)ColSize;
  205. cols[ordinal]=c;
  206. }
  207. return cols[ordinal];
  208. }
  209. public
  210. #if NET_2_0
  211. override
  212. #endif // NET_2_0
  213. void Close ()
  214. {
  215. // FIXME : have to implement output parameter binding
  216. OdbcReturn ret = libodbc.SQLFreeStmt (hstmt, libodbc.SQLFreeStmtOptions.Close);
  217. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  218. throw new OdbcException(new OdbcError("SQLCloseCursor",OdbcHandleType.Stmt,hstmt));
  219. open = false;
  220. currentRow = -1;
  221. ret = libodbc.SQLFreeHandle( (ushort) OdbcHandleType.Stmt, hstmt);
  222. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  223. throw new OdbcException(new OdbcError("SQLFreeHandle",OdbcHandleType.Stmt,hstmt));
  224. if ((this.CommandBehavior & CommandBehavior.CloseConnection)==CommandBehavior.CloseConnection)
  225. this.command.Connection.Close();
  226. }
  227. ~OdbcDataReader ()
  228. {
  229. if (open)
  230. Close ();
  231. }
  232. public
  233. #if NET_2_0
  234. override
  235. #endif // NET_2_0
  236. bool GetBoolean (int ordinal)
  237. {
  238. return (bool) GetValue(ordinal);
  239. }
  240. public
  241. #if NET_2_0
  242. override
  243. #endif // NET_2_0
  244. byte GetByte (int ordinal)
  245. {
  246. return (byte) Convert.ToByte(GetValue(ordinal));
  247. }
  248. public
  249. #if NET_2_0
  250. override
  251. #endif // NET_2_0
  252. long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length)
  253. {
  254. OdbcReturn ret = OdbcReturn.Error;
  255. bool copyBuffer = false;
  256. int returnVal = 0, outsize = 0;
  257. byte [] tbuff = new byte [length+1];
  258. length = buffer == null ? 0 : length;
  259. ret=libodbc.SQLGetData (hstmt, (ushort) (ordinal+1), SQL_C_TYPE.BINARY, tbuff, length,
  260. ref outsize);
  261. if (ret == OdbcReturn.NoData)
  262. return 0;
  263. if ( (ret != OdbcReturn.Success) && (ret != OdbcReturn.SuccessWithInfo))
  264. throw new OdbcException (new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt));
  265. OdbcError odbcErr = null;
  266. if ( (ret == OdbcReturn.SuccessWithInfo))
  267. odbcErr = new OdbcError ("SQLGetData", OdbcHandleType.Stmt, hstmt);
  268. if (buffer == null)
  269. return outsize; //if buffer is null,return length of the field
  270. if (ret == OdbcReturn.SuccessWithInfo) {
  271. if (outsize == (int) OdbcLengthIndicator.NoTotal)
  272. copyBuffer = true;
  273. else if (outsize == (int) OdbcLengthIndicator.NullData) {
  274. copyBuffer = false;
  275. returnVal = -1;
  276. } else {
  277. string sqlstate = odbcErr.SQLState;
  278. //SQLState: String Data, Right truncated
  279. if (sqlstate != libodbc.SQLSTATE_RIGHT_TRUNC)
  280. throw new OdbcException ( odbcErr);
  281. copyBuffer = true;
  282. }
  283. } else {
  284. copyBuffer = outsize == -1 ? false : true;
  285. returnVal = outsize;
  286. }
  287. if (copyBuffer) {
  288. int i = 0;
  289. while (tbuff [i] != libodbc.C_NULL) {
  290. buffer [bufferIndex + i] = tbuff [i];
  291. i++;
  292. }
  293. returnVal = i;
  294. }
  295. return returnVal;
  296. }
  297. [MonoTODO]
  298. public
  299. #if NET_2_0
  300. override
  301. #endif // NET_2_0
  302. char GetChar (int ordinal)
  303. {
  304. throw new NotImplementedException ();
  305. }
  306. [MonoTODO]
  307. public
  308. #if NET_2_0
  309. override
  310. #endif // NET_2_0
  311. long GetChars (int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length)
  312. {
  313. throw new NotImplementedException ();
  314. }
  315. [MonoTODO]
  316. [EditorBrowsableAttribute (EditorBrowsableState.Never)]
  317. public
  318. #if NET_2_0
  319. new
  320. #endif // NET_2_0
  321. IDataReader GetData (int ordinal)
  322. {
  323. throw new NotImplementedException ();
  324. }
  325. public
  326. #if NET_2_0
  327. override
  328. #endif // NET_2_0
  329. string GetDataTypeName (int index)
  330. {
  331. return GetColumn(index).OdbcType.ToString();
  332. }
  333. public DateTime GetDate(int ordinal) {
  334. return GetDateTime(ordinal);
  335. }
  336. public
  337. #if NET_2_0
  338. override
  339. #endif // NET_2_0
  340. DateTime GetDateTime (int ordinal)
  341. {
  342. return (DateTime) GetValue(ordinal);
  343. }
  344. [MonoTODO]
  345. public
  346. #if NET_2_0
  347. override
  348. #endif // NET_2_0
  349. decimal GetDecimal (int ordinal)
  350. {
  351. throw new NotImplementedException ();
  352. }
  353. public
  354. #if NET_2_0
  355. override
  356. #endif // NET_2_0
  357. double GetDouble (int ordinal)
  358. {
  359. return (double) GetValue(ordinal);
  360. }
  361. public
  362. #if NET_2_0
  363. override
  364. #endif // NET_2_0
  365. Type GetFieldType (int index)
  366. {
  367. return GetColumn(index).DataType;
  368. }
  369. public
  370. #if NET_2_0
  371. override
  372. #endif // NET_2_0
  373. float GetFloat (int ordinal)
  374. {
  375. return (float) GetValue(ordinal);
  376. }
  377. [MonoTODO]
  378. public
  379. #if NET_2_0
  380. override
  381. #endif // NET_2_0
  382. Guid GetGuid (int ordinal)
  383. {
  384. throw new NotImplementedException ();
  385. }
  386. public
  387. #if NET_2_0
  388. override
  389. #endif // NET_2_0
  390. short GetInt16 (int ordinal)
  391. {
  392. return (short) GetValue(ordinal);
  393. }
  394. public
  395. #if NET_2_0
  396. override
  397. #endif // NET_2_0
  398. int GetInt32 (int ordinal)
  399. {
  400. return (int) GetValue(ordinal);
  401. }
  402. public
  403. #if NET_2_0
  404. override
  405. #endif // NET_2_0
  406. long GetInt64 (int ordinal)
  407. {
  408. return (long) GetValue(ordinal);
  409. }
  410. public
  411. #if NET_2_0
  412. override
  413. #endif // NET_2_0
  414. string GetName (int index)
  415. {
  416. return GetColumn(index).ColumnName;
  417. }
  418. public
  419. #if NET_2_0
  420. override
  421. #endif // NET_2_0
  422. int GetOrdinal (string name)
  423. {
  424. int i=ColIndex(name);
  425. if (i==-1)
  426. throw new IndexOutOfRangeException ();
  427. else
  428. return i;
  429. }
  430. [MonoTODO]
  431. public
  432. #if NET_2_0
  433. override
  434. #endif // NET_2_0
  435. DataTable GetSchemaTable()
  436. {
  437. DataTable dataTableSchema = null;
  438. // Only Results from SQL SELECT Queries
  439. // get a DataTable for schema of the result
  440. // otherwise, DataTable is null reference
  441. if(cols.Length > 0)
  442. {
  443. dataTableSchema = new DataTable ();
  444. dataTableSchema.Columns.Add ("ColumnName", typeof (string));
  445. dataTableSchema.Columns.Add ("ColumnOrdinal", typeof (int));
  446. dataTableSchema.Columns.Add ("ColumnSize", typeof (int));
  447. dataTableSchema.Columns.Add ("NumericPrecision", typeof (int));
  448. dataTableSchema.Columns.Add ("NumericScale", typeof (int));
  449. dataTableSchema.Columns.Add ("IsUnique", typeof (bool));
  450. dataTableSchema.Columns.Add ("IsKey", typeof (bool));
  451. DataColumn dc = dataTableSchema.Columns["IsKey"];
  452. dc.AllowDBNull = true; // IsKey can have a DBNull
  453. dataTableSchema.Columns.Add ("BaseCatalogName", typeof (string));
  454. dataTableSchema.Columns.Add ("BaseColumnName", typeof (string));
  455. dataTableSchema.Columns.Add ("BaseSchemaName", typeof (string));
  456. dataTableSchema.Columns.Add ("BaseTableName", typeof (string));
  457. dataTableSchema.Columns.Add ("DataType", typeof(Type));
  458. dataTableSchema.Columns.Add ("AllowDBNull", typeof (bool));
  459. dataTableSchema.Columns.Add ("ProviderType", typeof (int));
  460. dataTableSchema.Columns.Add ("IsAliased", typeof (bool));
  461. dataTableSchema.Columns.Add ("IsExpression", typeof (bool));
  462. dataTableSchema.Columns.Add ("IsIdentity", typeof (bool));
  463. dataTableSchema.Columns.Add ("IsAutoIncrement", typeof (bool));
  464. dataTableSchema.Columns.Add ("IsRowVersion", typeof (bool));
  465. dataTableSchema.Columns.Add ("IsHidden", typeof (bool));
  466. dataTableSchema.Columns.Add ("IsLong", typeof (bool));
  467. dataTableSchema.Columns.Add ("IsReadOnly", typeof (bool));
  468. DataRow schemaRow;
  469. for (int i = 0; i < cols.Length; i += 1 )
  470. {
  471. OdbcColumn col=GetColumn(i);
  472. schemaRow = dataTableSchema.NewRow ();
  473. dataTableSchema.Rows.Add (schemaRow);
  474. schemaRow["ColumnName"] = col.ColumnName;
  475. schemaRow["ColumnOrdinal"] = i + 1;
  476. schemaRow["ColumnSize"] = col.MaxLength;
  477. schemaRow["NumericPrecision"] = 0;
  478. schemaRow["NumericScale"] = 0;
  479. // TODO: need to get KeyInfo
  480. schemaRow["IsUnique"] = false;
  481. schemaRow["IsKey"] = DBNull.Value;
  482. schemaRow["BaseCatalogName"] = "";
  483. schemaRow["BaseColumnName"] = col.ColumnName;
  484. schemaRow["BaseSchemaName"] = "";
  485. schemaRow["BaseTableName"] = "";
  486. schemaRow["DataType"] = col.DataType;
  487. schemaRow["AllowDBNull"] = col.AllowDBNull;
  488. schemaRow["ProviderType"] = (int) col.OdbcType;
  489. // TODO: all of these
  490. schemaRow["IsAliased"] = false;
  491. schemaRow["IsExpression"] = false;
  492. schemaRow["IsIdentity"] = false;
  493. schemaRow["IsAutoIncrement"] = false;
  494. schemaRow["IsRowVersion"] = false;
  495. schemaRow["IsHidden"] = false;
  496. schemaRow["IsLong"] = false;
  497. schemaRow["IsReadOnly"] = false;
  498. // FIXME: according to Brian,
  499. // this does not work on MS .NET
  500. // however, we need it for Mono
  501. // for now
  502. schemaRow.AcceptChanges();
  503. }
  504. dataTableSchema.AcceptChanges();
  505. }
  506. return dataTableSchema;
  507. }
  508. public
  509. #if NET_2_0
  510. override
  511. #endif // NET_2_0
  512. string GetString (int ordinal)
  513. {
  514. return (string) GetValue(ordinal);
  515. }
  516. [MonoTODO]
  517. public TimeSpan GetTime (int ordinal)
  518. {
  519. throw new NotImplementedException ();
  520. }
  521. public
  522. #if NET_2_0
  523. override
  524. #endif // NET_2_0
  525. object GetValue (int ordinal)
  526. {
  527. if (currentRow == -1)
  528. throw new IndexOutOfRangeException ();
  529. if (ordinal>cols.Length-1 || ordinal<0)
  530. throw new IndexOutOfRangeException ();
  531. OdbcReturn ret;
  532. int outsize=0, bufsize;
  533. byte[] buffer;
  534. OdbcColumn col=GetColumn(ordinal);
  535. object DataValue=null;
  536. ushort ColIndex=Convert.ToUInt16(ordinal+1);
  537. // Check cached values
  538. if (col.Value==null)
  539. {
  540. // odbc help file
  541. // mk:@MSITStore:C:\program%20files\Microsoft%20Data%20Access%20SDK\Docs\odbc.chm::/htm/odbcc_data_types.htm
  542. switch (col.OdbcType)
  543. {
  544. case OdbcType.Decimal:
  545. bufsize=50;
  546. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for decima.
  547. // 2005 03 10 : this now works with unixodbc with numeric c type.
  548. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  549. byte[] temp = new byte[outsize];
  550. for (int i=0;i<outsize;i++)
  551. temp[i]=buffer[i];
  552. if (outsize!=-1)
  553. DataValue=Decimal.Parse(System.Text.Encoding.Default.GetString(temp));
  554. break;
  555. case OdbcType.TinyInt:
  556. short short_data=0;
  557. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref short_data, 0, ref outsize);
  558. DataValue=System.Convert.ToByte(short_data);
  559. break;
  560. case OdbcType.Int:
  561. int int_data=0;
  562. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref int_data, 0, ref outsize);
  563. DataValue=int_data;
  564. break;
  565. case OdbcType.SmallInt:
  566. short sint_data=0;
  567. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref sint_data, 0, ref outsize);
  568. DataValue=sint_data;
  569. break;
  570. case OdbcType.BigInt:
  571. long long_data=0;
  572. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref long_data, 0, ref outsize);
  573. DataValue=long_data;
  574. break;
  575. case OdbcType.NVarChar:
  576. bufsize=col.MaxLength*2+1; // Unicode is double byte
  577. buffer=new byte[bufsize];
  578. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  579. if (outsize!=-1)
  580. DataValue=System.Text.Encoding.Unicode.GetString(buffer,0,outsize);
  581. break;
  582. case OdbcType.VarChar:
  583. bufsize=col.MaxLength+1;
  584. buffer=new byte[bufsize]; // According to sqlext.h, use SQL_CHAR for both char and varchar
  585. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, buffer, bufsize, ref outsize);
  586. if (outsize!=-1)
  587. DataValue=System.Text.Encoding.Default.GetString(buffer,0,outsize);
  588. break;
  589. case OdbcType.Real:
  590. float float_data=0;
  591. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref float_data, 0, ref outsize);
  592. DataValue=float_data;
  593. break;
  594. case OdbcType.Double:
  595. double double_data=0;
  596. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref double_data, 0, ref outsize);
  597. DataValue=double_data;
  598. break;
  599. case OdbcType.Timestamp:
  600. case OdbcType.DateTime:
  601. case OdbcType.Date:
  602. case OdbcType.Time:
  603. OdbcTimestamp ts_data=new OdbcTimestamp();
  604. ret=libodbc.SQLGetData(hstmt, ColIndex, col.SqlCType, ref ts_data, 0, ref outsize);
  605. if (outsize!=-1) // This means SQL_NULL_DATA
  606. DataValue=new DateTime(ts_data.year,ts_data.month,ts_data.day,ts_data.hour,
  607. ts_data.minute,ts_data.second,Convert.ToInt32(ts_data.fraction));
  608. break;
  609. case OdbcType.Binary :
  610. case OdbcType.Image :
  611. bufsize = col.MaxLength + 1;
  612. buffer = new byte [bufsize];
  613. long read = GetBytes (ordinal, 0, buffer, 0, bufsize);
  614. ret = OdbcReturn.Success;
  615. DataValue = buffer;
  616. break;
  617. default:
  618. bufsize=255;
  619. buffer=new byte[bufsize];
  620. ret=libodbc.SQLGetData(hstmt, ColIndex, SQL_C_TYPE.CHAR, buffer, bufsize, ref outsize);
  621. DataValue=System.Text.Encoding.Default.GetString(buffer);
  622. break;
  623. }
  624. if ((ret!=OdbcReturn.Success) && (ret!=OdbcReturn.SuccessWithInfo))
  625. throw new OdbcException(new OdbcError("SQLGetData",OdbcHandleType.Stmt,hstmt));
  626. if (outsize==-1) // This means SQL_NULL_DATA
  627. col.Value=DBNull.Value;
  628. else
  629. col.Value=DataValue;
  630. }
  631. return col.Value;
  632. }
  633. public
  634. #if NET_2_0
  635. override
  636. #endif // NET_2_0
  637. int GetValues (object[] values)
  638. {
  639. int numValues = 0;
  640. // copy values
  641. for (int i = 0; i < values.Length; i++) {
  642. if (i < FieldCount) {
  643. values[i] = GetValue(i);
  644. }
  645. else {
  646. values[i] = null;
  647. }
  648. }
  649. // get number of object instances in array
  650. if (values.Length < FieldCount)
  651. numValues = values.Length;
  652. else if (values.Length == FieldCount)
  653. numValues = FieldCount;
  654. else
  655. numValues = FieldCount;
  656. return numValues;
  657. }
  658. #if ONLY_1_1
  659. [MonoTODO]
  660. IDataReader IDataRecord.GetData (int ordinal)
  661. {
  662. throw new NotImplementedException ();
  663. }
  664. [MonoTODO]
  665. void IDisposable.Dispose ()
  666. {
  667. }
  668. IEnumerator IEnumerable.GetEnumerator ()
  669. {
  670. return new DbEnumerator (this);
  671. }
  672. #endif // ONLY_1_1
  673. public
  674. #if NET_2_0
  675. override
  676. #endif // NET_2_0
  677. bool IsDBNull (int ordinal)
  678. {
  679. return (GetValue(ordinal) is DBNull);
  680. }
  681. /// <remarks>
  682. /// Move to the next result set.
  683. /// </remarks>
  684. public
  685. #if NET_2_0
  686. override
  687. #endif // NET_2_0
  688. bool NextResult ()
  689. {
  690. OdbcReturn ret = OdbcReturn.Success;
  691. ret = libodbc.SQLMoreResults (hstmt);
  692. if (ret == OdbcReturn.Success) {
  693. short colcount = 0;
  694. libodbc.SQLNumResultCols (hstmt, ref colcount);
  695. cols = new OdbcColumn [colcount];
  696. GetSchemaTable ();
  697. }
  698. return (ret==OdbcReturn.Success);
  699. }
  700. /// <remarks>
  701. /// Load the next row in the current result set.
  702. /// </remarks>
  703. public bool NextRow ()
  704. {
  705. OdbcReturn ret=libodbc.SQLFetch (hstmt);
  706. if (ret != OdbcReturn.Success)
  707. currentRow = -1;
  708. else
  709. currentRow++;
  710. // Clear cached values from last record
  711. foreach (OdbcColumn col in cols)
  712. {
  713. if (col != null)
  714. col.Value = null;
  715. }
  716. return (ret == OdbcReturn.Success);
  717. }
  718. public
  719. #if NET_2_0
  720. override
  721. #endif // NET_2_0
  722. bool Read ()
  723. {
  724. return NextRow ();
  725. }
  726. #if NET_2_0
  727. [MonoTODO]
  728. public override object GetProviderSpecificValue (int i)
  729. {
  730. throw new NotImplementedException ();
  731. }
  732. [MonoTODO]
  733. public override int GetProviderSpecificValues (object[] values)
  734. {
  735. throw new NotImplementedException ();
  736. }
  737. [MonoTODO]
  738. public override Type GetFieldProviderSpecificType (int i)
  739. {
  740. throw new NotImplementedException ();
  741. }
  742. #endif // NET_2_0
  743. #endregion
  744. }
  745. }