I need to get a Bit from a sql server into c#. I tried differnt solutions like:
bool active = rdr.GetSqlBinary(5);
Int16 active = rdr.GetSqlBinary(5);
But can't find any way to get the Bit. Can someone give an example?
If you're certain that the column values will never be NULL
then the following will do the trick:
bool active = rdr.GetBoolean(rdr.GetOrdinal("Active"));
If it's possible that NULL
values might be returned:
int oActive = rdr.GetOrdinal("Active");
bool? active = rdr.IsDBNull(oActive) ? (bool?)null : rdr.GetBoolean(oActive);
Use the GetSqlBoolean method.
Update
Make sure you cast your return value as a boolean i.e.
var active = (bool)rdr.GetSqlBoolean(5);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With