0

I have an ancient Access97 database that contains astronomical data that I would like to use in a telescope's pointing software. The Hour and Minute of Right Ascension are stored as UnsignedTinyInt.

I am assuming UnsignedTinyInt is an unsigned short, or ushort. If I code the following, where "reader" is an OleDbDataReader and fld is the ordinal value. Int16 is the shortest integer available, apparently.

select myValue from myTable;

ushort myValue;
myValue = reader.GetInt16(fld);

The exact exception is Invalid Cast. There are a number of Get methods available in the OleDbDataReader, including three GetInts. No GetInt works, no matter how I declare myValue.

Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • `I am assuming UnsignedTinyInt is an unsigned short` That is incorrect. It is a `byte`. – mjwills Dec 23 '17 at 23:49
  • `No GetInt works` That is because they are designed for (2 bytes) `short`, (4 bytes) `int` and (8 bytes) `long` - while what you have is a (1 byte) `byte`. – mjwills Dec 24 '17 at 11:10
  • In future you can call [`object GetValue(int ordinal)`](https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.getvalue(v=vs.110).aspx) and inspect the returned object (`myValue.GetType()`) to see what type `OleDbDataReader` considers the field to be, that or [`GetSchemaTable`](https://msdn.microsoft.com/en-us/library/system.data.oledb.oledbdatareader.getschematable(v=vs.110).aspx). – ta.speot.is Dec 26 '17 at 01:00
  • Also there is [this table](https://support.microsoft.com/en-us/help/320435/info-oledbtype-enumeration-vs--microsoft-access-data-types) to consult. – ta.speot.is Dec 26 '17 at 01:01

1 Answers1

1

I was able to solve this with:

select myValue from myTable;

byte myValue;
myValue = reader.GetByte(fld);
Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • 1
    Why `ushort`? It has neither the size of the `byte` field in the dictionary, nor the size of the `int` or `uint` the math would be done on. – Jon Hanna Dec 23 '17 at 23:30
  • @JonHanna, it worked to get the information I needed. The unsigned tiny int in the Access97 database system is very tiny, as indicated, and sure you can put it into a byte in your code, but it's smaller than a byte or even uint. In my final code for the project, all I was doing was storing it as a regular integer in the Sql Server database I created to hold the data I was extracting. I modified my answer here in response to the pressure of users who didn't like my using the ushort. Oh well. By the way, I never finished reading your book, but found it interesting nevertheless. Happy trails! – Cyberherbalist Jan 07 '18 at 20:39
  • 1
    `ushort` is a bit of a worse of both worlds compromise. `byte` (or `sbyte` which is the most direct comparison to tiny in Access) has the size advantage, but with any arithmetic or bitwise use it gets promoted to `int` so there's a speed advantage of using that and not bothering to cast back again after operations, so there's advantages in either extreme but not in between. (The book is best read skim-reading the first chapter, its sort of has to get what it gets out of the way, out of the way, but looking back after a few years I'm afraid it starts rather weak). – Jon Hanna Jan 07 '18 at 23:51