SqlDataReader.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // System.Data.SqlClient.SqlDataReader
  3. //
  4. // Authors:
  5. // Konstantin Triger <[email protected]>
  6. // Boris Kirzner <[email protected]>
  7. //
  8. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Data.SqlTypes;
  31. using System.Data.ProviderBase;
  32. using java.sql;
  33. namespace System.Data.SqlClient
  34. {
  35. public class SqlDataReader : AbstractDataReader
  36. {
  37. #region Constructors
  38. internal SqlDataReader(SqlCommand command) : base(command)
  39. {
  40. }
  41. #endregion // Constructors
  42. #region Properties
  43. #endregion // Properties
  44. #region Methods
  45. protected sealed override SystemException CreateException(string message, SQLException e)
  46. {
  47. return new SqlException(message, e, (SqlConnection)_command.Connection);
  48. }
  49. protected sealed override SystemException CreateException(java.io.IOException e)
  50. {
  51. return new SqlException(e, (SqlConnection)_command.Connection);
  52. }
  53. public override String GetDataTypeName(int columnIndex)
  54. {
  55. try {
  56. string jdbcTypeName = Results.getMetaData().getColumnTypeName(columnIndex + 1);
  57. return SqlConvert.JdbcTypeNameToDbTypeName(jdbcTypeName);
  58. }
  59. catch (SQLException e) {
  60. throw CreateException(e);
  61. }
  62. }
  63. protected override int GetProviderType(int jdbcType)
  64. {
  65. return (int)SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  66. }
  67. // Gets the value of the specified column as a SqlBinary.
  68. public SqlBinary GetSqlBinary(int columnIndex)
  69. {
  70. byte[] bytes = GetBytes(columnIndex);
  71. if(IsDBNull(columnIndex)) {
  72. return SqlBinary.Null;
  73. }
  74. else {
  75. return new SqlBinary(bytes);
  76. }
  77. }
  78. // Gets the value of the specified column as a SqlBoolean.
  79. public SqlBoolean GetSqlBoolean(int columnIndex)
  80. {
  81. bool boolean = GetBoolean(columnIndex);
  82. if(IsDBNull(columnIndex)) {
  83. return SqlBoolean.Null;
  84. }
  85. else {
  86. return new SqlBoolean(boolean);
  87. }
  88. }
  89. // Gets the value of the specified column as a SqlByte.
  90. public SqlByte GetSqlByte(int columnIndex)
  91. {
  92. byte byt = GetByte(columnIndex);
  93. if(IsDBNull(columnIndex)) {
  94. return SqlByte.Null;
  95. }
  96. else {
  97. return new SqlByte(byt);
  98. }
  99. }
  100. // Gets the value of the specified column as a SqlDecimal.
  101. public SqlDecimal GetSqlDecimal(int columnIndex)
  102. {
  103. decimal dec = GetDecimal(columnIndex);
  104. if(IsDBNull(columnIndex)) {
  105. return SqlDecimal.Null;
  106. }
  107. else {
  108. return new SqlDecimal(dec);
  109. }
  110. }
  111. // Gets the value of the specified column as a SqlDateTime.
  112. public SqlDateTime GetSqlDateTime(int columnIndex)
  113. {
  114. DateTime dateTime = GetDateTime(columnIndex);
  115. if(IsDBNull(columnIndex)) {
  116. return SqlDateTime.Null;
  117. }
  118. else {
  119. return new SqlDateTime(dateTime);
  120. }
  121. }
  122. // Gets the value of the specified column as a SqlDouble.
  123. public SqlDouble GetSqlDouble(int columnIndex)
  124. {
  125. double doubl = GetDouble(columnIndex);
  126. if(IsDBNull(columnIndex)) {
  127. return SqlDouble.Null;
  128. }
  129. else {
  130. return new SqlDouble(doubl);
  131. }
  132. }
  133. // Gets the value of the specified column as a SqlInt16.
  134. public SqlInt16 GetSqlInt16(int columnIndex)
  135. {
  136. short s = GetInt16(columnIndex);
  137. if(IsDBNull(columnIndex)) {
  138. return SqlInt16.Null;
  139. }
  140. else {
  141. return new SqlInt16(s);
  142. }
  143. }
  144. // Gets the value of the specified column as a SqlInt32.
  145. public SqlInt32 GetSqlInt32(int columnIndex)
  146. {
  147. int i = GetInt32(columnIndex);
  148. if(IsDBNull(columnIndex)) {
  149. return SqlInt32.Null;
  150. }
  151. else {
  152. return new SqlInt32(i);
  153. }
  154. }
  155. // Gets the value of the specified column as a SqlInt64.
  156. public SqlInt64 GetSqlInt64(int columnIndex)
  157. {
  158. long l = GetInt64(columnIndex);
  159. if(IsDBNull(columnIndex)) {
  160. return SqlInt64.Null;
  161. }
  162. else {
  163. return new SqlInt64(l);
  164. }
  165. }
  166. // Gets the value of the specified column as a SqlMoney.
  167. public SqlMoney GetSqlMoney(int columnIndex)
  168. {
  169. decimal dec = GetDecimal(columnIndex);
  170. if(IsDBNull(columnIndex)) {
  171. return SqlMoney.Null;
  172. }
  173. else {
  174. return new SqlMoney(dec);
  175. }
  176. }
  177. // Gets the value of the specified column as a SqlSingle.
  178. public SqlSingle GetSqlSingle(int columnIndex)
  179. {
  180. float f = GetFloat(columnIndex);
  181. if(IsDBNull(columnIndex)) {
  182. return SqlSingle.Null;
  183. }
  184. else {
  185. return new SqlSingle(f);
  186. }
  187. }
  188. // Gets the value of the specified column as a SqlString.
  189. public SqlString GetSqlString(int columnIndex)
  190. {
  191. string str = GetString(columnIndex);
  192. if(IsDBNull(columnIndex)) {
  193. return SqlString.Null;
  194. }
  195. else {
  196. return new SqlString(str);
  197. }
  198. }
  199. // Gets the value of the specified column as a SqlGuid.
  200. public SqlGuid GetSqlGuid(int columnIndex)
  201. {
  202. object obj = GetValue(columnIndex);
  203. if(IsDBNull(columnIndex)) {
  204. return SqlGuid.Null;
  205. }
  206. else {
  207. if (obj is byte[]) {
  208. return new SqlGuid((byte[])obj);
  209. }
  210. else {
  211. return new SqlGuid((string)obj);
  212. }
  213. }
  214. }
  215. // Gets all the attribute columns in the current row.
  216. public int GetSqlValues(Object[] values)
  217. {
  218. int columnCount = FieldCount;
  219. int i = 0;
  220. for (; i < values.Length && i < columnCount; i++) {
  221. values[i] = GetSqlValue(i);
  222. }
  223. return i;
  224. }
  225. // Gets an Object that is a representation of the underlying SqlDbType Variant.
  226. public Object GetSqlValue(int columnIndex)
  227. {
  228. try {
  229. int jdbcType = ResultsMetaData.getColumnType(columnIndex + 1);
  230. SqlDbType sqlDbType = SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  231. switch (sqlDbType) {
  232. case SqlDbType.BigInt : return GetSqlInt64(columnIndex);
  233. case SqlDbType.Binary : return GetSqlBinary(columnIndex);
  234. case SqlDbType.Bit : return GetSqlBoolean(columnIndex);
  235. case SqlDbType.Char : return GetSqlString(columnIndex);
  236. case SqlDbType.DateTime : return GetSqlDateTime(columnIndex);
  237. case SqlDbType.Decimal : return GetSqlDecimal(columnIndex);
  238. case SqlDbType.Float : return GetSqlDouble(columnIndex);
  239. case SqlDbType.Image : return GetSqlBinary(columnIndex);
  240. case SqlDbType.Int : return GetSqlInt32(columnIndex);
  241. case SqlDbType.Money : return GetSqlDecimal(columnIndex);
  242. case SqlDbType.NChar : return GetSqlString(columnIndex);
  243. case SqlDbType.NText : return GetSqlString(columnIndex);
  244. case SqlDbType.NVarChar : return GetSqlString(columnIndex);
  245. case SqlDbType.Real : return GetSqlSingle(columnIndex);
  246. case SqlDbType.UniqueIdentifier : return GetSqlGuid(columnIndex);
  247. case SqlDbType.SmallDateTime : return GetSqlDateTime(columnIndex);
  248. case SqlDbType.SmallInt : return GetSqlInt16(columnIndex);
  249. case SqlDbType.SmallMoney : return GetSqlDecimal(columnIndex);
  250. case SqlDbType.Text : return GetSqlString(columnIndex);
  251. case SqlDbType.Timestamp : return GetSqlDateTime(columnIndex);
  252. case SqlDbType.TinyInt : return GetSqlByte(columnIndex);
  253. case SqlDbType.VarBinary : return GetSqlBinary(columnIndex);
  254. case SqlDbType.VarChar : return GetSqlString(columnIndex);
  255. case SqlDbType.Variant : return GetValue(columnIndex);
  256. default : return GetValue(columnIndex);
  257. }
  258. }
  259. catch (SQLException exp) {
  260. throw new Exception(exp.Message);
  261. }
  262. }
  263. #endregion // Methods
  264. }
  265. }