0

I'm puzzled as to what cast in C# to make for a query to T-SQL that returns a tinyint.

Using a SqlDataReader...

int Precision = 0;

SqlDataReader reader = myCommand.ExecuteReader();

if (reader.Read())
{
   Precision = reader.GetInt16(0);
}

I'm getting an InvalidCastException when I use GetInt16, and that's the smallest integer cast apparently available. In the same code I successfully cast a smallint to int16.

What Get.... do I use for tinyint?

Edited to add:

An answer which was subsequently deleted said to use GetByte. I did, and that worked.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cyberherbalist
  • 12,061
  • 17
  • 83
  • 121
  • 2
    I knew this had to have been answered already, but a search of SO didn't yield the question that @CodeCaster indicated as a prior question -- and the question itself didn't either. I thank everyone who answered. – Cyberherbalist Oct 10 '14 at 22:57

1 Answers1

2

As mentioned in "SQL-CLR Type Mapping", it is short when you are mapping from SQL to CLR.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • The documentation says TINYINT maps to System.Int16. But the code above does not work. On the other hand, GetByte *does* work. – Cyberherbalist Oct 10 '14 at 23:01
  • 1
    Ok, In [this](http://msdn.microsoft.com/en-us/library/cc716729.aspx) documentation stated that `tinyint` maps to `byte` which is more suitable in your case because AOD.NET on the scene. (the above mentioned doc related to LINQ2SQL). – Hamlet Hakobyan Oct 10 '14 at 23:05
  • That's fine, then. Thanks! The duplicate question has a nice table to it, also. – Cyberherbalist Oct 10 '14 at 23:12