SqlDataReader.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 java.sql;
  32. namespace System.Data.SqlClient
  33. {
  34. public class SqlDataReader : System.Data.Common.AbstractDataReader
  35. {
  36. #region Constructors
  37. internal SqlDataReader(SqlCommand command) : base(command)
  38. {
  39. }
  40. #endregion // Constructors
  41. #region Properties
  42. #endregion // Properties
  43. #region Methods
  44. protected override SystemException CreateException(string message, SQLException e)
  45. {
  46. return new SqlException(message, e, (SqlConnection)_command.Connection);
  47. }
  48. protected override SystemException CreateException(java.io.IOException e)
  49. {
  50. return new SqlException(e, (SqlConnection)_command.Connection);
  51. }
  52. public override String GetDataTypeName(int columnIndex)
  53. {
  54. try {
  55. string jdbcTypeName = Results.getMetaData().getColumnTypeName(columnIndex + 1);
  56. return SqlConvert.JdbcTypeNameToDbTypeName(jdbcTypeName);
  57. }
  58. catch (SQLException e) {
  59. throw CreateException(e);
  60. }
  61. }
  62. protected override int GetProviderType(int jdbcType)
  63. {
  64. return (int)SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  65. }
  66. // Gets the value of the specified column as a SqlBinary.
  67. public SqlBinary GetSqlBinary(int columnIndex)
  68. {
  69. byte[] bytes = GetBytes(columnIndex);
  70. if(IsDBNull(columnIndex)) {
  71. return SqlBinary.Null;
  72. }
  73. else {
  74. return new SqlBinary(bytes);
  75. }
  76. }
  77. // Gets the value of the specified column as a SqlBoolean.
  78. public SqlBoolean GetSqlBoolean(int columnIndex)
  79. {
  80. bool boolean = GetBoolean(columnIndex);
  81. if(IsDBNull(columnIndex)) {
  82. return SqlBoolean.Null;
  83. }
  84. else {
  85. return new SqlBoolean(boolean);
  86. }
  87. }
  88. // Gets the value of the specified column as a SqlByte.
  89. public SqlByte GetSqlByte(int columnIndex)
  90. {
  91. byte byt = GetByte(columnIndex);
  92. if(IsDBNull(columnIndex)) {
  93. return SqlByte.Null;
  94. }
  95. else {
  96. return new SqlByte(byt);
  97. }
  98. }
  99. // Gets the value of the specified column as a SqlDecimal.
  100. public SqlDecimal GetSqlDecimal(int columnIndex)
  101. {
  102. decimal dec = GetDecimal(columnIndex);
  103. if(IsDBNull(columnIndex)) {
  104. return SqlDecimal.Null;
  105. }
  106. else {
  107. return new SqlDecimal(dec);
  108. }
  109. }
  110. // Gets the value of the specified column as a SqlDateTime.
  111. public SqlDateTime GetSqlDateTime(int columnIndex)
  112. {
  113. DateTime dateTime = GetDateTime(columnIndex);
  114. if(IsDBNull(columnIndex)) {
  115. return SqlDateTime.Null;
  116. }
  117. else {
  118. return new SqlDateTime(dateTime);
  119. }
  120. }
  121. // Gets the value of the specified column as a SqlDouble.
  122. public SqlDouble GetSqlDouble(int columnIndex)
  123. {
  124. double doubl = GetDouble(columnIndex);
  125. if(IsDBNull(columnIndex)) {
  126. return SqlDouble.Null;
  127. }
  128. else {
  129. return new SqlDouble(doubl);
  130. }
  131. }
  132. // Gets the value of the specified column as a SqlInt16.
  133. public SqlInt16 GetSqlInt16(int columnIndex)
  134. {
  135. short s = GetInt16(columnIndex);
  136. if(IsDBNull(columnIndex)) {
  137. return SqlInt16.Null;
  138. }
  139. else {
  140. return new SqlInt16(s);
  141. }
  142. }
  143. // Gets the value of the specified column as a SqlInt32.
  144. public SqlInt32 GetSqlInt32(int columnIndex)
  145. {
  146. int i = GetInt32(columnIndex);
  147. if(IsDBNull(columnIndex)) {
  148. return SqlInt32.Null;
  149. }
  150. else {
  151. return new SqlInt32(i);
  152. }
  153. }
  154. // Gets the value of the specified column as a SqlInt64.
  155. public SqlInt64 GetSqlInt64(int columnIndex)
  156. {
  157. long l = GetInt64(columnIndex);
  158. if(IsDBNull(columnIndex)) {
  159. return SqlInt64.Null;
  160. }
  161. else {
  162. return new SqlInt64(l);
  163. }
  164. }
  165. // Gets the value of the specified column as a SqlMoney.
  166. public SqlMoney GetSqlMoney(int columnIndex)
  167. {
  168. decimal dec = GetDecimal(columnIndex);
  169. if(IsDBNull(columnIndex)) {
  170. return SqlMoney.Null;
  171. }
  172. else {
  173. return new SqlMoney(dec);
  174. }
  175. }
  176. // Gets the value of the specified column as a SqlSingle.
  177. public SqlSingle GetSqlSingle(int columnIndex)
  178. {
  179. float f = GetFloat(columnIndex);
  180. if(IsDBNull(columnIndex)) {
  181. return SqlSingle.Null;
  182. }
  183. else {
  184. return new SqlSingle(f);
  185. }
  186. }
  187. // Gets the value of the specified column as a SqlString.
  188. public SqlString GetSqlString(int columnIndex)
  189. {
  190. string str = GetString(columnIndex);
  191. if(IsDBNull(columnIndex)) {
  192. return SqlString.Null;
  193. }
  194. else {
  195. return new SqlString(str);
  196. }
  197. }
  198. // Gets the value of the specified column as a SqlGuid.
  199. public SqlGuid GetSqlGuid(int columnIndex)
  200. {
  201. object obj = GetValue(columnIndex);
  202. if(IsDBNull(columnIndex)) {
  203. return SqlGuid.Null;
  204. }
  205. else {
  206. if (obj is byte[]) {
  207. return new SqlGuid((byte[])obj);
  208. }
  209. else {
  210. return new SqlGuid((string)obj);
  211. }
  212. }
  213. }
  214. // Gets all the attribute columns in the current row.
  215. public int GetSqlValues(Object[] values)
  216. {
  217. int columnCount = FieldCount;
  218. int i = 0;
  219. for (; i < values.Length && i < columnCount; i++) {
  220. values[i] = GetSqlValue(i);
  221. }
  222. return i;
  223. }
  224. // Gets an Object that is a representation of the underlying SqlDbType Variant.
  225. public Object GetSqlValue(int columnIndex)
  226. {
  227. try {
  228. int jdbcType = ResultsMetaData.getColumnType(columnIndex + 1);
  229. SqlDbType sqlDbType = SqlConvert.JdbcTypeToSqlDbType(jdbcType);
  230. switch (sqlDbType) {
  231. case SqlDbType.BigInt : return GetSqlInt64(columnIndex);
  232. case SqlDbType.Binary : return GetSqlBinary(columnIndex);
  233. case SqlDbType.Bit : return GetSqlBoolean(columnIndex);
  234. case SqlDbType.Char : return GetSqlString(columnIndex);
  235. case SqlDbType.DateTime : return GetSqlDateTime(columnIndex);
  236. case SqlDbType.Decimal : return GetSqlDecimal(columnIndex);
  237. case SqlDbType.Float : return GetSqlDouble(columnIndex);
  238. case SqlDbType.Image : return GetSqlBinary(columnIndex);
  239. case SqlDbType.Int : return GetSqlInt32(columnIndex);
  240. case SqlDbType.Money : return GetSqlDecimal(columnIndex);
  241. case SqlDbType.NChar : return GetSqlString(columnIndex);
  242. case SqlDbType.NText : return GetSqlString(columnIndex);
  243. case SqlDbType.NVarChar : return GetSqlString(columnIndex);
  244. case SqlDbType.Real : return GetSqlSingle(columnIndex);
  245. case SqlDbType.UniqueIdentifier : return GetSqlGuid(columnIndex);
  246. case SqlDbType.SmallDateTime : return GetSqlDateTime(columnIndex);
  247. case SqlDbType.SmallInt : return GetSqlInt16(columnIndex);
  248. case SqlDbType.SmallMoney : return GetSqlDecimal(columnIndex);
  249. case SqlDbType.Text : return GetSqlString(columnIndex);
  250. case SqlDbType.Timestamp : return GetSqlDateTime(columnIndex);
  251. case SqlDbType.TinyInt : return GetSqlByte(columnIndex);
  252. case SqlDbType.VarBinary : return GetSqlBinary(columnIndex);
  253. case SqlDbType.VarChar : return GetSqlString(columnIndex);
  254. case SqlDbType.Variant : return GetValue(columnIndex);
  255. default : return GetValue(columnIndex);
  256. }
  257. }
  258. catch (SQLException exp) {
  259. throw new Exception(exp.Message);
  260. }
  261. }
  262. #endregion // Methods
  263. }
  264. }