SqlDataReader.cs 7.9 KB

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